// import a library for an LCD
#include <LiquidCrystal.h>
void diodePulseToggle();
void writeLCDMessage();
bool shouldResetSecondRow();
void writeLCDRelayState();
void lcdRowReset(int rowNr);
void createSpecialCharacters();
// LCD setup
const int RS = 11,
E = 12,
D4 = 6,
D5 = 5,
D6 = 4,
D7 = 3;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
const int LCD_2_ROW_START_COL = 7;
// pins declarations
const int redLedPin = 8;
//const int greenLedPin = 7;
const int redBtnPin = 2;
const int relayPin = 7;
// arrays with pin numbers
const int ledPinNrsArray[] = {redLedPin, relayPin};
const int btnPinNrsArray[] = {redBtnPin};
const char *const pinColors[] = {"red", "green"};
const char *const pinColorsPL[] = {"czerwony", "zielony"};
int ledStates[] = {0, 0};
// length counter for the above
const int amountOfPinsUsed = (sizeof(ledPinNrsArray) / sizeof(ledPinNrsArray[0]));
// diodes counter
int x = 0;
// init diode status
int shouldLightOn = 0;
int relayState = 0;
// time counter initial values
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long currentMillis = 0;
const byte a_[8] = { //ą
B00000,
B01110,
B00001,
B01111,
B10001,
B01111,
B00010,
B00001,
};
const byte c_[8] = { //ć
B00010,
B00100,
B01110,
B10000,
B10000,
B10001,
B01110,
B00000,
};
const byte e_[8] = { //ę
B00000,
B01110,
B10001,
B11111,
B10000,
B01110,
B00100,
B00010,
};
const byte l_[8] = { //ł
B01100,
B00100,
B00100,
B00110,
B01100,
B00100,
B01110,
B00000,
};
const byte n_[8] = { //ń
B00010,
B00100,
B10110,
B11001,
B10001,
B10001,
B10001,
B00000,
};
const byte o_[8] = { //ó
B00010,
B00100,
B01110,
B10001,
B10001,
B10001,
B01110,
B00000,
};
const byte s_[8] = { //ś
B00010,
B00100,
B01110,
B10000,
B01110,
B00001,
B11110,
B00000,
};
const byte z__[8] = { //ź
B00010,
B00100,
B11111,
B00010,
B00100,
B01000,
B11111,
B00000,
};
const byte z_[8] = { //ż
B00100,
B00000,
B11111,
B00010,
B00100,
B01000,
B11111,
B00000,
};
const byte heartSymbol[8] = {
B00000,
B01010,
B11111,
B11111,
B01110,
B00100,
B00000,
B00000
};
const byte resetCharacter[8] = {
B00000,B00000,B00000,B000000,B00000,B00000,B00000,B00000
};
void setup() {
Serial.begin(9600);
createSpecialCharacters();
// lcd printing init
lcd.begin(16,2);
// set cursor at the beginning (col, row)
lcd.setCursor(0,0);
lcd.print("Przeka");
lcd.write(byte(4));
lcd.print("nik");
lcd.setCursor(5,0);
lcd.write(byte(3));
// declare using pins
for (int i = 0; i < amountOfPinsUsed; i++) {
pinMode(ledPinNrsArray[i], OUTPUT);
}
pinMode(btnPinNrsArray[0], INPUT_PULLUP);
writeLCDMessage();
}
// main loop for reading buttons' actions
void loop() {
// default 1 means HIGH and 0 means LOW, so I use ! to convert
int newShouldLightOn = !digitalRead(redBtnPin);
// eliminate blinking & catch change
if (shouldLightOn != newShouldLightOn) {
shouldLightOn = newShouldLightOn;
}
for(int i = 0; i < amountOfPinsUsed; i++) {
x = i;
diodePulseToggle();
//if (shouldResetSecondRow()) lcdRowReset(1);
}
}
// switch the light of a diode
// write status on the LCD
void diodePulseToggle() {
//currentMillis = millis();
int ledPinNr = ledPinNrsArray[x];
int ledState = shouldLightOn;
// eliminate blinking & catch change
if(ledState != ledStates[x]){
digitalWrite(ledPinNr, ledState);
//lcdRowReset(1);
ledStates[x] = ledState;
//previousMillis = currentMillis;
if ( ledPinNr == relayPin ) {
relayState = digitalRead(relayPin);
writeLCDMessage();
}
}
//delay(2000);
}
// set cursor below the initially written "Light status "
// and write status of the specific diode
void writeLCDMessage() {
lcd.setCursor(LCD_2_ROW_START_COL, 1);
/*for(int i = 0; i < String(pinColorsPL[x]).length(); i++){
char currLetter = pinColorsPL[x][i];
lcd.setCursor(LCD_2_ROW_START_COL + i, 1);
if (currLetter == "ł") {
lcd.write(byte(0));
} else if (currLetter == "ó") {
lcd.write(byte(1));
} else if (currLetter == "ż") {
lcd.write(byte(2));
} else {
lcd.print(pinColorsPL[x][2]);
}
}*/
writeLCDRelayState();
String stringToPrint = String("Przekaźnik: ");
stringToPrint += (relayState ? "za" : "wy") + String("łączony");
Serial.println(stringToPrint);
//lcd.print(stringToPrint);
}
// return a boolean saying whether to reset the 2nd row
// if a second has passed and the text is still visible on the LCD
/*bool shouldResetSecondRow(){
if (!ledStates[x] && lastLight == x) {
int millisDifference = currentMillis-previousMillis;
return millisDifference >= interval;
} else {
return false;
}
}*/
void writeLCDRelayState() {
// check if relay pin is on
if(relayState) {
lcd.print("za");
} else {
lcd.print("wy");
}
lcd.write(byte(1));
lcd.write(byte(0));
lcd.print("czony");
}
void lcdRowReset(int rowNr) {
lcd.setCursor(0, rowNr);
lcd.print(" ");
}
void createSpecialCharacters() {
// maximum is 0 - 7
// reset character to avoid problem
// with displaying letters
// after e.g. changing their index nr
lcd.createChar(0, resetCharacter);
lcd.createChar(0, a_);
//lcd.createChar(, resetCharacter);
//lcd.createChar(, c_);
//lcd.createChar(, resetCharacter);
//lcd.createChar(, e_);
lcd.createChar(1, resetCharacter);
lcd.createChar(1, l_);
//lcd.createChar(, resetCharacter);
//lcd.createChar(, n_);
lcd.createChar(2, resetCharacter);
lcd.createChar(2, o_);
//lcd.createChar(, resetCharacter);
//lcd.createChar(, s_);
lcd.createChar(4, resetCharacter);
lcd.createChar(4, z__);
//lcd.createChar(2, resetCharacter);
//lcd.createChar(2, z_);
lcd.createChar(3, resetCharacter);
lcd.createChar(3, heartSymbol);
//lcd.write(byte(0));
//lcd.write(byte(1));
//lcd.write(byte(2));
//lcd.write(byte(3));
//lcd.write(byte(4));
//lcd.write(byte(5));
//lcd.write(byte(6));
//lcd.write(byte(7));
}