Input a number with Keypad and storing a number to EEPROM

tanjamaya:
Hi it’s me again… and I want to tell you that today I have the same problem with the serial monitor results

array1:100
int1:100
array2:20013100
int2:24620
array3:3002420013100
int3:-22484
array4:400333002420013100
int4:18476

the prices should be

array1:100
int1:100
array2:200
int2:200
array3:300
int3:300
array4:400
int4:400

but they are not… yesterday for a moment they wore OK… now they are wrong again and I am going crazy I don’t know what is the problem

We don’t know, either.

We can’t fix what we can’t see.

Please post your entire sketch. Please post the sketch exactly the way it is now.

yesterday for a moment they wore OK... now they are wrong again and I am going crazy I don't know what is the problem

You probably did not save the working sketch, or have loaded a previous version. Change control and version management take some attention to detail. I have never noticed a version number on any of your posted code.

cattledog:
Change control and version management take some attention to detail.

The OP is very probably a beginner at programming, and English is almost certainly not her first language.
English is my first language, and I like to think I am no longer a beginner at programming, and even so, I had to read that sentence more than once to figure out what you were talking about.

cattledog:
I have never noticed a version number on any of your posted code.

At this point, the lack of version numbers is the least of the OP's problems.

@odometer
ok… sorry about my language, English it’s not my first Language
and yes I’m not a professional programmer I’m sort of hobbies :slight_smile:

everything is working properly on LCD except “array numbers” in serial monitor
here is my late’s version of a my code

#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] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {4, 5, 6, 7}; //R1R2R3R4
byte colPins [cols] = {8, 9, 10, 11}; //C1C2C3C4

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);


char price1[5] = "";
char price2[5] = "";
char price3[5] = "";
char price4[5] = "";

char newPrice1[4] = "";
char newPrice2[4] = "";
char newPrice3[4] = "";
char newPrice4[4] = "";

int intPrice1 = 0;
int intPrice2 = 0;
int intPrice3 = 0;
int intPrice4 = 0;

char* password = "###"; //create a password
int passLenght = 0; //keypad passLenght
int passState = 0;
int ProductLenght = 0;
int productState = 0;
int saveLenght = 0;

char* P1 = "A1";
char* P2 = "A2";
char* P3 = "A3";
char* P4 = "A4";
char pressKey;
char* backKey = "**";
char* enterKey = "BB";
int j = 0;


void setup()
{
  Serial.begin(115200);
  lcd.begin(16, 2);
  lcd.backlight();
  myMenu();

  
}
void myMenu(){
  Serial.print("My Menu");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("My Menu");
  
  passState = 0;
  productState = 0;
  ProductLenght = 0;
  passLenght = 0;
  j = -1;
  }

void enterPassword2() {
  lcd.setCursor(0, 0);   //cursor position Top Line
  lcd.clear();
  lcd.print("Enter Password..");
  lcd.setCursor(0, 1);
  passState = 1;
  productState = 0;
  ProductLenght = 0;
  passLenght = 0;
  j = -1;
}
void checkPassword2() {
  if ((passState == 1) && pressKey == password [passLenght]) {
    passLenght ++;
    if (passLenght == 3) {
      passState = 2;
      showPrice();            //Price List
      delay(1000);
    }
  }

}

void setNewPrice2() {
  if ((passState == 2) && pressKey == P1 [ProductLenght]) {
    ProductLenght ++;
    if (ProductLenght == 2) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Set new Price1:");
      lcd.setCursor(0, 1);
      passState = 2;
      productState = 1;
      ProductLenght = 0;
      j = -1;
    }
  }
  if ((passState == 2) && pressKey == P2 [ProductLenght]) {
    ProductLenght ++;

    if (ProductLenght == 2) {

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Set new Price2:");
      lcd.setCursor(0, 1);
      passState = 2;
      productState = 2;
      ProductLenght = 0;
      j = -1;
    }
  }
  if ((passState == 2) && pressKey == P3 [ProductLenght]) {
    ProductLenght ++;

    if (ProductLenght == 2) {

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Pls new Price3:");
      lcd.setCursor(0, 1);
      passState = 2;
      productState = 3;
      ProductLenght = 0;
      j = -1;
    }
  }
  if ((passState == 2) && pressKey == P4 [ProductLenght]) {
    ProductLenght ++;

    if (ProductLenght == 2) {

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Set new Price4:");
      lcd.setCursor(0, 1);
      passState = 2;
      productState = 4;
      ProductLenght = 0;
      j = -1;
    }
  }
    
  if (pressKey == enterKey [ProductLenght]) {
    ProductLenght ++;
    if (ProductLenght == 2) {
      enterPassword2();
      //insertCoins();
    }
  }
  if ((passState == 2) && pressKey == backKey [ProductLenght]) {
    ProductLenght ++;
    if (ProductLenght == 2) {
      passState = 0;
      // enterPassword2();
      myMenu();
    }
  }
}

void saveNewPrice() {
  if (productState == 1) {
    lcd.print(pressKey);
    newPrice1[j] = pressKey;
    j++;
    if (j == 3)
    {
      EEPROM.put(0x0010, newPrice1);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 2) {
    lcd.print(pressKey);
    newPrice2[j] = pressKey;
    j++;
    if (j == 3)
    {
      EEPROM.put(0x0014, newPrice2);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 3) {
    lcd.print(pressKey);
    newPrice3[j] = pressKey;
    j++;
    if (j == 3)
    {
      EEPROM.put(0x0018, newPrice3);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 4) {
    lcd.print(pressKey);
    newPrice4[j] = pressKey;
    j++;
    if (j == 3)
    {
      EEPROM.put(0x001C, newPrice4);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
}
void loop() {
  pressKey = myKeypad.getKey();
  if (pressKey != NO_KEY) { // if any key is pressed
    checkPassword2();      //---- checking the entry of correct password -----
    setNewPrice2();
    saveNewPrice();
    
  }
}

void showPrice() {
  Serial.begin(115200);
  lcd.clear();
  //--------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Pr1:");
  EEPROM.get(0x0010, price1); lcd.print(price1);
  delay(100);
  lcd.setCursor(7, 0);
  lcd.print(" ");
  intPrice1 = atoi(price1);       //convert to integer becouse I need this later in the code as int number... this sometimes work sometimes don't I dont know why???
  Serial.print("array1:");
  Serial.println(price1);
  Serial.print("int1:");
  Serial.println(intPrice1);
  //---------------------------------------
  lcd.setCursor(8, 0);
  lcd.print("Pr2:");
  EEPROM.get(0x0014, price2); lcd.print(price2);
  delay(100);
  lcd.setCursor(15, 0);
  lcd.print(" ");
  intPrice2 = atoi(price2);
  Serial.print("array2:");
  Serial.println(price2);
  Serial.print("int2:");
  Serial.println(intPrice2);
  //---------------------------------------
  lcd.setCursor(0, 1);
  lcd.print("Pr3:");
  EEPROM.get(0x0018, price3); lcd.print(price3);
  delay(100);
  lcd.setCursor(7, 1);
  lcd.print(" ");
  intPrice3 = atoi(price3);
  Serial.print("array3:");
  Serial.println(price3);
  Serial.print("int3:");
  Serial.println(intPrice3);
  //---------------------------------------
  lcd.setCursor(8, 1);
  lcd.print("Pr4:");
  EEPROM.get(0x001C, price4); lcd.print(price4);
  delay(100);
  lcd.setCursor(15, 1);
  lcd.print(" ");
  intPrice4 = atol(price4);
  Serial.print("array4:");
  Serial.println(price4);
  Serial.print("int4:");
  Serial.println(intPrice4);
  Serial.println("--------------------");
  //---------------------------------------
}

serial monitor result from my code:
array1:100
int1:100
array2:20013100
int2:24620
array3:3002420013100
int3:-22484
array4:4003
int4:4003

as I say early it should be like this:
array1:100
int1:100
array2:200
int2:200
array3:300
int3:300
array4:400
int4:400

char price1[5] = "";
char price2[5] = "";
char price3[5] = "";
char price4[5] = "";

char newPrice1[4] = "";
char newPrice2[4] = "";
char newPrice3[4] = "";
char newPrice4[4] = "";

I have not looked at your code in detail, but why are these 2 sets of data different in length if they both hold the price ?

UKHeliBob:

char price1[5] = "";

char price2[5] = "";
char price3[5] = "";
char price4[5] = "";

char newPrice1[4] = "";
char newPrice2[4] = "";
char newPrice3[4] = "";
char newPrice4[4] = "";





I have not looked at your code in detail, but why are these 2 sets of data different in length if they both hold the price ?

I took the example of @GolamMostafa post #7 on the first page
if I change them all to be [5] then I have problem, when I try to save Price1, the price2 is empty in Serial Monitor and on LCD like this
array1:100
int1:100
array2:
int2:
array3:300
int3:300
array4:400
int4:400

after that when I save price2 its ok it prints all ok, but if I change price1 once again than price2 is empty again
for price3 and price4 its OK

If I change arrays all to be [4] then I get this
array1:100
int1:100
array2:2001100
int2:-30516
array3:30022001100
int3:25036
array4:400330022001100
int4:6604

In a previous reply I suggested all the price character array sizes should 4 to hold the 3 digits and the null terminator.

The EEPROM storage address need to be consistent with the variable sizing, because .put() and .get() will access the number of bytes of the variable.

Storage addresses in the code are in hex and are consistent with the length of 4.

0x0010 
0x0014
0x0018
0x001C

Your address scheme and variable sizing must be consistent.

cattledog:
In a previous reply I suggested all the price character array sizes should 4 to hold the 3 digits and the null terminator.

The EEPROM storage address need to be consistent with the variable sizing, because .put() and .get() will access the number of bytes of the variable.

Storage addresses in the code are in hex and are consistent with the length of 4.

0x0010 

0x0014
0x0018
0x001C




Your address scheme and variable sizing must be consistent.

Ok I did as you say I put everything with length of 4 (arrays and Storage addresses)
and I change everywhere j=-1 to j=0 and I solve the length of the number to be 3 characters,
This was what was causing the problem before ... its start to iterate from -1 not from 0
but now when I press "A1" in order to change price1 to value "000" it saves it like "100"
"A2" instead of "000" it saves it to "200" and so on .. so it start saving the number in to array too soon
here is a Serial Monitor result:
array1:100
int1:100
array2:200
int2:200
array3:300
int3:300
array4:400
int4:400

so how can I make the array to start saving numbers after I press "A1" or "A2" and so on..

The Null termination is not being handled properly with the keypad input.

EDIT: Leave j to start at 0 and not -1
With the array sizes at 4 and the addresses at 0x0010 0x0014 0x0018 0x001C you can change the saveNewPrice() function to force Null termination as follows

void saveNewPrice() {
  if (productState == 1) {
    lcd.print(pressKey);
    newPrice1[j] = pressKey;
    j++;
    newPrice1[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0010, newPrice1);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 2) {
    lcd.print(pressKey);
    newPrice2[j] = pressKey;
    j++;
    newPrice2[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0014, newPrice2);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 3) {
    lcd.print(pressKey);
    newPrice3[j] = pressKey;
    j++;
    newPrice3[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0018, newPrice3);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 4) {
    lcd.print(pressKey);
    newPrice4[j] = pressKey;
    j++;
    newPrice4[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x001C, newPrice4);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
}

cattledog:
The Null termination is not being handled properly with the keypad input.

EDIT: Leave j to start at 0 and not -1
With the array sizes at 4 and the addresses at 0x0010 0x0014 0x0018 0x001C you can change the saveNewPrice() function to force Null termination as follows

void saveNewPrice() {

if (productState == 1) {
    lcd.print(pressKey);
    newPrice1[j] = pressKey;
    j++;
    newPrice1[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0010, newPrice1);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 2) {
    lcd.print(pressKey);
    newPrice2[j] = pressKey;
    j++;
    newPrice2[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0014, newPrice2);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 3) {
    lcd.print(pressKey);
    newPrice3[j] = pressKey;
    j++;
    newPrice3[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x0018, newPrice3);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
  else if (productState == 4) {
    lcd.print(pressKey);
    newPrice4[j] = pressKey;
    j++;
    newPrice4[j] = '\0';//ensure null termination
    if (j == 3)
    {
      EEPROM.put(0x001C, newPrice4);
      delay(500);
      showPrice();
      j = 0;
      productState = 0;
      ProductLenght = 0;
    }
  }
}

@cattledog
that's it... Owww Thank you sooo much... that solve it :):):slight_smile: