0

Hi there I don't have very much experience in coding just a basic staff so I need help

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 read 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);
}
Kentaro Okuda
  • 1,557
  • 2
  • 12
  • 16
tanjamaya
  • 23
  • 1
  • 8

2 Answers2

0

Let's analyze your loop code:

  1. The beginning of the loop looks OK. The static variables userinput, countwill retain their value (important) and variables count and number will be zeroed with every new loop (OK).
  2. In case '#', you probably want to clear everything and prepare for receiving new number. Erase the number=(userinput, 0, sizeof(userinput)); line, because that's not how you set value of a variable and because you do not want to take "#" as input number. The new entered number is saved to your userinput array in default case .
  3. In case 'A', you will write variable into the EEPROM. The right (basic) way how to do it is (in your case) EEPROM.write(count, key). You should also put 4 ms delay after writing to EEPROM memory (it takes 3,3 ms to write a byte in an EEPROM memory). I also recommend to use function EEPROM.update(0)rather than EEPROM.write(0). Look here why. My other recommendation is to do EEPROM-writing operations after all 10 digits has been entered – you'll save some EEPROM write cycles in case user does not enter the number correctly (e.g. presses "#")...
  4. In case 'B', you want to read number from EEPROM. The correct implementation is

    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;
    

    I haven't tested this, it is more about the idea how to do it.

  5. In case '*', I am not sure what you want to do – it is not clear from your decription. I think you just want to print out numbers in your userinputarray. This can be done in similar way to case 'B', so:

    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;
    
  6. In case 'default', you are saving last entered number to the userinput array. This is OK.
JSC
  • 81
  • 5
  • Thank you I took your advise and now I have this problem... because I don't know how to post it here here is a link https://forum.arduino.cc/index.php?topic=633081.0 – tanjamaya Aug 27 '19 at 19:38
  • A link to whole discussion thread is not useful, I do not know what your problem is. Learn how to post it here... – JSC Aug 27 '19 at 20:59
0

Ok thanks JSC you'v been so helpfull I took your advice and in the end 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"
5) 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 :)
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;

    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
  • 23
  • 1
  • 8