code
#include <LiquidCrystal.h>
#define HASKEYPAD
#define LCD20X4
#define LCD16x2
#define ST_DISPLAY_MAINMENU 0
#define ST_WAIT 1
#define ST_SETSPEED 2
#define ST_SETTIMING 3
// options to control the motor
#define MOTOR_CONTINUE 0
#define MOTOR_START 1
#define MOTOR_FORCESTOP 2
int motorPin =3 ;
#include <LiquidCrystal.h>;
#ifdef HASKEYPAD
#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] = {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
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#else
#define NO_KEY 0
#endif
// initialize the library with the numbers of the interface pins
#ifdef LCD20X4
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
#else
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#endif
// the state of our application; start with main menu display
byte currentState = ST_DISPLAY_MAINMENU;
// speed and timing variables
// both are global so we can display them in the main menu
int theSpeed = 0;
long theTiming = 0;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
// set up the LCD's number of columns and rows:
#ifdef LCD20X4
lcd.begin(20, 4);
Serial.println("20x4 display");
#else
lcd.begin(16, 2);
Serial.println("16x2 display");
#endif
}
void loop()
{
// single key from keypad
char key;
// text from keypad
char *text;
// let the motor do what it was doing
runMotor(MOTOR_CONTINUE);
//Serial.print("Speed: "); Serial.println(theSpeed);
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);
runMotor(MOTOR_START);
}
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();
// print the project title on the first line
lcd.setCursor(0, 0);
lcd.print("Speed Controller");
}
/*
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 the title on the first line and update currentLine
displayTitle();
currentLine = 1;
// print the current settings on the second line
lcd.setCursor(0, currentLine++);
sprintf(textbuffer, "S = %d, T = %d", theSpeed, theTiming);
lcd.print(textbuffer);
#ifdef LCD20X4
// print the first menu option on the third line
lcd.setCursor(0, currentLine++);
lcd.print("1 Set speed");
// print the second menu option on the fourth line
lcd.setCursor(0, currentLine++);
lcd.print("2 Set time");
#endif
}
/*
display a 'menu' where the user can enter a speed
*/
void displaySpeedMenu()
{
// display the title on the 1st line
displayTitle();
#ifdef LCD20X4
// display additional info on 3rd line
lcd.setCursor(0, 2);
lcd.print("#Finish, *Cancel");
#endif
// 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()
{
// display the title on the 1st line
displayTitle();
#ifdef LCD20X4
// display additional info on 3rd line
lcd.setCursor(0, 2);
lcd.print("#Finish, *Cancel");
#endif
// prompt user to set speed (2nd line)
lcd.setCursor(0, 1);
lcd.print("Set time: ");
}