I'm currently writing some arduino code for a GUI with with Keypad and 16x2 LCD implementation. Part of my program requires a password to be entered before certain content on the Arduino can be accessed, however I can't seem to figure out how to get my saved password and given input to work properly.
String pswd = "0000";char* Input(int Length, byte clmn, byte row) { char output[Length];int i = 0;while (i < Length) {char KeyPress = keypad.getKey();lcd.setCursor(clmn,row);if (KeyPress == '0' || KeyPress == '1' || KeyPress == '2' || KeyPress == '3' || KeyPress == '4' || KeyPress == '5' || KeyPress == '6' || KeyPress == '7' || KeyPress == '8' ||KeyPress == '9') {output[i] = KeyPress;lcd.print(KeyPress);i++;clmn++; lcd.setCursor(i+1,0);lcd.cursor();} }delay(3000);Serial.println(output);return output;}bool Is_Psswrd() { bool Passed = false;char *Test;String test;CH2 = true; while (CH2) { say("Password: ",0,0);Test = Input(4, 10, 0);test = Test;if (test==pswd) {Passed = true;CH2 = false; }else {for(int i = 0; i < 3; i++) {lcd.clear();say("Incorrect ",0,0);delay(200); } } }return Passed;} void setup() {Is_Psswrd();}void loop() {}
I've tried alot of different things for saving, inputting, and checking characters, some more jankier than others. My original plan was to save all variables as char* variables and use the strcmp() function, but that didn't seem to work (strcmp() kept outputting "144") and I learned I needed const char* to make the function work properly. I've given the code I believe is needed to access the issue, but if you need the rest of the code I can paste it.
I am very new to C++. Most of my code is written in Java. Can someone please explain what needs to be done to get two char*/strings to compare in the way I want. I am open to fully rewriting my functions I just need to get this implemented.
Best Answer
If 4 character hard coded "password" is fine, String objects are too much overhead, IMO.Here's demo code with a 3x4 keypad and a 16x2 display via I2C:
#include <LCD_I2C.h>LCD_I2C lcd(0x27,16,2);#include <Keypad.h>const uint8_t ROWS = 4;const uint8_t COLS = 3;char keys[ROWS][COLS] = {{ '1', '2', '3' },{ '4', '5', '6' },{ '7', '8', '9' },{ '*', '0', '#' }};uint8_t colPins[COLS] = { 4, 3, 2 }; // Pins connected to C1, C2, C3uint8_t rowPins[ROWS] = { 8, 7, 6, 5 }; // Pins connected to R1, R2, R3, R4Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);const byte OFFS =4; // position on lcdbyte pos=0; // 0...3void setup() {lcd.begin();lcd.setCursor(0,1);lcd.print("PWD:");Serial.begin(9600);Serial.println("keypad with lcd");}char pwd[5]; // 4 digits + '\0'bool check (const char* pwd) {return (strcmp(pwd, "1234")==0);}void loop() {char key = keypad.getKey();if (key != NO_KEY) {// Serial.println(key);lcd.write(key);if (pos < 4) pwd[pos++] = key;if (pos == 4) {bool result = check(pwd);if (result == false) {pos = 0;lcd.setCursor(0,0);lcd.print("Wrong pwd");lcd.setCursor(0,1);lcd.print("PWD: ");lcd.setCursor(OFFS,1);} else if (pos == 4) {lcd.setCursor(0,0);lcd.print("Hooray ");lcd.setCursor(0,1);pos++; } // else continue writing to bottom line }}}