#include <Keypad.h>
#include <LiquidCrystal.h>
// Keypad configuration
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// LCD configuration
LiquidCrystal lcd(A2, A1,13, 12, 11, 10);
char msg1[] = " WELCOME "; //msg1
char msg2[] = "Select Wavelength(nm) A-254,B-302,C-365"; //msg2
char msg3[] = "Exposure Duration(Sec.):"; //msg3
char msg4[] = "Thank you"; //msg4
String input = ""; // to store the decimal number
String input1 = ""; // to store the input from the keypad
void showLetters(const char *message, int startPos) {
lcd.setCursor(0, 0);
for (int i = startPos; i < startPos + 16 && message[i] != '\0'; i++) {
lcd.print(message[i]);
}
delay(250);
}
char getRelaySelection() {
char input = '\0'; // to store the input from the keypad
// lcd.clear();
lcd.setCursor(0, 1); // move the cursor to the second row
lcd.print("Select Wav.:");
lcd.setCursor( 13,1); // move the cursor to the second row
while (true) {
char key = keypad.getKey();
if (key) {
if (key == 'D') {
// Enter key pressed
// lcd.clear();
lcd.setCursor(0,1);
lcd.print("Selected Wav:");
// lcd.setCursor(0, 1);
//lcd.print("Wav. ");
lcd.setCursor(13,1);
lcd.print(input);
delay(2000); // wait for 2 seconds to show the selected relay
return input; // return the selected relay value
}
else if (key == '#') {
// Backspace key pressed
if (input != '\0') {
input = '\0'; // clear the input
// lcd.clear();
//deleteCharAt(13,1);
lcd.setCursor(13,1);
// Print a space to "delete" the character
lcd.print(" ");
lcd.setCursor(0,1); // move the cursor to the second row
lcd.print("Select Wav.:");
lcd.setCursor(13,1); // move the cursor to the second row
}
}
else if (key == 'A' || key == 'B' || key == 'C') {
// Option key pressed
input = key;
lcd.setCursor(13,1);
lcd.print(input);
}
}
}
}
String getInputFromKeypad() {
input = ""; // Reset the input string
while (true) {
char key = keypad.getKey();
if (key) {
if ((key >= '0' && key <= '9') || key == '*') { // If the key is a number or decimal point
input += (key == '*' ? '.' : key); // Replace '*' with '.' for decimal point
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print(input); // Display the input
} else if (key == '#') { // If the key is backspace
if (input.length() > 0) {
input.remove(input.length() - 1); // Remove the last character
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second row
lcd.setCursor(0, 1);
lcd.print(input); // Display the updated input
}
} else if (key == 'D') { // If the key is enter
return input; // Return the input string
}
}
}
}
void controlRelay(char selection) {
// Turn off all relays initially
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
// Turn on the selected relay
switch (selection) {
case 'A':
digitalWrite(A3, HIGH);
//delay(2000); // keep the relay on for 2 seconds
// digitalWrite(A1, LOW);
break;
case 'B':
digitalWrite(A4, HIGH);
// delay(2000); // keep the relay on for 2 seconds
// digitalWrite(A2, LOW);
break;
case 'C':
digitalWrite(A5, HIGH);
// delay(2000); // keep the relay on for 2 seconds
// digitalWrite(A3, LOW);
break;
default:
// Do nothing if the selection is invalid
break;
}
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT); // Initialize relay pins
pinMode(A5, OUTPUT);
}
void loop() {
Serial.println("WELCOME");
lcd.clear();
lcd.setCursor(0, 0); // PRINT WELCOME
lcd.print(msg1);
delay(500);
char key_enter = keypad.getKey(); // Get the key pressed from the keypad
if (key_enter == 'D') {
lcd.clear(); // Clear the LCD screen
lcd.print("Hello, User"); // Print message to the LCD
delay(2000);
Serial.println("Select Wavelength(nm) A-254,B-302,C-365 ");
lcd.clear();
lcd.setCursor(0, 0);
for (int i = 0; i <= strlen(msg2) - 16; i++) {
showLetters(msg2, i); // Display for selecting the relay
}
char selectedRelay = getRelaySelection(); // Get the relay selection from the keypad
Serial.print("Selected Wav.:");
Serial.println(selectedRelay);
lcd.clear();
lcd.setCursor(0, 0);
for (int i = 0; i <= strlen(msg3) - 16; i++) {
showLetters(msg3, i); // Display for selecting the relay
}
delay(500);
Serial.println("Exposure Duration(Sec.):");
String number = getInputFromKeypad(); // Get the input from the keypad
delay(500);
lcd.setCursor(0, 1);
lcd.print(number); // Display the final number
lcd.print(" Seconds.");
Serial.print(number);
Serial.println(" Seconds.");
delay(1000); // Wait for 2 seconds
float delay_dur = number.toFloat();
controlRelay(selectedRelay); // Control the relay based on the selected value
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Wavelength ");
lcd.print(selectedRelay);
lcd.setCursor(0,1);
lcd.print("Exp.for ");
lcd.print(number);
lcd.print("Sec.");
delay(delay_dur * 1000);
digitalWrite(A5, LOW);
digitalWrite(A4, LOW); // Turn off all relays initially
digitalWrite(A3, LOW);
Serial.println(msg4);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(msg4);
delay(1500);
}
}