嗨,我在编码方面没有太多经验,所以我需要帮助
但我需要修改,例如: 首先,当用户按"#"键时,他必须写下数字,就像产品的价格一样, 其次,当用户按下"A"键时,价格/数字必须存储到EEPROM存储器中, 第三,当用户按"B"键时,必须从EEPROM存储器中读取价格/数字,
当我按"*"键时它工作,但这不是内存中的数字,这只是LCD的打印数字,
我的问题是当我在LCD上按"B"键时,我得到一些奇怪的字符
我需要代码来使用 EEPROM
这是我的代码:
#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);
void setup() {
// Setup size of LCD 16 characters and 2 lines
lcd.begin(16, 2);
// Back light on
lcd.backlight();
}
void loop()
{
// user input array; 10 digits and nul character
static char userinput[11];
// variable to remember where in array we will store digit
static int count = 0;
char number;
char key = myKeypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
}
switch (key)
{
case NO_KEY:
// nothing to do
break;
case '#':
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Press a number:"));
// clear the current user input
lcd.setCursor(0, 2);
memset(userinput, 0, sizeof(userinput));
number=(userinput, 0, sizeof(userinput));
// reset the counter
count = 0;
break;
case 'A': //Store number to memory
EEPROM.write(0, number);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved"));
break;
case 'B': //Get number from memory and print to LCD
number = EEPROM.read(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Saved Number is:"));
lcd.setCursor(0, 2);
lcd.println(number);//print the stored number
break;
case '*':
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Number:"));
lcd.setCursor(0, 2);
lcd.println(userinput);//print the stored number
break;
default:
// if not 10 characters yet
if (count < 10)
{
// add key to userinput array and increment counter
userinput[count++] = key;
}
break;
}
//delay(200);
}
让我们分析一下你的循环代码:
- 循环的开头看起来正常。静态变量
userinput, count
将保留其值(重要),变量count
和number
将在每次新循环(OK)时清零。 - 在
case '#'
中,您可能希望清除所有内容并准备接收新号码。擦除number=(userinput, 0, sizeof(userinput));
行,因为这不是您设置变量值的方式,并且您不想将"#"作为输入数字。新输入的数字将保存到您的userinput
数组中default
以备不时之需。 - 在
case 'A'
中,您将变量写入EEPROM。正确的(基本)方法是(在您的情况下)EEPROM.write(count, key)
.您还应该在写入 EEPROM 存储器后放置 4 毫秒的延迟(在 EEPROM 存储器中写入一个字节需要 3,3 毫秒)。我还建议使用函数EEPROM.update(0)
而不是EEPROM.write(0)
。看看这里为什么。我的另一个建议是在输入所有 10 位数字后执行 EEPROM 写入操作 – 如果用户未正确输入数字(例如按"#"),您将节省一些 EEPROM 写入周期...... -
在
case 'B'
中,您希望从EEPROM读取数字。正确的实现是lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("Saved Number is:")); for (int i = 0; i <= count; i++) { lcd.setCursor(i, 2); number = EEPROM.read(i); lcd.println(number);//print the stored number } break;
我还没有测试过这个,更多的是关于如何做到这一点的想法。
-
case '*'
,我不确定你想做什么——从你的描述中不清楚。我认为您只想在userinput
数组中打印出数字。这可以通过类似于case 'B'
的方式完成,因此:lcd.clear(); lcd.setCursor(0, 0); lcd.print(F("userinput no:")); for (int i = 0; i <= count; i++) { lcd.setCursor(i, 2); number = userinput[i]; lcd.println(number); } break;
- 在
case 'default'
中,您将上次输入的数字保存到userinput
数组中。这没关系。
好的,谢谢JSC,你很有帮助 我接受了你的建议,最后我的代码应该这样做:
1)用户输入密码"###">
2)LCD显示4产品"价格表"(价格应来自EEPROM)3)用户在LCD上按键"1"4):"请输入新价格1",用户为"newPrice1"设置一些新数字5)
用户按键"A"和"newPrice1">
在短暂延迟后保存到EEPROM 在LCD上,再次出现一个"价目表",有4个价格,但有"newPrice1">
6)用户按键"2" 7)在LCD上:"请输入新价格2",用户为新的"newPrice2"5设置了一些新号码)用户再次按键"A",在LCD上稍有延迟后,"newPrice2"将保存到EEPROM,再次出现"价目表">
,有4个价格,但带有"newPrice2" 以此类推,价格 3 和价格 4
这就是:)
的想法但是我的代码无法正常工作,我不知道为什么?
这是到目前为止
的代码
#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);
}
}
}