[英]Comparing Strings in Arduino C++?
I'm currently writing some arduino code for a GUI with with Keypad and 16x2 LCD implementation.我目前正在为带有键盘和 16x2 LCD 实现的 GUI 编写一些 arduino 代码。 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.我的程序的一部分需要输入密码才能访问 Arduino 上的某些内容,但是我似乎无法弄清楚如何获取我保存的密码并提供输入以正常工作。
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.我最初的计划是将所有变量保存为 char* 变量并使用 strcmp() function,但这似乎不起作用(strcmp() 一直输出“144”),我了解到我需要 const char* 来制作 function好好工作。 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.我已经给出了我认为访问该问题所需的代码,但如果您需要代码的 rest,我可以粘贴它。
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.我对 C++ 很陌生。我的大部分代码都是用 Java 编写的。有人可以解释一下需要做什么才能让两个 char*/strings 以我想要的方式进行比较。 I am open to fully rewriting my functions I just need to get this implemented.我愿意完全重写我的功能我只需要实现它。
If 4 character hard coded "password" is fine, String objects are too much overhead, IMO.如果 4 个字符的硬编码“密码”没问题,那么 String 对象的开销太大,IMO。 Here's demo code with a 3x4 keypad and a 16x2 display via I2C:这是通过 I2C 使用 3x4 键盘和 16x2 显示器的演示代码:
#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, C3
uint8_t rowPins[ROWS] = { 8, 7, 6, 5 };
// Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const byte OFFS =4; // position on lcd
byte pos=0; // 0...3
void 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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.