Pages: [1]   Go Down
Author Topic: keypad interfacing  (Read 440 times)
0 Members and 1 Guest are viewing this topic.
pune
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im new to arduino group.
please can anyone help me in reading two digit integer no. from keypad. i hv searched a lot on it but all in vain. none of the codes are working.please please do help me . please.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 88
Posts: 3448
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please post the code that does not work (the complete sketch).  Don't forget to put the code inside code tags when you post it so that it does not get mangled.  It is much easier to help when there is something to go on.

Have you got any Serial.prints in your code that would give an idea of what is going on, such as input from the keypad ?  If so, outputs from then could give clues so please post them too.  Details of the keypad and any library code used would also be helpful.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

pune
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int i = 0;
volatile long int digitOne ;
volatile long int digitTwo ;
volatile long int num;
volatile long int digitThree = 0;

char digitOneChar;
char digitTwoChar;
char digitThreeChar;

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {11, 10, 9, 8}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
  Serial.println("press A to start");
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    switch (key){
      
    case 'A':
      
      digitOne = 0; digitTwo = 0; digitThree = 0 ;  
      Serial.println("-------------------------");
      Serial.println("enter the triggering angle, in 3 digits");
      Serial.println("-------------------------");
      Serial.println("                         ");
      getNumbers();
      break;
      
    
      
     case 'B':
      
           Serial.println("-------------------------");
           Serial.println("the triggering angle confirmed, which is: ");
           Serial.println(num,BYTE);
           Serial.println("-------------------------");
           Serial.println("                         ");
           break;
          
    }  
  }
}

void getNumbers(){
  i = 0;
  char key = keypad.getKey();

  if (key != NO_KEY){
  while ( i <= 3){
        switch (i){
          case 1:
            digitOneChar = key; i++;
            break;
          case 2:
            digitTwoChar = key; i++;
            break;
          case 3:
            digitThreeChar = key; i++;
            break;
          
            
        }
  }
  }
      
        // convert the char to int
        digitOne = digitOneChar - 48;
        digitTwo = digitTwoChar - 48;
        digitThree = digitThreeChar - 48;
        
        //multiplying each digit so as to form a 3 digit number hundreds + tenths + ones
        digitOne = digitOne * 100;
        digitTwo = digitTwo * 10;
        
        // finding the final 3 digit number
        num = digitOne + digitTwo + digitThree;
        
        
           Serial.println("-------------------------");
           Serial.println("3 digits had been entered");
           Serial.println("press B to confirm, or A to enter again");
           Serial.println("-------------------------");
           Serial.println("                         ");
}
      
« Last Edit: January 14, 2013, 04:28:44 am by sameertandale » Logged

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

Quote
Don't forget to put the code inside code tags when you post it so that it does not get mangled.
Hey, you forgot something!
Logged

pune
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry . im new so it happened. il take care next time
Logged

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

Quote
il take care next time
Or fix it this time, if you expect anyone to read the code. You can modify posts you have made.
Logged

pune
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ya. I have posted it correctly in the code.please. can anyone post the correct code.
Logged

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

Code:
  if (key != NO_KEY){
  while ( i <= 3){
        switch (i){
          case 1:
            digitOneChar = key; i++;
            break;
          case 2:
            digitTwoChar = key; i++;
            break;
          case 3:
            digitThreeChar = key; i++;
            break;
        }
  }
If there is a key, execute the while loop, and assign the same key value to all three variables. You have the while loop in the wrong place.

Code:
void getNumbers()
{
  i = 0;
  while(i < 3)
  {
     // get a key
     // if there is a key,
     //   assign it the correct variable
  }

  // manipulate the variables to produce a number
}
Logged

Pages: [1]   Go Up
Jump to: