好的,谢谢 JSC,你一直很有帮助,我接受了你的建议,最后我的代码应该这样做:
1)用户输入密码“###”
2)LCD 显示 4 个产品“价格表”(价格应该来自 EEPROM)
3) 用户按下“1”键
4) LCD 上:“请输入新价格1”,用户为“newPrice1”设置一些新数字
5) 用户按下“A”键,“newPrice1”在一小段延迟后保存到 EEPROM在 LCD 上再次有一个“PriceList”,有 4 个价格,但有“newPrice1”
6)用户按下键“2”
7)在 LCD 上:“请输入新价格 2”,用户为新的“newPrice2”设置一些新数字
5)用户再次按下“A”键,“newPrice2”在 LCD 上稍作延迟后被保存到 EEPROM 中,再次出现一个“PriceList”,其中包含 4 个价格,但带有“newPrice2”,以此类推 price3 和 price4
就是这个想法: )
但是我的代码不能正常工作,我不知道为什么?
这是到目前为止的代码
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#include <Keypad.h> //include keypad library - first you must install library (library link in the video description)
const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;
char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {4, 5, 6, 7}; //pins of the keypad
byte colPins [cols] = {8, 9, 10, 11};
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);
// user input array; 10 digits and nul character 1234567890
static char priceArrey1[4];
static char priceArrey2[4];
static char priceArrey3[4];
static char priceArrey4[4];
// variable to remember where in array we will store digit
static int count1 = 0;
static int count2 = 0;
static int count3 = 0;
static int count4 = 0;
int newrPrice1; //int Val za pretvoranje na od "char priceArry" vo "int number"
int newrPrice2;
int newrPrice3;
int newrPrice4;
char pressKey;
int passState = 0;
int productState = 0;
char* password = "###"; //create a password
int passLenght = 0; //keypad passLenght
void setup() {
// Setup size of LCD 16 characters and 2 lines
lcd.begin(16, 2);
// Back light on
lcd.backlight();
}
void loop() {
Serial.begin(115200);
pressKey = myKeypad.getKey();
if (pressKey != NO_KEY) // if any key is pressed
{
lcd.print(pressKey);
}
switch (pressKey)
{
case NO_KEY:
// nothing to do if no key is pressed Blank Screen
break;
case '1':
if (passState = 1) {
productState = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Set New Price P1"));
// clear the current user input
lcd.setCursor(0, 2);
memset(priceArrey1, 0, sizeof(priceArrey1));
// reset the counter
count1 = 0;
}
break;
case '2':
if (passState = 1) {
productState = 2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Set New Price P2"));
// clear the current user input
lcd.setCursor(0, 2);
memset(priceArrey2, 0, sizeof(priceArrey2));
// reset the counter
count2 = 0;
}
break;
case '3':
if (passState = 1) {
productState = 3;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Set New Price P3"));
// clear the current user input
lcd.setCursor(0, 2);
memset(priceArrey3, 0, sizeof(priceArrey3));
// reset the counter
count3 = 0;
}
break;
case '4':
if (passState = 1) {
productState = 4;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Set New Price P4"));
// clear the current user input
lcd.setCursor(0, 2);
memset(priceArrey4, 0, sizeof(priceArrey4));
// reset the counter
count4 = 0;
}
break;
case 'A': //Store number to memory
if (passState == 1 && productState == 1) {
newrPrice1 = atoi(priceArrey1); // funkcioata atoi() pretvara od char to int
EEPROM.put(0, newrPrice1); //EEPROM.put (address, val) snima u memorijata pogolemi brojki od 255
delay(100); //it takes 3,3 ms to write a byte in an EEPROM memory)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved P1"));
}
else if (passState == 1 && productState == 2) {
newrPrice2 = atoi(priceArrey2); // funkcioata atoi() pretvara od char to int
EEPROM.put(3, newrPrice2); //EEPROM.put (address, val) snima u memorijata pogolemi brojki od 255
delay(100); //it takes 3,3 ms to write a byte in an EEPROM memory)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved P2"));
}
else if ((passState == 1) && (productState == 3)) {
newrPrice3 = atoi(priceArrey3); // funkcioata atoi() pretvara od char to int
EEPROM.put(6, newrPrice3); //EEPROM.put (address, val) snima u memorijata pogolemi brojki od 255
delay(100); //it takes 3,3 ms to write a byte in an EEPROM memory)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved P3"));
}
else if ((passState == 1) && (productState == 4)) {
newrPrice4 = atoi(priceArrey4); // funkcioata atoi() pretvara od char to int
EEPROM.put(9, newrPrice4); //EEPROM.put (address, val) snima u memorijata pogolemi brojki od 255
delay(100); //it takes 3,3 ms to write a byte in an EEPROM memory)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved P4"));
}
delay(1500);
getSavedPrce();
printSavedPrceLCD(); //Price List
delay(1000);
break;
default:
// if not 10 characters yet
if (count1 < 3)
{
// add key to priceArrey array and increment counter
priceArrey1[count1++] = pressKey;
} else if (count2 < 3)
{
// add key to priceArrey array and increment counter
priceArrey2[count2++] = pressKey;
} else if (count3 < 3)
{
// add key to priceArrey array and increment counter
priceArrey3[count3++] = pressKey;
} else if (count4 < 3)
{
// add key to priceArrey array and increment counter
priceArrey4[count4++] = pressKey;
}
break;
}
stateKeypad();
}
void getSavedPrce() { //Reads savedPrice from EEPROM
newrPrice1 = EEPROM.get(0, newrPrice1);
delay(500);
newrPrice2 = EEPROM.get(3, newrPrice2);
delay(500);
newrPrice3 = EEPROM.get(6, newrPrice3);
delay(500);
newrPrice4 = EEPROM.get(9, newrPrice4);
delay(500);
}
/// Shows Price List from keypad on LCD
void printSavedPrceLCD () { //"Price List keypad MODE" on lcd
lcd.clear(); //clears lcd sreen
//1
lcd.setCursor(0, 0);
lcd.print("P1");
lcd.setCursor(3, 0);
lcd.print(newrPrice1);
//2
lcd.setCursor(9, 0);
lcd.print("P2");
lcd.setCursor(12, 0);
lcd.print(newrPrice2);
//3
lcd.setCursor(0, 2);
lcd.print("P3");
lcd.setCursor(3, 2);
lcd.print(newrPrice3);
//4
lcd.setCursor(9, 2);
lcd.print("P4");
lcd.setCursor(12, 2);
lcd.print(newrPrice4);
}
void stateKeypad() { //menu password
if (pressKey == password [passLenght]) {
passLenght ++;
if (passLenght == 3) {
passState = 1;
productState = 0;
getSavedPrce();
printSavedPrceLCD(); //Price List
delay(1000);
}
}
}