Notifications
Clear all

Code Question

7 Posts
5 Users
0 Likes
808 Views
(@barefoot_engineer)
Member
Joined: 3 years ago
Posts: 2
Topic starter  

Hi! I'm trying to get a keypad to work with a 16x2 lcd display, using an Arduino Uno board. I programmed it so that when you press the "0" button on the keypad, it should display a series of dots. But I can't seem to get it to work. When I push "0" it doesn't do anything, but the IDE doesn't give me any errors.

I've pasted the code below. Thanks so much for any help you can give!

-- The Barefoot Engineer

 

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int ROW_NUM = 4;
const int COLUMN_NUM = 4;

char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {12, 11, 10, 9};
byte pin_column[COLUMN_NUM] = {8, 7, 6, 5};

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

void setup() {
// put your setup code here, to run once:
char key = keypad.getKey();
if (key == '0') {
lcd.setCursor(0, 1);
lcd.print("....................");
delay(5000);
lcd.clear();
}
}

void loop() {
// put your main code here, to run repeatedly:

}


   
Quote
 Foxy
(@foxy)
Member
Joined: 3 years ago
Posts: 56
 

I haven't used "Keypad.h" but made up my own code to read the keypad.

What catches my eye in a quick look at your code is that you've put the action in the setup.  The setup executes only once on startup and if you don't have the key pressed then, it will be missed forever after.  Try moving the action to the loop.

Ken


   
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

Try working out this project incrementally. Perhaps starting with the keypad, can you get keypad input and send it to the serial monitor? There are many LCD types. Did this come with your Arduino starter kit? Working with the LCD, try one of the LCD examples to test LCD output. Now try combining these components into your code. 

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
(@barefoot_engineer)
Member
Joined: 3 years ago
Posts: 2
Topic starter  

@foxy, thanks! I tried the action in the loop, but still no luck. I'll keep experimenting, but if you have suggestions feel free to let me know . . .


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @barefoot_engineer

@foxy, thanks! I tried the action in the loop, but still no luck. I'll keep experimenting, but if you have suggestions feel free to let me know . . .

Try embedding some print statements to see if you're actually receiving the values you expect, and also to check that any conditional statements are being satisfied - The following may help narrow down the problem you're experiencing:

char key = keypad.getKey();
  Serial.println(key);
  
if (key == '0') {
  Serial.println("if statement satisfied");

Cheers.


   
ReplyQuote
jker
 jker
(@jker)
Member
Joined: 3 years ago
Posts: 82
 

You have the getKey() function in your setup() function.  getKey() is non-blocking... which means it returns immediately whether or not a key is pressed. setup() runs right away on reset time.  So you are almost certainly missing the window where the arduino is looking for your keypress.

Everything in setup() should be in loop() instead, but also try printing to the Serial console instead of the LCD first... just to make sure that they keypad is working correctly.

 

"A resistor makes a lightbulb and a capacitor makes an explosion when connected wrong"
"There are two types of electrical engineers, those intentionally making antennas and those accidentally doing so."


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@jker

Posted by: @jker

You have the getKey() function in your setup() function.  getKey() is non-blocking... which means it returns immediately whether or not a key is pressed. setup() runs right away on reset time.  So you are almost certainly missing the window where the arduino is looking for your keypress.

Exactly one one of the reasons I always recommend placing print statements in strategic locations, is simply to double check that the values at that point in time are what you actually expect to see.  If they are not, then knowledge of that leads to further questions, and ultimately narrows down the root cause.

Good stuff!

Cheers.


   
ReplyQuote