Hello.
I am quite new to programming but I am reading and learning non stop, however I have hit a wall and I can't figure it out.
I have a keypad that in the end will be used to enter a target voltage level for a adjustable power supply, but I can't even understand how the following code works.
/* Numeric keypad and I2C LCD
http://tronixstuff.com/tutorials > chapter 42
Uses Keypad library for Arduino
http://www.arduino.cc/playground/Code/Keypad
by Mark Stanley, Alexander Brevig */
#include "Keypad.h"
#include "Wire.h" // for I2C LCD
#include "TWILiquidCrystal.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {
5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
9, 8, 7, 6}; //connect to the column pinouts of the keypad
int count=0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Keypad test!");
delay(1000);
lcd.clear();
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
Serial.print(key);
count++;
if (count==17)
{
lcd.clear();
count=0;
}
}
}
Lets focus on the loop, this code prints out on the lcd every key you hit in succession one after another.
And the following loop prints out the key that you press but when you press a new key it prints out at the same location overwriting the first:
void loop()
{
/* char key = keypad.getKey();
if (int(key) != 0)
{
lcd.setCursor(0,2);
lcd.print(key);
Serial.println(key);
}*/
Although I understand what the code says I don't follow how it all works out as it does, but my real problem is that I don't understand how I would do to be able to read each key press so that I can do something else with the information.
How to store each press in a global variable, or something like that?
I can't really ask for something specific seeing as I am totally lost.