#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
LiquidCrystal_I2C lcd2(0x26, 16, 2); // I2C address 0x27, 16 column and 2 rows
byte customChar[8] = {
0b01110,
0b11111,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111,
0b11111
};
byte customChar2[8] = { // sync up
0b01101,
0b10011,
0b10111,
0b10000,
0b00001,
0b11101,
0b11001,
0b10110
};
byte customChar3[8] = {
0b11100,
0b01101,
0b10101,
0b10001,
0b10001,
0b10101,
0b10110,
0b00111
};
//digital
const int pinNum13 = 13; // 7 segment
const int pinNum12 = 12; // 7 segment
const int pinNum11 = 11; // 7 segment
const int pinNum10 = 10; // 7 segment
const int pinNum8 = 8; // LE 7 segment
const int pinNum7 = 7; // LE 7 segment
const int pinNum6 = 6; // LE 7 segment
//analog
const int pinAnalogA1 = A1; // analog input
const int pinAnalogA3 = A3; // analog input display 1-3
int cntArr = 0; // counter for 7 digit per display
int arr7Keys[7]; // array for 7 digit per display
int analogValue; //
int btnScreenChacker = 0; // loop
String hexaKeys[] = {
// numbers 0-9
// bcd D-C-B-A table
"0000", // 0
"0001", // 1
"0010", // 2
"0011", // 3
"0100", // 4
"0101", // 5
"0110", // 6
"0111", // 7
"1000", // 8
"1001", // 9
"1010", // 10 blank
};
int bcdToPins(int val, int pin) { // convert number to bcd
// Serial.println(val);
for (int i = 0; i <= 4; i++) { // send 1 or 0 four times to the BCD digital pins and LE
String hexVal = hexaKeys[val];
/* if (i == 4) { // finish BCD and send LE
digitalWrite(pin, (LOW)); // LE sending
} else */
if (hexaKeys[val][i] == '1') { // send 1 to monitor
Serial.print(1);
digitalWrite((10 + i), HIGH);
} else if (hexaKeys[val][i] == '0') { // send 0 to monitor
Serial.print(0);
digitalWrite((10 + i), LOW);
} /* else { // error
Serial.print("error in bcdToPins value: ");
} */
}
Serial.print(" send to segment number ");
Serial.print(cntArr + 1);
Serial.print(" send LE to pin: ");
Serial.println(pin);
}
int analogToArr(int val) { // convert analog to num
// Analog Data
if (val < 15) { // get zero status
return 'A';
} else if (val > 30 && val < 60) {
return 3;
} else if (val > 83 && val < 113) {
return 2;
} else if (val > 145 && val < 175) {
return 1;
} else if (val > 231 && val < 261) {
return 'B';
} else if (val > 271 && val < 301) {
return 6;
} else if (val > 321 && val < 351) {
return 5;
} else if (val > 380 && val < 410) {
return 4;
} else if (val > 456 && val < 488) {
return 'C';
} else if (val > 493 && val < 527) {
return 9;
} else if (val > 542 && val < 572) {
return 8;
} else if (val > 597 && val < 627) {
return 7;
} else if (val > 668 && val < 698) {
return 'D';
} else if (val > 705 && val < 737) {
return '#';
} else if (val > 748 && val < 778) {
return 0;
} else if (val > 781 && val < 811) {
return '*';
} /* else { //error
Serial.print("error in analogToArr value: ");
Serial.println(val);
return -1;
} */
}
int analogToDisplay(int val) { // convert analog to num
// Analog Data
if (val > 900) {
return 0;
} else if (val > 600 && val <= 900) {
return 3;
} else if (val >= 300 && val <= 600) {
return 2;
} else if (val < 300) {
return 1;
} else { //error
Serial.print("error in analogToDisplay value: ");
Serial.println(val);
return -1;
}
}
void setup() { // initialize digital pin 13 as an output.
pinMode(pinNum13, OUTPUT); // bcd output to 7 segment
pinMode(pinNum12, OUTPUT); // bcd output to 7 segment
pinMode(pinNum11, OUTPUT); // bcd output to 7 segment
pinMode(pinNum10, OUTPUT); // bcd output to 7 segment
pinMode(pinNum8, OUTPUT); // LE to clock 1
pinMode(pinNum7, OUTPUT); // LE to clock 2
pinMode(pinNum6, OUTPUT); // LE to clock 3
digitalWrite(pinNum8, (HIGH)); // LE sending 1 on
digitalWrite(pinNum7, (HIGH)); // LE sending 1 on
digitalWrite(pinNum6, (HIGH)); // LE sending 1 on
Serial.begin(9600); // serial debug
lcd.init(); // initialize the lcd
lcd.backlight();
lcd2.init(); // initialize the lcd
lcd2.backlight();
}
// the loop function runs over and over again forever
void loop() {
//lcd
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("battery: 10%"); // print message at (0, 0)
lcd.createChar(0, customChar); // create a new custom character
lcd.createChar(1, customChar2); // create a new custom character
lcd.createChar(2, customChar3); // create a new custom character
lcd.setCursor(0, 1); // move cursor to (2, 0)
lcd.write((byte)0);
lcd2.clear(); // clear display
lcd2.setCursor(0, 0); // move cursor to (0, 0)
lcd2.print("battery: 20%"); // print message at (0, 0)
lcd2.createChar(0, customChar); // create a new custom character
lcd2.createChar(1, customChar2); // create a new custom character
lcd2.createChar(2, customChar3); // create a new custom character
lcd2.setCursor(0, 1); // move cursor to (2, 0)
lcd2.write((byte)0);
int chacker = 1;
while (chacker != 0) {
btnScreenChacker = analogToDisplay(analogRead(pinAnalogA3)); // GET data from the analog pin and convert to number
/* if */ while(btnScreenChacker != 0) { // select display
analogValue = analogRead(pinAnalogA1); // GET data from the analog pin
digitalWrite((btnScreenChacker + 5), (LOW)); // sending LE START transmiting
if (analogValue == 1023) { // blinking digit loop
for (int i = 0; i < 10; i++) {
if (analogValue == 1023) {
bcdToPins(8, (btnScreenChacker + 5)); // send 8
} else {
break;
}
delay(100);
}
for (int i = 0; i < 10; i++) {
if (analogValue == 1023) {
bcdToPins(10, btnScreenChacker + 5); // send blank
} else {
break;
}
// delay(100);
}
Serial.println("blinking loop activate");
// delay(1000);
}
if (analogValue != 1023) { // key pressed
int analogArr = analogToArr(analogValue); // converting analog to number
if (cntArr == 7) { // Finish dailing
cntArr = 0; // initialize array counter
arr7Keys[7] = {}; // initialize the array
Serial.print("finish dailing loop => display: ");
Serial.println(btnScreenChacker);
btnScreenChacker = 0 ;
// break;
} else if (cntArr < 7 && btnScreenChacker != 0) { // key BCD dailing up to 7 digits
arr7Keys[cntArr] = analogArr; // insert number to the array
bcdToPins(arr7Keys[cntArr], btnScreenChacker + 5); // send number to BCD and pin LE
digitalWrite(btnScreenChacker + 5, (HIGH)); // sending LE END transmiting
cntArr++;
}
}
delay(300); // delay for next keypad press
analogValue = 1023; // reset for the next number
} // end btnScreenChacker loop
}
Serial.println("END OF btnScreenChacker LOOP");
delay(100);
}
//loop