Pages: [1]   Go Down
Author Topic: Keypad - Multiple Value Read [resolved]  (Read 683 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good Morning, I'm trying to using keypad and then read a multiple value ... in the specific cases I'd like to press '*' blink 2 times the led and then read the second one value for example one ... and then put everything inside a variable and print it on serial ... due the fact Im not able to say to the keypad for the second charachter after blink 2 times ... wait at max un second if the user I press key read the keypad read a key otherwise go on ... below the code ... is there someone that can help me ? thanks Andrea

Code:
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  char customKey2;
  char testo[1];
  if (customKey != NO_KEY)
    {
    if (customKey = '*')
      {
       int a = 1;
       int cont = 3;
       asled(a,cont);
       customKey2 = customKeypad.getKey();
       a = 1;
       cont = 4;
       asled(a,cont);
       testo[0] = customKey;
       testo[1] = customKey2;
      }  
    Serial.println(testo);
    
    }
}

void asled(int a,int cont)
{
  for (a = 1; a < cont; a++)
    {
     digitalWrite(13, HIGH);
     delay(150);
     digitalWrite(13, LOW);
     delay(150);
    }
  
}
« Last Edit: April 18, 2012, 05:01:54 am by gnusso » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


to read multiple keys can imply that you need a keybuffer.
Add a timer (millis based) that checks the duration between keypresses and if too long wipe the buffer.

char testo[1];  is an array of length 1 so only index 0 is valid and you use index 1 too , will overwrite something in memory.

some code idea how to do the wiping and buffering
Code:

char buffer[10];
int index = 0;
unsigned long lastKey = 0;

void loop()
{
  // read a key
  char key = customKeypad.getKey();
  if (key != NO_KEY)
  {
    buffer[index++] = key;
    buffer[index] = '\0'; // keep string correctly terminated ;
    lastKey = millis();
  }

  // test wipe time
  if ((millis() - 2000 > lastKey) || index == 9) // after 2 seconds or buffer full
  {
    clearBuffer();
  }


  if (buffer[0] == '*' && strlen(buffer)>1)
  {
    switch(buffer[1])
    {
    case '0' :
      asled(10);
      break;
    case '1' :
      asled(20);
      break;
    default :
      asled(5);
      break;
    }
  }

  if (buffer[0] == '#' && strlen(buffer)>1)
  {
    switch(buffer[1])
    {
    case '0' : Serial.println("Hello ");
      break;
    case '1' : Serial.println("Goodbye ");
      break;
    default :
      asled(5);
      break;
    }
  }

  Serial.println(buffer);
}

void clearBuffer()
{
    index = 0;
    buffer[index] = '\0';
}

void asled(int cont)
{
  for (int a = 0; a < cont; a++)
    {
     digitalWrite(13, HIGH);
     delay(150);
     digitalWrite(13, LOW);
     delay(150);
    }
}

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao, it's a very good idea :-) I will try and then I'll let you know :-) ,

Perfect, it's work

I've added also end command :-) thanks


warms regards,
Andrea
« Last Edit: April 18, 2012, 05:03:36 am by gnusso » Logged

Pages: [1]   Go Up
Jump to: