// Digital Relay Full Adder
// (c) Torsten Jaeger <www.spassantechnik.de> 2024
#include <Wire.h>
#include <SimpleKeypad.h>
#include <LiquidCrystal_I2C.h>
const int adder_input_a = A1;
const int adder_input_b = A2;
const int adder_carry_in = A3;
const int adder_sum = 10;
const int adder_carry_out = 11;
const byte nb_rows = 4; // four rows
const byte nb_cols = 4; // four columns
char key_chars[nb_rows][nb_cols] = { // The symbols of the keys
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[nb_rows] = {9, 8, 7, 6}; // The pins where the rows are connected
byte colPins[nb_cols] = {5, 4, 3, 2}; // The pins where the columns are connected
int potpin = 0; // analog pin used to connect the potentiometer
int potval; // variable to read the value from the analog pin
char cursorBlink = 0;
unsigned long previousMillis = 0;
byte adder_input_a_state = 0;
byte adder_input_b_state = 0;
byte adder_carry_in_state = 0;
byte adder_sum_state = 0;
byte adder_carry_out_state = 0;
byte current_input_row = 0;
byte current_input_column[2] = { 19, 19 };
char input_number[2][20] = { "", "" };
int calculated_summ = 0;
SimpleKeypad kp1((char*)key_chars, rowPins, colPins, nb_rows, nb_cols); // New keypad called kp1
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
lcd.init(); // initialize the lcd
Serial.begin(9600);
Serial.println("Press any key on the keypad and it will show up here :");
// Setup Welcome Screen
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("A: ");
lcd.setCursor(0,1);
lcd.print("B: ");
lcd.setCursor(0,2);
lcd.print("--------------------");
lcd.setCursor(0,3);
lcd.print("S: ");
// full adder
pinMode(adder_input_a, OUTPUT);
pinMode(adder_input_b, OUTPUT);
pinMode(adder_carry_in, OUTPUT);
pinMode(adder_sum, INPUT);
pinMode(adder_carry_out, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
potval = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
potval = map(potval, 0, 1023, 0, 255); // scale it to use it with the servo (value between 0 and 180)
//lcd.setCursor(0, 1);
//lcd.print("Poti: ");
//lcd.print(potval);
//lcd.print(" ");
adder_sum_state = digitalRead(adder_sum);
adder_carry_out_state = digitalRead(adder_carry_out);
// blinking cursor
if (currentMillis - previousMillis >= 350) {
previousMillis = currentMillis;
if (cursorBlink) {
lcd.setCursor(19, current_input_row);
lcd.print("#");
cursorBlink = 0;
} else {
lcd.setCursor(19, current_input_row);
lcd.print(input_number[current_input_row][19-current_input_column[current_input_row]-1]);
cursorBlink = 1;
}
}
//lcd.setCursor(0, 0);
//lcd.print("SUM: ");
//lcd.print(adder_sum_state);
//lcd.print(" / C-OUT: ");
//lcd.print(adder_carry_out_state);
char key = kp1.getKey(); // The getKey function scans the keypad every 10 ms and returns a key only one time, when you start pressing it
if (key) {
Serial.println(key);
//lcd.setCursor(0, 2);
//lcd.print("Key: ");
//lcd.print(key);
//lcd.setCursor(current_input_column, current_input_row);
//lcd.print(key);
//input_number[current_input_row][20-current_input_column] = key;
if (key == 'C') {
for (int i = 0; i < 20; i++) {
input_number[current_input_row][i] = {""};
}
lcd.setCursor(2, current_input_row);
lcd.print(" ");
current_input_column[current_input_row] = 19;
}
if (current_input_column[current_input_row] > 2 && key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '*' && key != '#') {
input_number[current_input_row][20-current_input_column[current_input_row]-1] = key;
current_input_column[current_input_row]--;
Serial.print("Number: ");
for (int i = 0; i < 19-current_input_column[current_input_row]+1; i++) {
Serial.print(input_number[current_input_row][i-1]);
//if (input_number[current_input_row][i-1] != "*") {
lcd.setCursor(current_input_column[current_input_row]+i, current_input_row);
lcd.print(input_number[current_input_row][i-1]);
//}
}
Serial.println("");
}
if (key == '#') {
if (current_input_row == 0) {
lcd.setCursor(19, current_input_row);
lcd.print(input_number[current_input_row][19-current_input_column[current_input_row]-1]);
current_input_row++;
}
}
if (key == 'A') {
if (current_input_row == 1) {
lcd.setCursor(19, current_input_row);
lcd.print(input_number[current_input_row][19-current_input_column[current_input_row]-1]);
current_input_row--;
}
}
if (key == 'B') {
if (current_input_row == 0) {
lcd.setCursor(19, current_input_row);
lcd.print(input_number[current_input_row][19-current_input_column[current_input_row]-1]);
current_input_row++;
}
}
//if (key == 'A') {
// if (adder_input_a_state) {
// analogWrite(adder_input_a, 0);
// adder_input_a_state = 0;
// } else {
// analogWrite(adder_input_a, 255);
// adder_input_a_state = 1;
// }
//}
//if (key == 'B') {
// if (adder_input_b_state) {
// analogWrite(adder_input_b, 0);
// adder_input_b_state = 0;
// } else {
// analogWrite(adder_input_b, 255);
// adder_input_b_state = 1;
// }
//}
//if (key == 'C') {
// if (adder_carry_in_state) {
// analogWrite(adder_carry_in, 0);
// adder_carry_in_state = 0;
// } else {
// analogWrite(adder_carry_in, 255);
// adder_carry_in_state = 1;
// }
//}
//delay(500);
//lcd.setCursor(0, 3);
//lcd.print("A: ");
//lcd.print(adder_input_a_state);
//lcd.print(" B: ");
//lcd.print(adder_input_b_state);
//lcd.print(" C-IN: ");
//lcd.print(adder_carry_in_state);
}
}