hi I am having an error with my lcd i2c circuit and using arduino code can you please help me?
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
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] = { 11, 10, 9, 8 };
byte colPins[COLS] = { 7, 6, 5, 4 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int operation_count=0;
char selected_operation[4];
char questions [4][20]={
"555+10=?",
"36-10=?",
"4231x2=?",
"24/4=?"
};
char answers [4][5]={
"565",
"26",
"8462",
"6"
};
void setup() {
lcd.begin();
Serial .begin(9600); // or lcd.begin();
lcd.setCursor(0, 0);
lcd.print("press any key");
Serial.println("press any key");
lcd.setCursor(0,1);
lcd.print("to start");
Serial.println("to start");
delay(1000);
lcd.backlight();
}
void loop() {
char key = keypad.getKey();
char operation;
if (key){
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("A: Add B: Sub");
Serial.println("A: Add B: Sub");
lcd.setCursor(0, 1);
lcd.print("C: Multi D: Div");
Serial.println("C: Multi D: Div");
while (true){
char input=keypad.getKey();
if (input=='A' || input=='B' || input=='C' || input=='D'){
operation=input;
break;
}
}
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("enter 1st number");
Serial.println("enter 1st number");
lcd.setCursor(0, 1);
lcd.print("then press #");
Serial.println("then press #");
int num1 = readNumber();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("num1=");
Serial.println("num1=");
lcd.setCursor(5,0);
lcd.print(num1);
Serial.println(num1);
delay(2000);
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("enter 2nd number");
Serial.println("enter 2nd number");
lcd.setCursor(0, 1);
lcd.print("then press #");
Serial.println("then press #");
int num2 = readNumber();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("num2=");
Serial.println("num2=");
lcd.setCursor(5,0);
lcd.print(num2);
Serial.println(num2);
delay(2000);
lcd.setCursor(0, 0);
lcd.clear();
float result;
char operation_symbol;
switch(operation){
case 'A':
result=num1+num2;
operation_symbol='+';
break;
case 'B':
result=num1-num2;
operation_symbol='-';
break;
case 'C':
result=num1*num2;
operation_symbol='x';
break;
case 'D':
if (num2!=0){
result=num1/num2;
operation_symbol='/';
}
else {
lcd.print("Error: Division by zero");
Serial.println("Error: Division by zero");
delay(2000);
lcd.clear();
break; // Exit the switch
}
break;
default:
lcd.print("Invalid operation");
Serial.println("Invalid operation");
delay(2000);
lcd.clear();
break; // Exit the switch
}
lcd.setCursor(0,0);
lcd.print("Result:");
Serial.println("Result:");
lcd.print(num1);
Serial.println(num1);
lcd.print(operation_symbol);
Serial.println(operation_symbol);
lcd.print(num2);
Serial.println(num2);
lcd.setCursor(0,1);
lcd.print("=");
Serial.println("=");
lcd.setCursor(2,1);
lcd.print(result);
Serial.println(result);
delay(3000);
lcd.clear();
selected_operation[operation_count]=operation;
operation_count++;
if (operation_count>=4){
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, 0);
lcd.print("Q");
Serial.println("Q");
lcd.print(i + 1);
Serial.println(i + 1);
lcd.print(": ");
Serial.println(": ");
lcd.print(questions[i]);
Serial.println(questions[i]);
lcd.setCursor(0, 1);
lcd.print("Your answer: ");
Serial.println("Your answer: ");
char userAnswer[5];
int answerIndex = 0;
while (true) {
char input = keypad.getKey();
if (input >= '0' && input <= '9') {
userAnswer[answerIndex] = input;
lcd.print(input);
Serial.println(input);
answerIndex++;
} else if (input == '#') {
userAnswer[answerIndex] = '\0';
break;
}
}
if (strcmp(userAnswer,answers[i])==0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Correct!");
Serial.println("Correct!");
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect.");
Serial.println("Incorrect.");
}
delay(3000);
lcd.clear();
}
operation_count=0;
lcd.clear();
lcd.print("Press #");
Serial.println("Press #");
lcd.setCursor(0,1);
lcd.print("to try again");
Serial.println("to try again");
while (true){
char input=keypad.getKey();
if (input=='#'){
lcd.clear();
break;
}
}}
lcd.print("press any key");
Serial.println("press any key");
}
}
int readNumber() {
int num=0;
while (true){
char key=keypad.getKey();
if(key >= '0' && key <= '9'){
num = num * 10 + (key - '0');
}
else if (key=='#'){
break;
}
}
return num;
}
I am also using a keypad.So basicilly initially the lcd display is blank and not powered and when I try clicking on the buttons lcd gets on but does not display anything this is the error please help me.and thank you in advance