Saving variable using keypad

I am a beginner with Arduino

I make a code that save variable from keypad

The number I entered with the following code neither displayed on LCD nor saved to Arduino

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{{'1','2','3','.'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {14, 15, 16, 17}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //connect to the column pinouts of the keypad
int count=0;
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

byte index = 0;
char numbers[20]; // Plenty to store a representation of a float

float myvar;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(18, 4);
lcd.clear();
}

void loop()
{

lcd.setCursor(0,0);
lcd.print("myvar: ");
lcd.setCursor(0,2);
lolo5 = GetNumber();

}

int GetNumber()

{

char key = kpd.getKey();

if(key != NO_KEY)
{
if(key == 'A')
{
index = 0;
numbers[index] = '\0';
}
else if(key == '*')
{
numbers[index++] = '.';
numbers[index] = '\0';
}
else if(key >= '0' && key <= '9')
{
numbers[index++] = key;
numbers[index] = '\0';
}
else if(key == 'B')
{
float myvar = atof(numbers); // Do whatever you need to with len

index = 0;
numbers[index] = '\0';

}
}
}

The number I entered with the following code neither displayed on LCD nor saved to Arduino

You never print anything to the LCD apart from "myvar: ". Do you see that ?

Where do you expect the number to be saved?

Adding to that
You have a global and a local myvar. You also do not seem to return anything from GetNumber() so lolo5 will contain rubbish.

I am sorry
I copied the code and forget change the variable name

The code is:

void loop()
{

lcd.setCursor(0,0);
lcd.print("myvar: ");
lcd.setCursor(0,2);
myvar = GetNumber();

voidbony:
I am sorry
I copied the code and forget change the variable name

The code is:

void loop()
{

lcd.setCursor(0,0);
lcd.print("myvar: ");
lcd.setCursor(0,2);
myvar = GetNumber();

So where do you print myvar ?

UKHeliBob:
So where do you print myvar ?

So you mean to add print myvar:

The final code is:

#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>

//keypad

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,5);
#include <Keypad.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{{'1','2','3','.'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {14, 15, 16, 17}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //connect to the column pinouts of the keypad
int count=0;
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

byte index = 0;
char numbers[20]; // Plenty to store a representation of a float

float myvar;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(18, 4);
lcd.clear();
}

void loop()
{

lcd.setCursor(0,0);
lcd.print("myvar: ");
lcd.setCursor(0,2);
myvar = GetNumber();
lcd.print(myvar);

}

int GetNumber()

{

char key = kpd.getKey();

if(key != NO_KEY)
{
if(key == 'A')
{
index = 0;
numbers[index] = '\0';
}
else if(key == '*')
{
numbers[index++] = '.';
numbers[index] = '\0';
}
else if(key >= '0' && key <= '9')
{
numbers[index++] = key;
numbers[index] = '\0';
}
else if(key == 'B')
{
float myvar = atof(numbers); // Do whatever you need to with len

index = 0;
numbers[index] = '\0';

}
}
}

 {
         float myvar = atof(numbers);  // Do whatever you need to with len

Lose the "float" - this variable goes out of scope - use the global.

CtrlAltElite:

 {

float myvar = atof(numbers);  // Do whatever you need to with len




Lose the "float" - this variable goes out of scope - use the global.

I am sorry Sir, I did not get what do you mean

I replace this line in code with

myvar = atof(numbers);

[code]

but without change

Your function GetNumber doesn't return anything.
If it did, you promised it would be an int, but you seem to think it ought to be a float.

Which is it?

CtrlAltElite:
Your function GetNumber doesn't return anything.
If it did, you promised it would be an int, but you seem to think it ought to be a float.

Which is it?

Thanks Sir for your instructions, it is now working perfectly.

Another issue , If I need Arduino to save this variable value even if Arduino shutting down, how can I do that??

If I need Arduino to save this variable value even if Arduino shutting down, how can I do that??

Well, te easy bit is writing it to EEPROM.
The hard bit is knowing when the Arduino is shutting down,.

Perhaps write it to EEPROM any time it has stopped changing.

CtrlAltElite:
Well, te easy bit is writing it to EEPROM.
The hard bit is knowing when the Arduino is shutting down,.

Perhaps write it to EEPROM any time it has stopped changing.

I add the EEprom code but LCD dose not display the last value for (myvar) after restarting Arduino
Could you help?

//zzzzzzzzzzz

#include <EEPROM.h>
int address = 0;


#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>

//keypad


#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,5);
#include <Keypad.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = 
{{'1','2','3','.'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {14, 15, 16, 17}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //connect to the column pinouts of the keypad
int count=0;
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


byte index = 0;
char numbers[20]; // Plenty to store a representation of a float

float myvar;


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup()
{
 
  
  
  // set up the LCD's number of columns and rows:
  lcd.begin(18, 4);
  lcd.clear();
}



void loop()
{




  
 lcd.setCursor(0,0);
    lcd.print("myvar:     "); 
    lcd.setCursor(0,1);
    myvar = GetNumber();
    lcd.print(numbers);
lcd.setCursor(0,3);
 lcd.print(myvar);





EEPROM.update(address, myvar);

address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;

myvar = EEPROM.read(address);
address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;
  }

 

  }
delay(100);

}




float GetNumber()

{

    
    char key = kpd.getKey();


   if(key != NO_KEY)
   {
      if(key == 'C')
      {
         index = 0;
         numbers[index] = '\0';
      }
      else if(key == '*')
      {
         numbers[index++] = '.';
         numbers[index] = '\0';
      }
      else if(key >= '0' && key <= '9')
      {
         lcd.clear();
         numbers[index++] = key;
         numbers[index] = '\0';
      }


 else if(key == 'B')
      {
lcd.clear();
      }

     
      else if(key == '#')
      {
        myvar = atof(numbers);  // Do whatever you need to with len
        
         index = 0;
         numbers[index] = '\0';
        
      }
   }
    return myvar;
}

[code]

I add the EEprom code but LCD dose not display the last value for (myvar) after restarting Arduino

You do not read the initial value from EEPROM in setup().

To compound the problem you use EEPROM.update() to write the value to EEPROM but that function only writes a byte not a float. Look at the EEPROM.put() and EEPROM.get() functions to see how to save and load a float.

UKHeliBob:
You do not read the initial value from EEPROM in setup().

To compound the problem you use EEPROM.update() to write the value to EEPROM but that function only writes a byte not a float. Look at the EEPROM.put() and EEPROM.get() functions to see how to save and load a float.

I try this code but it dose not save myvar, Any suggestion??

//zzzzzzzzzzz

#include <EEPROM.h>

struct MyObject {
  float field1;
  byte field2;
  char name[10];
};



float myvar;













#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>

//keypad


#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,5);
#include <Keypad.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = 
{{'1','2','3','.'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}};
byte rowPins[ROWS] = {14, 15, 16, 17}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {18, 19, 20, 21}; //connect to the column pinouts of the keypad
int count=0;
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


byte index = 0;
char numbers[20]; // Plenty to store a representation of a float




// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup()
{
 
  
  
  // set up the LCD's number of columns and rows:
  lcd.begin(18, 4);
  lcd.clear();









  
  int eeAddress = 0;   //Location we want the data to be put.


  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, myvar);

  

 

  

 

EEPROM.get( eeAddress, myvar );
 
  eeAddress = sizeof(float); //Move address to the next byte after float 'f'.









 








  
}



void loop()
{




  
 lcd.setCursor(0,0);
    lcd.print("myvar:     "); 
    lcd.setCursor(0,1);
    myvar = GetNumber();
    lcd.print(numbers);
lcd.setCursor(0,3);
 lcd.print(myvar);




























}



 



float GetNumber()

{

    
    char key = kpd.getKey();


   if(key != NO_KEY)
   {
      if(key == 'C')
      {
         index = 0;
         numbers[index] = '\0';
      }
      else if(key == '*')
      {
         numbers[index++] = '.';
         numbers[index] = '\0';
      }
      else if(key >= '0' && key <= '9')
      {
         lcd.clear();
         numbers[index++] = key;
         numbers[index] = '\0';
      }


 else if(key == 'B')
      {
lcd.clear();
      }

     
      else if(key == '#')
      {
        myvar = atof(numbers);  // Do whatever you need to with len
        
         index = 0;
         numbers[index] = '\0';
        
      }
   }
    return myvar;
}

I try this code but it dose not save myvar,

Where in the code do you save it after you read it from the keypad ?

These 2 lines are in setup()

  EEPROM.put(eeAddress, myvar); //put the value of myvar (currently zero) in address zero
  EEPROM.get( eeAddress, myvar ); //reload myvar from EEPROM address zero

UKHeliBob:
Where in the code do you save it after you read it from the keypad ?

These 2 lines are in setup()

  EEPROM.put(eeAddress, myvar); //put the value of myvar (currently zero) in address zero

EEPROM.get( eeAddress, myvar ); //reload myvar from EEPROM address zero

Many thanks Sir , I got it. now it is working perfectly.

Should I use EEPROM.update to save memory life time?

Should I use EEPROM.update to save memory life time?

EEPROM.update() writes a byte, not a float, so no.

However, EEPROM.put() itself uses the update() mechanism so only updates what has changed.

UKHeliBob:
EEPROM.update() writes a byte, not a float, so no.

However, EEPROM.put() itself uses the update() mechanism so only updates what has changed.

Thank you so much for the help you gave me. I greatly appreciate the assistance you have provided me.

I tried to enter two variables to be saved in EEPROM but when I enter the second variable Arduino save this value for both values.
I am sure that there are some thing wrong in the code that I hope to find.

// Multi LCD
current_up = debounce(last_up, up);         //Debounce for Up button
current_down = debounce(last_down, down);   //Debounce for Down button
//----Page counter function to move pages----//
//Page Up
    if (last_up== LOW && current_up == HIGH){  //When up button is pressed
                     //When page is changed, lcd clear to print new page  
      if(page_counter <2){              //Page counter never higher than 3(total of pages)
      page_counter= page_counter +1;   //Page up
      }
      else{
      page_counter=2;  
      }
  }
    last_up = current_up;

//Page Down
    if (last_down== LOW && current_down == HIGH){ //When down button is pressed
                    //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter= page_counter -1;   //Page down
      }
      else{
      page_counter= 1;  
      }
  }
    last_down = current_down;
//------- Switch function to write and show what you want---// 
  switch (page_counter) {
case 1: {   
     lcd.setCursor(0,0);
    lcd.print("Enter lo set point:     "); 
    lcd.setCursor(0,2);
   lcd.print("New value:     ");
   lcd.setCursor(13,2);
    lolo5 = GetNumberr();
    lcd.print(numbers);
    lcd.setCursor(0,3);
  lcd.print("Current value:     ");
   lcd.setCursor(16,3);
  int eeAddresslo = 0;   //Location we want the data to be put.
    eeAddresslo = sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.get( eeAddresslo, lolo5 );
 lcd.print(lolo);
      }
    break;
case 2: {   
      lcd.setCursor(0,0);
    lcd.print("Enter hi set point:     "); 
    lcd.setCursor(0,2);
lcd.print("New value:     ");
   lcd.setCursor(13,2);
    hihi = GetNumber();
lcd.print(numbers);
lcd.setCursor(0,3);
  lcd.print("Current value:     ");
   lcd.setCursor(16,3);
int eeAddress = 0;   //Location we want the data to be put.
    eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
EEPROM.get( eeAddress, hihi );
  lcd.print(hihi);
    }
    break;
  }//switch end
float GetNumber()
{
    char key = kpd.getKey();
   if(key != NO_KEY)
   {
      if(key == 'C')
      {
         index = 0;
         numbers[index] = '\0';
      }
      else if(key == '*')
      {
         numbers[index++] = '.';
         numbers[index] = '\0';
      }
      else if(key >= '0' && key <= '9')
      {
         lcd.clear();
         numbers[index++] = key;
         numbers[index] = '\0';
      }

 else if(key == 'B')
     {
lcd.clear();
      }
      else if(key == '#')
      {
        hihi = atof(numbers);  // Do whatever you need to with len
         index = 0;
         numbers[index] = '\0';
int eeAddress = 0;   //Location we want the data to be put.
    eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
  EEPROM.put(eeAddress, hihi); 
      }
   }
    return hihi;
}
float GetNumberr()
{
    char key = kpd.getKey();
   if(key != NO_KEY)
   {
      if(key == 'C')
      {
         index = 0;
         numbers[index] = '\0';
      }
      else if(key == '*')
      {
         numbers[index++] = '.';
         numbers[index] = '\0';
      }
      else if(key >= '0' && key <= '9')
      {
        lcd.clear();
         numbers[index++] = key;
         numbers[index] = '\0';
      }
 else if(key == 'B')
      {
lcd.clear();
      }
      else if(key == '#')
      {
        lolo5 = atof(numbers);  // Do whatever you need to with len
             index = 0;
         numbers[index] = '\0';
  int eeAddresslo = 0;   //Location we want the data to be put.
 eeAddresslo = sizeof(float); //Move address to the next byte after float 'f'.
  EEPROM.put(eeAddresslo, lolo5);            
      }
   }
    return lolo5;
}

Because you're using the same address?