Go Down

Topic: keypad and serial monitor help (Read 746 times) previous topic - next topic

Atak

Hello, I'm new here.  :)

Anyways, I am playing with a keypad on my Leonardo board.

If I run this code, it will output the numbers I press to the serial monitor....

Code: [Select]
#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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad



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



void setup(){

  Serial.begin(9600);

}

 

void loop(){

  char key = keypad.getKey();

 

  if (key){

  Serial.println(key);

  }
 
}



I was playing with this and wanted to also make the LED on the board blink when a number is pressed so I added...

Code: [Select]
#include <Keypad.h>

int led = 13;

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad



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



void setup(){

  Serial.begin(9600);
  pinMode(led, OUTPUT);

}

 

void loop(){

  char key = keypad.getKey();

 

  if (key){

  Serial.println(key);
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
  }
 
}


When I run this it will blink the LED on the board but it wont ouput the numbers to the serial monitor at the same time.
Why not?

PaulS

Code: [Select]
  char key = keypad.getKey();

 

  if (key)

First question. What's with all the blank lines?

Second question. Why are you treating key, which is a char variable, like a boolean? If key equals something makes sense. The value that getKey() returns if no key has been pressed is also true. So, this test is just about worthless. In fact, worse than useless since it causes a two second break before you can read another key press.

Nick Gammon

The keypad library I have returns NO_KEY if no key is read. NO_KEY is:

Code: [Select]

const char NO_KEY = '\0';


So that should, effectively, be "false" in a boolean test.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Atak

I copied and pasted this example from the Arduiono website, then added the led part.
I don't program.

So what exactly causes it to not be able to blink the led and not output the number pressed at the same time?

Nick Gammon

Quote
Anyways, I am playing with a keypad on my Leonardo board.


Try adding testing for serial to be available:

Code: [Select]

//Initialize serial and wait for port to open:
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up