Input a number with Keypad and storing a number to EEPROM

Hi there I don’t have very much experience in coding just a basic staff so I need help
I have a code that I’v got from this topic https://forum.arduino.cc/index.php?topic=407476.0 thanks…

but I need modification like:
First when user press “#” key he has to write the number which will be like a price for a product,
Second when user press “A” key the price/number has to be stored to EEPROM memory,
Third when user press “B” key the price/number has to be readet from EEPROM memory,

when I press “*” key its working but this is not a number from memory this is just a printed number to LCD,

my problem is when I press “B” key on LCD I get some strange characters

I need code to work with EEPROM

this is 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] = { //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);
}

Some important information would be: are you using the identical keypad the original program used? And did the original program work correctly with your keypad before you modified the program?

Paul

// user input array; 10 digits and nul character
  static char userinput[11];

userinput is a character array.

char number;

number is a single character.

number=(userinput, 0, sizeof(userinput));

This line certainly does not copy userinput into number if that is what you are trying to do.

EEPROM.write(0, number);
number = EEPROM.read(0);

You can not store a 10 character array in a one byte memory location of eeprom.

lcd.println(number);//print the stored number

The lcd library does not support .println(). You need to use cursor location commands to go to a new line.

I suggest that you

  1. make "number" a character array equivalent to userinput
  2. use eeprom.put() and eeprom.get() to store and retrieve the character array.
  3. manage the lcd correctly

ok thanks I took your suggestions … my goal is user to save/get 4 different prices trouh keypad…and now I have this code but its not working properly… I don’t know why?

#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[11];
static char priceArrey2[11];
static char priceArrey3[11];
static char priceArrey4[11];
// 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 numberPrice1;          //int Val za pretvoranje na od "char priceArry" vo "int number"
int numberPrice2;
int numberPrice3;
int numberPrice4;

char pressKey;
int passState = 0;
int productState = 0;
int secretButtonState = 5;

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;
        secretButtonState = 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;
        secretButtonState = 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;
        secretButtonState = 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;
        secretButtonState = 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) {
        numberPrice1 = atoi(priceArrey1);     // funkcioata atoi() pretvara od char to int
        EEPROM.put(0, numberPrice1);        //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) {
        numberPrice2 = atoi(priceArrey2);     // funkcioata atoi() pretvara od char to int
        EEPROM.put(1, numberPrice2);        //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)) {
        numberPrice3 = atoi(priceArrey3);     // funkcioata atoi() pretvara od char to int
        EEPROM.put(2, numberPrice3);        //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)) {
        numberPrice4 = atoi(priceArrey4);     // funkcioata atoi() pretvara od char to int
        EEPROM.put(3, numberPrice4);        //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;

    case 'B':           //Get number from memory and print to LCD
      numberPrice1 = EEPROM.get(0, numberPrice1);             //EEPROM.get (address, val) simnuva od memorijata pogolemi brojki od 255
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(F("Saved Number is:"));
      lcd.setCursor(0, 2);
      lcd.println(numberPrice1);//print the stored number
      lcd.setCursor(8, 2);
      lcd.println("OK");//print the stored number
      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
  numberPrice1 = EEPROM.get(0, numberPrice1);
  delay(500);
  numberPrice2 = EEPROM.get(1, numberPrice2);
  delay(500);
  numberPrice3 = EEPROM.get(2, numberPrice3);
  delay(500);
  numberPrice4 = EEPROM.get(3, numberPrice4);
  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(numberPrice1);
  //2
  lcd.setCursor(9, 0);
  lcd.print("P2");
  lcd.setCursor(12, 0);
  lcd.print(numberPrice2);
  //3
  lcd.setCursor(0, 2);
  lcd.print("P3");
  lcd.setCursor(3, 2);
  lcd.print(numberPrice3);
  //4
  lcd.setCursor(9, 2);
  lcd.print("P4");
  lcd.setCursor(12, 2);
  lcd.print(numberPrice4);
}
void stateKeypad() {      //menu password

  if (pressKey == password [passLenght]) {
    passLenght ++;

    if (passLenght == 3) {
      passState = 1;
      productState = 0;
      secretButtonState = 0;

      getSavedPrce();
      printSavedPrceLCD();           //Price List
      delay(1000);
    }
  }
}

now I have this code but its not working properly... I don't know why?

Please explain in detail what the code is actually doing, and what you want it to do.
What is it not doing correctly?

Have you developed the code in smaller pieces? Is the keypad input section working correctly by itself? Is the display working correctly? Is the eeprom store and retrieve working correctly by itself?

You are supposed to test code as you write it, and as you build the program from smaller pieces, when does it break?

I need the code to do this

  1. user enters some password
  2. LCD shows 4 product "price List" (gets four prices from EEPROM)
  3. user press key "1"
  4. on LCD: "please enter new password" and the user sets some new number for new "price1"
  5. user press key "A" and "price1" is saved to EEPROM and so on for "price2", "price3", "price 4"
    that is the idea :slight_smile:

btw. keypad is working correctly I tasted it before, LCD is working properly as well

when I save price 1, its ok, after I try to save "price2" it saves price2 corectley, but for price1 gives me some random number,
after that when I save "price3" it saves prise3 correctly but now for price1 and prise 2 shows random number,
after I save "price4" price 4 is ok, but price1, price2, price3 are some random numbers. WHY?
sorry for my bad English

int numberPrice1;          //int Val za pretvoranje na od "char priceArry" vo "int number"
int numberPrice2;
int numberPrice3;
int numberPrice4;
EEPROM.put(0, numberPrice1);
EEPROM.put(1, numberPrice2);
EEPROM.put(2, numberPrice3); 
EEPROM.put(3, numberPrice4);

You are storing and retrieving integers which are 2 bytes. Each eeprom cell can only hold one byte. When you increment the address by 1, you are overwriting data.

Manage your addresses with increments of two

Store and retrieve from address 0,2,4,6 instead of 1,2,3,4

@OP

Are you saying that--

1. When you press # on the Keypad, the following message appears on Top Line of LCD, and it allows you to enter 4-digit (0 to 9) price of goods:

Price? 4567

2. When you press A on the Keypad, the price figure 4567 would be saved into the internal EEPROM of UNO starting at location: 0x0010. After saving, the message Saved! will appear at the Bottom Line of LCD. The LCD shows:

Price? 4567
Saved!

3. When you press B on the Keypad, the stored prices] (4567) will be read back from EEPROM and will be shown on the last part of of the Bottom Line of LCD. The LCD shows:

Price? 4567
Saved! Got 4567

4. When you press * on the Keypad, what will happen? I have not understood. (May be LCD is cleared. The process repeats.)

5. Please, upload the following sketch in your UNO.

#include<Keypad.h>
#include<LiquidCrystal_I2C.h>
#include<EEPROM.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
bool f1 = false, f2 = false, f3 = false;
const byte rows = 4;   //four rows
const byte cols = 4;  //four columns
char priceArray[5] = "";      //to store four digit price + null-byte
char priceRead[5] = "";
int i = 0;

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 connection Pin = 4567
byte colPins[cols] = {8, 9, 10, 11};     //C1C2C3C4 connection Pin = 891011
Keypad myKeypad = Keypad(makeKeymap(keyMap), rowPins, colPins, rows, cols); //Library Function

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);   //cursor position DP0 Top Line
  lcd.clear();
}

void loop()
{
  char input = myKeypad.getKey();  //scan keypad
  if (input != 0x00)       //key pressed
  {
    if (f1 == true)
    {
      lcd.print(input);
      priceArray[i] = input;
      i++;
      if (i == 4)
      {
        EEPROM.put(0x0010, priceArray);
        lcd.setCursor(0, 1);
        lcd.print("Saved!");
        f2 = true;
        f1 = false;
      }
    }
    else
    {
      if (f2 == true)
      {
        if(input == 'B')
        {
          EEPROM.get(0x0010, priceRead);
          lcd.setCursor(7, 1);
          lcd.print("Got ");
          lcd.print(priceRead);
        }
      }
      else
      {
        if (f3 == true)
        {
          ;
        }
        else
        {
          if (input == '#')
          {
            lcd.print("Price?");
            f1 = true;
            lcd.setCursor(7, 0);   //DP7, TL
          }
        }
      }
    }
  }
}

6. Observe that LCD is clear.
7. Press #. LCD shows:
Price?

8. Press:4 5 6 7. LCD shows:
Price? 4567
Saved!

9. Press: B. LCD shows:
Price? 4567
Saved! Got 4567

Read the codes of the given sketch. Draw a flow chart for the sketch and then implement the function of A and *.

The Flow Chart (might require adjustments)
keppad-2x.png

keppad-2x.png

@tanjamaya

There is no need to try hijack another thread.
Continue to do that and your time in the forum will be a short one.

Please read the sticky posts at the top of forum sections.
They will help you keep out of trouble.

Bob.

Sorry gues if I did something wrong, I’m new to this stuff
@cattledog I took your advise about Store and retrieve from address 0,2,4,6 instead of 1,2,3,4
now is working but not not very well

@GolamMostafa Itry your program but its working just for one price not for 4 and its not what I need… I think you did not understanf me before very well so here is my better explanation (sorry for my bad englih)
my code should do this:

  1. user enters password “###”
  2. LCD shows 4 product “price List” (prices should be from EEPROM)
  3. user press key “1”
  4. on LCD: “please enter new price1” and the user sets some new number for “newPrice1”
  5. user press key “A” and “newPrice1” is saved to EEPROM after a small delay
    on LCD there is a “PriceList” again with 4 prices but with “newPrice1”
  6. user press key “2”
  7. on LCD: “please enter new price2” and the user sets some new number for new “newPrice2”
  8. user press key “A” again and “newPrice2” is saved to EEPROM after a small delay on LCD there is a “PriceList” again with 4 prices but with “newPrice2”
    and so on for price3 and price4
    that is the idea :slight_smile:
    But my code its not working correctly and I don’t know Why?
    Here is the code so far
#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;

    case 'B':           //Get number from memory and print to LCD
      newrPrice1 = EEPROM.get(0, newrPrice1);             //EEPROM.get (address, val) simnuva od memorijata pogolemi brojki od 255
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(F("Saved Number is:"));
      lcd.setCursor(0, 2);
      lcd.println(newrPrice1);//print the stored number
      lcd.setCursor(8, 2);
      lcd.println("OK");//print the stored number
      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);
    }
  }
}

tanjamaya:
@GolamMostafa Itry your program but its working just for one price not for 4 and its not what I need… I think you did not understanf me before very well so here is my better explanation (sorry for my bad englih)

I have not given a solution to your problem; rather, I have given you some example codes which deal with interactive Keypad and LCD.

@GolamMostafa
Thank you you'v been so kind, with the pictures and block diagram.
I get your code I understand it, but when I try to modified to save 4 prices ist not working for me.
My code its working for one price as well, but when I try to do it for 4 prices it has problems, and I don't now why?

tanjamaya:

  1. user enters password “###”
  2. LCD shows 4 product “price List” (prices should be from EEPROM)
  3. user press key “1”
  4. on LCD: “please enter new price1” and the user sets some new number for “newPrice1”
  5. user press key “A” and “newPrice1” is saved to EEPROM after a small delay
    on LCD there is a “PriceList” again with 4 prices but with “newPrice1”
  6. user press key “2”
  7. on LCD: “please enter new price2” and the user sets some new number for new “newPrice2”
  8. user press key “A” again and “newPrice2” is saved to EEPROM after a small delay on LCD there is a “PriceList” again with 4 prices but with “newPrice2”
    and so on for price3 and price4

Try this (my approach) only for Price1; this is the building block; expand it to include other prices. And then optimize/simply the codes using case-switch and subroutines.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Keypad.h>

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);

static char priceArray1[] = "123";
static char priceArray2[] = "567";
static char priceArray3[] = "101";
static char priceArray4[] = "203";

char pr1[5] = "";
char pr2[5] = "";
char pr3[5] = "";
char pr4[5] = "";

static int count1 = 0;
static int count2 = 0;
static int count3 = 0;
static int count4 = 0;

char newPrice1[4] = "";          //int Val za pretvoranje na od "char priceArry" vo "int number"
int newPrice2;
int newPrice3;
int newPrice4;

char pressKey;
int passState = 0;
int productState = 0;

char entryPW[5] = "";
bool f1 = false, f2 = false;
int i = 0, j = 0;

void setup()
{
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);   //cursor position DP0 Top Line
  lcd.clear();
  lcd.print("Enter Password..");
  lcd.setCursor(0, 1);
  //-- initialize EEPROM with 4 prices-----
  EEPROM.put(0x0010, priceArray1);
  EEPROM.put(0x0014, priceArray2);
  EEPROM.put(0x0018, priceArray3);
  EEPROM.put(0x001C, priceArray4);
}

void loop()
{
  pressKey = myKeypad.getKey();
  if (pressKey != NO_KEY) // if any key is pressed
  {
    //----checking the entry of correct password-----
    if (f2 == false)   
    {
      if (f1 == false) //password has not been checked before
      {
        if (pressKey == '#')
        {
          lcd.print(pressKey);
          entryPW[i++] = pressKey;
          if (i == 4)
          {
            int matchCode = strcmp(entryPW, "###");
            if (matchCode != 0) //password has matched
            {
              showPrice();
              f1 = true;    //LCD shows prices
            }
          }
        }
      }
      else
      {
        if (pressKey == '1')
        {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Pls new Pr1:");
          f1 = false;
          f2 = true;
        }
      }
    }
    else
    {
      lcd.print(pressKey);
      newPrice1[j] = pressKey;
      j++;
      if(j == 3)
      {
        EEPROM.put(0x0010, newPrice1);
        delay(500);
        showPrice();
      }
    }
  }
}

void showPrice()
{
  lcd.clear();
  //--------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Pr1:");
  EEPROM.get(0x0010, pr1); lcd.print(pr1);
  //---------------------------------------
  lcd.setCursor(8, 0);
  lcd.print("Pr2:");
  EEPROM.get(0x0014, pr2); lcd.print(pr2);
  //---------------------------------------
  lcd.setCursor(0, 1);
  lcd.print("Pr3:");
  EEPROM.get(0x0018, pr3); lcd.print(pr3);
  //---------------------------------------
  lcd.setCursor(8, 1);
  lcd.print("Pr4:");
  EEPROM.get(0x001C, pr4); lcd.print(pr4);
  //---------------------------------------
}

@GolamMostafa
Thank you. I used your approach and modified for my needs
Now I have 2 questions which I cant make it to work

  1. “showPrice” function works well on LCD (it writes the correct prices to LCD) but when I try to print them on Seralmonitor or to convert them to integer because I need them as integers for later, I get strange result for my “Array” and for my “integer”. I used “atoi” for converting to int, I try "atol’ as well for converting to long int but my problem is that the array number is longer than 3 characters
    for example on LCD I get correct numbers:
    pr1=999
    pr2=888
    pr3=777
    pr4=666

but on SerialMonitor I get:
array1:999
int1:999
array2:888999
int2:-28505
array3:777888999
int3:-23321
array4:666777888999
int4:10471

I undestand why my int is not ok but why my array in not ok I dont now…
like it ads one price to the second, than to a third and so on

  1. in “setNewPrice” instead of pressing
    ‘1’ I want to change it to be pressing “*1” or “A1”
    ‘2’ I want to change it to be pressing “*2” or “A2”
    ‘3’ I want to change it to be pressing “*3” or “A3”
    ‘4’ I want to change it to be pressing “*4” or “A4”
    because now when I what to set price which contains numbers 1,2,3 or 4 in it,for example like “154” or “243” or other it goes to “setNewPrice Mode” which is asking me to write new price which is not what I want
    here is my late’s version
#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[3] = "";
char price2[3] = "";
char price3[3] = "";
char price4[3] = "";

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

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

char pressKey;
int productState = 0;

char entryPW[5] = "";
int i = 0, j = 0;

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

}

void enterPassword() {
  i = 0;
  productState = 0;
  lcd.setCursor(0, 0);   //cursor position Top Line
  lcd.clear();
  lcd.print("Enter Password..");
  lcd.setCursor(0, 1);

}
void checkPassword() {
  if (pressKey == '#') {
    lcd.print(pressKey);
    entryPW[i++] = pressKey;
    if (i == 4) {
      int matchCode = strcmp(entryPW, "###");
      if (matchCode != 0) { //password has matched
        showPrice();
      
      }
    }
  }
}

void setNewPrice() {

  if (pressKey == '1') {      //here I need insted of pressing '1' to be pressing "*1"

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set new Price1:");
    lcd.setCursor(0, 1);
    productState = 1;
    j = -1;
  }
  if (pressKey == '2') {    //here I need insted of pressing '2' to be pressing "*2"

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set new Price2:");
    lcd.setCursor(0, 1);
    productState = 2;
    j = -1;
  }
  if (pressKey == '3') {    //here I need insted of pressing '3' to be pressing "*3"

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Pls new Price3:");
    lcd.setCursor(0, 1);
    productState = 3;
    j = -1;
  }
  if (pressKey == '4') {    //here I need insted of pressing '4' to be pressing "*4"

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set new Price4:");
    lcd.setCursor(0, 1);
    productState = 4;
    j = -1;
  }
  if (pressKey == '*') {
    enterPassword();
  }
}

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;
    }
  }
  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;
    }
  }
  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;
    }
  }
  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;
    }
  }
}
void loop() {

  pressKey = myKeypad.getKey();
  if (pressKey != NO_KEY) { // if any key is pressed

    checkPassword();      //---- checking the entry of correct password -----

    setNewPrice();

    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("--------------------");
  //---------------------------------------
}

about my previous post:
I managed to solve the 2nd problem about pressing “A1” or"A2" or “A3” or"A4" instead of just pressing ‘1’ or ‘2’ or ‘3’ or ‘4’ but I’m stuck for problem #1 about “showPrice” function and arrays I have no Idea how to solve it
can someone help me please :slight_smile:

@GolamMostafa any help any idea? :slight_smile: :slight_smile:
tanks

here is my late’s version

#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[3] = "";
char price2[3] = "";
char price3[3] = "";
char price4[3] = "";

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

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

char pressKey;
int productState = 0;
int passState = 0;
int ProductLenght = 0;
int saveLenght = 0;
char* P1 = "A1";
char* P2 = "A2";
char* P3 = "A3";
char* P4 = "A4";
char* backKey = "**";

char* password = "###"; //create a password
int passLenght = 0; //keypad passLenght

char entryPW[5] = "";
int i = 0, j = 0;

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

}

void enterPassword2() {
  i = 0;
  passState = 0;
  productState = 10;
  ProductLenght = 0;
  passLenght = 0;
  j = -1;
  lcd.setCursor(0, 0);   //cursor position Top Line
  lcd.clear();
  lcd.print("Enter Password..");
  lcd.setCursor(0, 1);

}
void checkPassword2() {
  if (pressKey == password [passLenght]) {
    passLenght ++;

    if (passLenght == 3) {
      passState = 1;
      showPrice();            //Price List
      delay(1000);
    }
  }

}

void setNewPrice2() {
  if ((passState == 1) && pressKey == P1 [ProductLenght]) {
    ProductLenght ++;

    if (ProductLenght == 2) {

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

    if (ProductLenght == 2) {

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

    if (ProductLenght == 2) {

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

    if (ProductLenght == 2) {

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

    if (ProductLenght == 2) {

      enterPassword2();

    }
  }
}

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("--------------------");
  //---------------------------------------
}

You do not leave space in the arrays for a null terminator which is required for working with atoi as a c-string. Change all the array sizing from 3 to 4. With the initialization that you have with "" there will abe a null terminator at the end of your three digits.

Note: I looked at the issue with priceN size and not specifically at newPriceN but they should be change as well is you are using any of the c-string functions which require the null termination.

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

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

Serial output with this change

array1:321
int1:321
array2:567
int2:567
array3:101
int3:101
array4:203
int4:203
----------------

@cattledog

I made this changes

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

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

and this is the result in SerialMonitor

array1:321
int1:321
array2:5671321
int2:-30311
array3:10125671321
int3:31641
array4:203310125671321
int4:-7271
--------------------

and this is the result in SerialMonitor

I only looked at the start up serial report after entering the password. What did you exactly do to see the result you post. I will look deeper into the program.

I think that I did it I solve it
the right solllution was

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 dont know why, but now its working

sereial monitor

array1:050
int1:50
array2:010
int2:10
array3:009
int3:9
array4:165
int4:165
--------------------

thanks you all

tanjamaya:
I think that I did it I solve it
the right solllution was

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 dont know why, but now its working

sereial monitor



array1:050
int1:50
array2:010
int2:10
array3:009
int3:9
array4:165
int4:165





thanks you all

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