storing digits from keypad

how to store what i have pressed from keypad in variables as when i type 53 i want to store 53 in a variable

Start with mastering the Keypad library.
http://playground.arduino.cc/Code/Keypad
Review Arrays.
Together, you will be able to come up with a sketch that you want.
https://www.arduino.cc/en/Reference/Array
.

LarryD:
Start with mastering the Keypad library.
Arduino Playground - HomePage
Review Arrays.
Together, you will be able to come up with a sketch that you want.
https://www.arduino.cc/en/Reference/Array
.

thanks a lot :smiley:

To convert digits to a number, you multiply by 10 and then add.

Example: if the digits are 5 and 3, in that order, then you multiply 5 by 10, which gets you 50, and then you add 3, which gets you 53.

Another example: if the digits are 2, 1, 8, then you multiply 2 by 10 (getting 20), then add 1 (getting 21), then multiply by 10 (getting 210), and finally you add 8, and that gets you 218.

Note: Do not confuse the characters '0', '1', '2', etc. with the numeric values 0, 1, 2, etc.

The usual way to convert a character to its numeric value is to subtract '0'. Note the quotes around the '0'.

Example:

int x = '3'; // x will not be 3 (it will be 51)

int y = ('3' - '0'); // y will be 3

odometer:
To convert digits to a number, you multiply by 10 and then add.

Example: if the digits are 5 and 3, in that order, then you multiply 5 by 10, which gets you 50, and then you add 3, which gets you 53.

Another example: if the digits are 2, 1, 8, then you multiply 2 by 10 (getting 20), then add 1 (getting 21), then multiply by 10 (getting 210), and finally you add 8, and that gets you 218.

Note: Do not confuse the characters '0', '1', '2', etc. with the numeric values 0, 1, 2, etc.

The usual way to convert a character to its numeric value is to subtract '0'. Note the quotes around the '0'.

Example:

int x = '3'; // x will not be 3 (it will be 51)

int y = ('3' - '0'); // y will be 3

THANKS :smiley:

WHAT FUNCTION COULD STORE FROM KEYPAD WHAT I PRESSED AS WHEN I PRINT 5 I WANT TO STORE IT IN A VRAIABLE THEN IN THE SAME CODE WHEN I PRINT 7 I WANT TO STORE IT IN ANOTHER
VARIABLE

AS I WILL USE WHAT I HAVE STORED LATER

Post your code and don't use caps.

Using all capital letters is considered shouting.

Haven't you asked this in another thread?

Show your work since the last post.

.

ok :smiley:

first the user should enter the time for 3 times as i want the first time to be 5 and the second time be 9 and third be 3 then after i enter these numbers then they countdown as 5 will countdown then 9 then 3
but my code makes me enter the first time then it enters to the count down without typing the second time and third and it makes the time i entered for the first time for all
as when i enter 5 it counts down for the first time , the second time and the third time (same time for all )
thanks in advance :smiley:

#include <LiquidCrystal.h>
#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 3; 
char keys[ROWS][COLS] = 
{
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {31,33,35,37}; 
byte colPins[COLS] = {39,41,43}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(19,18,17,16,15,14);

 char Redtime[1];
 char Yellowtime[1];
 char Greentime[1];
 
 byte x;
 byte y;
 byte z;

 
void setup() {
  
  lcd.begin(16, 2);
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("Enter Redtime:");
  Redtime[0] = '0';
  lcd.setCursor(0,1);
  lcd.print(Redtime[0]);
 }

void loop() {
  
  
               
  char key = keypad.getKey();
  lcd.setCursor(0,0);
  
  if (key != NO_KEY ){
    if (key != '#'){
      Redtime[0] = key;
       x= Redtime[0]-'0';
        lcd.setCursor(0,1);
        lcd.print(Redtime[0]);
            }
            
          else{
       lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Yellowtime");
        Yellowtime[0] = '0';
        lcd.setCursor(0,1);
        lcd.print(Yellowtime[0]);
        }}
        
        if (key != NO_KEY ){
             if (key != '*'){
             Yellowtime[0] =key ;
              y= Yellowtime[0]-'0';
               lcd.setCursor(0,1);
               lcd.print(Yellowtime[0]);
                  }
                  
               else {
              lcd.clear();
               lcd.setCursor(0,0);
               lcd.print("Enter Greentime");
               Greentime[0] = '0';
               lcd.setCursor(0,1);
               lcd.print(Greentime[0]);
               
                 }}    
           
                 if (key != NO_KEY ){
                 if (key != '#'){
                   Greentime[0] = key;
                    lcd.setCursor(0,1);
                    lcd.print(Greentime[0]);
                    z= Greentime[0]-'0';
                    
                   }
                  
   if (x!=0){  
     lcd.clear();
    for(int i=x;i>-1;i--){ 
    lcd.setCursor(10,0);
    lcd.print(i);
 delay(1000); } }
 lcd.clear();
   
    if (y!=0){
      lcd.clear();
      for(int i=y;i>-1;i--){
  lcd.setCursor(10,0);
  lcd.print(i);
   delay(1000); }}
  lcd.clear();
      
        if (z!=0){
          lcd.clear();
          for(int i=z;i>-1;i--){
  lcd.setCursor(10,0);
  lcd.print(i);
 delay(1000);}}
 lcd.clear();

 
      
                
        
 
  }
   
              
                }

tammytam:
Post your code and don't use caps.

first the user should enter the time for 3 times as i want the first time to be 5 and the second time be 9 and third be 3 then after i enter these numbers then they countdown as 5 will countdown then 9 then 3

Would you please explain this better.

.

LarryD:
Would you please explain this better.

.

it is a traffic project first,you should enter for each light(green,yellow,red) time
as later i will need the time i entered for each light to count down

note: i deleted all led defintions from code so as to make code a little bit smaller

Here is the complete code if you want

#include <LiquidCrystal.h>
#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 3; 
char keys[ROWS][COLS] = 
{
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {31,33,35,37}; 
byte colPins[COLS] = {39,41,43}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(19,18,17,16,15,14);

const int ledpinG1 = 13;
 const int ledpinY1 = 12;
 const int ledpinR1 = 11;
 const int ledpinG2 = 9;
 const int ledpinR2 = 8;
 
 char Redtime[1];
 char Yellowtime[1];
 char Greentime[1];
 
 byte x;
 byte y;
 byte z;

 
void setup() {
  
  lcd.begin(16, 2);
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("Enter Redtime:");
  Redtime[0] = '0';
  lcd.setCursor(0,1);
  lcd.print(Redtime[0]);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
 }

void loop() {
  
  
               
  char key = keypad.getKey();
  lcd.setCursor(0,0);
  if (key != NO_KEY ){
  
    if (key != '#'){
      Redtime[0] = key;
       x= Redtime[0]-'0';
        lcd.setCursor(0,1);
        lcd.print(Redtime[0]);
        
            }
          else{
       lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Yellowtime");
        Yellowtime[0] = '0';
        lcd.setCursor(0,1);
        lcd.print(Yellowtime[0]);
        }}
        
        if (key != NO_KEY ){
             if (key != '*'){
             Yellowtime[0] =key ;
              y= Yellowtime[0]-'0';
               lcd.setCursor(0,1);
               lcd.print(Yellowtime[0]);
                  }
                  
            else 
              {lcd.clear();
               lcd.setCursor(0,0);
               lcd.print("Enter Greentime");
               Greentime[0] = '0';
               lcd.setCursor(0,1);
               lcd.print(Greentime[0]);
               
                 }}    
           
                 if (key != NO_KEY ){
                 if (key != '#'){
                   Greentime[0] = key;
                    lcd.setCursor(0,1);
                    lcd.print(Greentime[0]);
                    z= Greentime[0]-'0';
                    
                   }
                   lcd.clear();
   if (x!=0){    
  digitalWrite(13, HIGH); 
 digitalWrite(8, HIGH);  
 lcd.setCursor(0, 0);
 lcd.print("GREEN");
 lcd.setCursor(0, 1);
 lcd.print("RED");
  for(int i=x;i>-1;i--){
    lcd.setCursor(10,0);
    lcd.print(i);
 delay(1000);} } 
 lcd.clear();
   
    if (y!=0){
   digitalWrite(13, LOW);    
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("YELLOW");
  lcd.setCursor(0, 1);
  lcd.print("RED");
  for(int i=y;i>-1;i--){
  lcd.setCursor(10,0);
  lcd.print(i);
  delay(1000);}}
  lcd.clear();
      
        if (z!=0){
  digitalWrite(12, LOW);
  digitalWrite(8, LOW);  
  digitalWrite(11, HIGH);
  digitalWrite(9,HIGH);  
  lcd.setCursor(0, 0);
  lcd.print("RED");
  lcd.setCursor(0, 1);
  lcd.print("GREEN");
  for(int i=z;i>-1;i--){
  lcd.setCursor(10,0);
  lcd.print(i);
 delay(1000);}}
 lcd.clear();
digitalWrite(11, LOW);
digitalWrite(9, LOW); 
 
      
                
        
 
  }
   
              
                }

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Hey Nick, just a note, when you merged the threads I lost my response which I was in the middle of writing. Maybe this could be addressed in the new and improved forum software.

  • use CTRL T to format your code.
  • get into the habit of placing your opening braces { on a separate line
  • you should add useful comments to you code e.g. //this is a comment

You need to read the key presses in sequence:
Read the red time
then
Proceed to reading the yellow time
then
Proceed to reading the green time
After all times have been read proceed to the count down timing.

I would use a State Machine for this project.
See:

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
http://digitaldiy.io/articles/mcu-programming/general-programming/500-finite-state-machines#.VY88qc_bLcf

Sorry.

Yes, when the forum is improved no doubt that could be part of it.

LarryD:

  • use CTRL T to format your code.
  • get into the habit of placing your opening braces { on a separate line
  • you should add useful comments to you code e.g. //this is a comment

You need to read the key presses in sequence:
Read the red time
then
Proceed to reading the yellow time
then
Proceed to reading the green time
After all times have been read proceed to the count down timing.

I would use a State Machine for this project.
See:
Gammon Forum : Electronics : Microprocessors : State machines
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
http://digitaldiy.io/articles/mcu-programming/general-programming/500-finite-state-machines#.VY88qc_bLcf

thanks a lot i appreciate it :smiley:

Can you follow this example?

//State Machine example 
//LarryD

unsigned long TaskWait       = millis();
unsigned long DebounceWait   = millis();
unsigned long FlashWait      = millis();
unsigned long currentmillis  = millis(); 
unsigned long delay1Second   = 1000UL;
unsigned long debounceMillis = 100UL;
byte lastMySwitchState       = 1;
byte counter                 = 0;

//the following are enable flags
//some of these may not be used
boolean flag13    = true;
boolean flagStart = true;
boolean flagOne   = true;
boolean flagTwo   = true;
boolean flagThree = true;
boolean flagFour  = true;
boolean flagFive  = true;

const byte mySwitch = 2;

//define the available machine states for this sketch
//add more states as needed
enum States{
  stateStart, stateOne, stateTwo, stateThree, stateFour, stateFive};

//mState = machine State
States mState = stateStart;        //we start out in this machine state 

//**********************************************************************

void setup()
{
  Serial.begin(9600);

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);
  
  mState = stateStart;   //we start out in this machine state 
  flagStart = true;      //enable the state
  
} //  >>>>>>>>>>>>>> E N D   O F   s e t u p ( ) <<<<<<<<<<<<<<<<<


void loop()
{
  //leave this line of code at the top of loop()
  currentmillis = millis();

  //***************************
  //just some code to see if the sketch is blocking
  if (flag13 == true && CheckTime(FlashWait, 100UL, true))
  {
    //toggle the LED on pin 13
    digitalWrite(13,!digitalRead(13));

    //uncomment if you only want this to happen once
    //flag13 = false;
  }

  //***************************
  //is it time to check my switches?
  if (CheckTime(DebounceWait, debounceMillis, true))
  {
    debounceSwitches();      //check the switches 
  } 
  //***************************

  //***************************
  //Put other non-blocking stuff here
  //***************************

  //check the sketch machine state
  checkState();

} //   >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                       F U N C T I O N S
//======================================================================

//**********************************************************************
//Delay time expired function
//lastMillis = time we started, wait = delay in mS, retart = do we start again   
boolean CheckTime(unsigned long &lastMillis, unsigned long wait,boolean restart) 
{
  //is the time up for this task?
  if (currentmillis - lastMillis >= wait) 
  {
    //should this start again? 
    if(restart)
    {
      lastMillis += wait;  //get ready for the next iteration
    }

    return true;
  }

  return false;

} // END of CheckTime()

//**********************************************************************
//Check machine state and do things that must be happen accordingly
void checkState()
{
  switch (mState)
  {
    //***************************
  case stateStart:
    //let us wait for 1 seconds before we print the Welcome message
    //delay1Second is "type" unsigned long
    if (flagStart == true && CheckTime(TaskWait, delay1Second, false)) 
    {      
      //disable this state
      flagStart = false;
      
      Serial.println();
      Serial.println(F("Welcome"));
      Serial.println(F("Press a KEY then press SEND"));
      Serial.println();
      //Other start stuff goes here

      //we are now finished with this mState, advance to the next mState
      mState = stateOne; 
    }

    break; // END case stateStart:

    //***************************
  case stateOne: 
    if (Serial.available() > 0)
    {
      Serial.println(F("I am now in stateOne"));

      // read the incoming character:
      char incomingChar = Serial.read();

      // print what you received:
      Serial.print(F("I received: "));
      Serial.println(incomingChar);

      //we are now finished with this mState, advance to the next mState
      mState = stateTwo; 
    }

    break; // END case stateOne:

    //***************************
  case stateTwo:
    //Print the message immediately 
    Serial.println(F("I am now in stateTwo"));

    TaskWait = millis(); //initialize the next wait time
    //we are now finished with this mState, advance to the next mState
    mState = stateThree; 
    //enable the state
    flagThree = true;

    break; // END case stateTwo:

    //***************************
  case stateThree:
    //let us wait for 3 seconds before we print the message
    if (flagThree == true && CheckTime(TaskWait, 3000UL, false)) 
    {
      //Disabled this state
      flagThree = false;
      
      Serial.println(F("I am now in stateThree"));

      //we are now finished with this mState, advance to the next mState
      mState = stateFour; 
    }

    break; // END case stateThree:

    //***************************   
  case stateFour:
    //you put stateFour stuff here
    //nothing goes here in this example

    TaskWait = millis(); //initialize the next wait time
    //we are now finished with this mState, advance to the next mSstate
    mState = stateFive;  
    //enable the state
    flagFive = true;

    break; // END case stateFour:

    //***************************   
  case stateFive:
    //let us wait for 3 1/2 seconds before we print the message
    if (flagFive == true && CheckTime(TaskWait, 3500UL, false)) 
    {
      //Disabled this state
      flagFive = false;

      Serial.println(F("I was in stateFour for only a fraction of a second"));
      Serial.println(F("I am now in stateFive"));
      Serial.println(F("I am now going to stateStart"));

      TaskWait = millis(); //initialize the next wait time
      //we are now finished with this mState, let us go back to stateStart
      mState = stateStart; 
      //enable the state
      flagStart = true;
    }

    break; // END case stateFive:

    //***************************   
  default:
    // You put default stuff here

    //we are now finished with this mState, let us go to the next state
    //mState = state????;
    //enable the state
    //flag???? = true;

    break; //end of default
    //***************************  
    
  } // END of switch(mState)
  
} //END of checkState()

//**********************************************************************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void debounceSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;     

  //***************************    
  //check if this switch has changed state
  thisState = digitalRead(mySwitch);
  if (thisState != lastMySwitchState)
  {  
    //update the switch state
    lastMySwitchState = thisState;  

    //this switch has changed so do some stuff

    //HIGH condition code
    if(thisState == HIGH)         
    {
      //LED 12 is a ON/OFF
      digitalWrite(12,!digitalRead(12)); 
    }
    //LOW condition code
    else                          
    {
      Serial.println(++counter);  //example: display the current switch push count     
    }
    
  } // END of mySwitch code

  //***************************   

  //similar code for other switches goes here 

} // END of debounceSwitches()

//**********************************************************************

//======================================================================
//                       E N D   O F   C O D E
//======================================================================