Pages: [1]   Go Down
Author Topic: keypad and serial monitor help  (Read 688 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm new here.  smiley

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:
#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:
#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?
Logged

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

Code:
  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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19026
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
const char NO_KEY = '\0';

So that should, effectively, be "false" in a boolean test.
Logged


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

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19026
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try adding testing for serial to be available:

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

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
Logged


Pages: [1]   Go Up
Jump to: