hello,
I would like some help. trying to make a code that collect digits from keypad, put it in array, and then print it in the serial monitor after "#" is pressed.
but then, I saw that my array doesn't save itself between cases.
may someone help me please?
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//main:
byte i = 0;
bool codeflag;
//keypad:
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//lcd:
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("power on");
delay(700);
lcd.clear();
}
void loop() {
char array1[5];
// put your main code here, to run repeatedly:
char key = keypad.getKey();
switch (key){
case ('#'):
lcd.clear();
codeflag = false;
for (int f=1; f<= i; f++){
Serial.print(array1[f]);
}
i = 0;
Serial.println();
break;
case ('*'):
codeflag =true;
break;
default:
if (key and codeflag== true){ //2 conditions: pressing key+ the first character is '*' (code flag is true).
lcd.setCursor(i,0);
array1[i] = key;
lcd.print(array1[i]);
Serial.print(array1[i]);
i++;
if (i>=3){
i = 3;
}
}
break;
}
}