Pages: [1]   Go Down
Author Topic: [SOLVED] Keypad number input and store  (Read 2573 times)
0 Members and 1 Guest are viewing this topic.
Slovakia
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
I'm a beginner with Arduino programming and need some advice. My project is a bit bigger, but I'm stuck right now on this (quite simple?) problem.
What I need is read numbers that are typed on o 4x3 keypad, then store them in a variable and put on LCD display. The variables are three, and for each one the user can enter values. My code looks somtehing like this (the setup part is not shown):

Quote
void loop()
{
  // for 12V -----------------------------------------------------------
  char key1 = kpd.getKey();
  switch (key1) {
    case NO_KEY:
   break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
   lcd.print(key1);
        firstvariable = firstvariable* 10 + key1 - '0';
   break;

    case '#':
        Serial.println ();
   Serial.print ("Value for 12V is: ");
   Serial.print (firstvariable );
        break;

    case '*':
        firstvariable = 0;
        lcd.clear();
        Serial.println ();
   Serial.print ("Reset value:");
   Serial.print (firstvariable );
  }
  // for 12V end -----------------------------------------------------------
}


This is only for the first value. What I need is when the user press #, the firstvalue is stored, and the program continues on the input of the second value. After the second value, it continues with the input of the last (third) value.

I've tried doing some functions and calling it after the # button press, but with no luck. Can someone please give me a hint, how should it look like?
Thank you.
« Last Edit: April 05, 2011, 02:31:03 pm by Gudas » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to keep track of whether the keypad input is for the first variable, the second variable, or the third variable. The easiest way to do this is to put the code you have in a function that returns an int.

Code:
int GetNumber()
{
   int num = 0;
   char key1 = kpd.getKey();
   switch (key1)
   {
     case NO_KEY:
      break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      lcd.print(key1);
      num = num * 10 + (key1 - '0');
      break;

    case '#':
        return num;
        break;

    case '*':
        num = 0;
        lcd.clear();
   }
}

Then call the function as required:
Code:
void loop()
{
   v1 = GetNumber();
   // Do something with v1
   v2 = GetNumber();
   // Do something with v2
   v3 = GetNumber();
   // Do something with v3
}
Logged

Slovakia
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you.
I've tried that code, but I must doing something wrong (dunno what). To me, it looks like the program never 'waits' for the # button to pass the stored number out of the called function.
When I read the output in the function, it gives me only zero, whatever is pressed. And when the output is read in void loop, there is only a constatn flow of zeroes.
What I need to do is count the numbers in one variable until # is pressed, then the program should continue the same way for the second variable etc. After that the code will work with the inserted numbers...

Code:
#include <Keypad.h>
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

int v1 = 0;
int v2 = 0;
int v3 = 0;
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] = { 22, 23, 24, 25 };   
byte colPins[COLS] = { 26, 27, 28 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  lcd.init();                             
  lcd.commandWrite(0x0F);               
  lcd.clear();                             
  Serial.begin(9600);                     
}

void loop()
{
   v1 = GetNumber();
   //Serial.println ();
   //Serial.print (v1);
   v2 = GetNumber();
   v3 = GetNumber();

}


int GetNumber()
{
   int num = 0;
   char key = kpd.getKey();
   switch (key)
   {
     case NO_KEY:
      break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      lcd.print(key);
      num = num * 10 + (key - '0');
      break;

    case '#':
        //Serial.println ();
//Serial.print (num);
        return num;
        break;

    case '*':
        num = 0;
        lcd.clear();
   }
}
[/size]

Sorry if I ask dumb questions, and thanks for patience  smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry. There needs to be a while loop in the GetNumber function. Something like this:
Code:
int GetNumber()
{
   int num = 0;
   char key = kpd.getKey();
   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            lcd.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            num = 0;
            lcd.clear();
            break;
      }

      key = kpd.getKey();
   }

   return num;
}
Logged

Slovakia
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent! Now its working fine. 
Many thanks  smiley
Logged

Pages: [1]   Go Up
Jump to: