Arduino.com
Arduino.com
#define ST_WAIT 1
#define ST_SETSPEED 2
#define ST_SETTIMING 3
#include <LiquidCrystal.h>;
#include <Keypad.h>;
int motorPin = 3;
byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of the keypad
void setup() {
pinMode(motorPin, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
}
void loop()
{
// single key from keypad
char key;
// text from keypad
char *text;
switch (currentState)
{
case ST_DISPLAY_MAINMENU:
// display main menu
displayMenu();
// switch to wait state
currentState = ST_WAIT;
break;
case ST_WAIT:
// get key
key = getKeyWithEcho();
// if speed setting selected
if (key == '1')
{
// display speed 'menu'
displaySpeedMenu();
// change to state where user can enter speed
currentState = ST_SETSPEED;
}
// if timing setting selected
if (key == '2')
{
// display 'timing' menu
displayTimingMenu();
// change to state where user can enter timing
currentState = ST_SETTIMING;
}
// Note: state does not change if entry is not '1' or '2'
break;
case ST_SETSPEED:
// get the text entered on the keypad
text = getKeypadText();
// if text complete
if (text != NULL)
{
// if user did enter a speed
if (text[0] != '\0')
{
theSpeed = atoi(text);
}
currentState = ST_DISPLAY_MAINMENU;
}
break;
case ST_SETTIMING:
// get the text entered on the keypad
text = getKeypadText();
// if text complete
if (text != NULL)
{
// if user did enter a speed
if (text[0] != '\0')
{
theTiming = atoi(text);
}
currentState = ST_DISPLAY_MAINMENU;
}
break;
} // end_of_switch
}
/*
display a title on the first line of the display; it always clears the LCD
*/
void displayTitle()
{
// clear the lcd
lcd.clear();
/*
display main menu
*/
void displayMenu()
{
// current line where to write on LCD; every time that we write, currentLine will be incremented
byte currentLine = 0;
// text buffer for 20 characters and string terminator
char textbuffer[21];
/*
display a 'menu' where the user can enter a speed
*/
void displaySpeedMenu()
{
// display the title on the 1st line
displayTitle();
// display additional info on 3rd line
lcd.setCursor(0, 2);
lcd.print("#Finish, *Cancel");
// prompt user to set speed (2nd line)
lcd.setCursor(0, 1);
lcd.print("Set speed: ");
}
/*
display a 'menu' where the user can enter a timing
*/
void displayTimingMenu()
{
/*
get a keystroke from the keypad
echo the key that was pressed to the LCD
the echo will be on the current position of the LCD
returns
the key that was pressed or NO_KEY
*/
char getKeyWithEcho()
{
// read a key
char key = keypad.getKey();
// if no key pressed
if (key != NO_KEY)
{
// for debugging, output to serial monitor
Serial.print("Key: "); Serial.println(key);
// display on current position of LCD
lcd.print(key);
}
return key;
}
/*
get a text from the keypad (max 20 characters)
'#' finishes the entry of data
'*' cancels the entry of data
returns
NULL if not complete
empty text if canceled
entered tet (might be empty)
*/
char *getKeypadText()
{
// a buffer for 20 keypresses and one
static char keypadbuffer[21];
// index in above buffer
static char index = 0;
// read a key
char key = getKeyWithEcho();
// if nothing pressed
if (key == NO_KEY)
{
// indicate no complete data
return NULL;
}
// if 'cancel' key
if (key == '*')
{
// reset index
index = 0;
// create empty text
keypadbuffer[index] = '\0';
// return text
return keypadbuffer;
}
// if 'enter' key
if (key == '#')
{
// add a string terminator
keypadbuffer[index] = '\0';
// reset index for next time
index = 0;
// return the text
return keypadbuffer;
}
// check for buffer overflow
if (index >= sizeof(keypadbuffer))
{
// add a string terminator
keypadbuffer[sizeof(keypadbuffer) - 1] = '\0';
// reset index for next time
index = 0;
// return the text
return keypadbuffer;
}