Keypad State Machine

I'm working on a state machine which displays the letters A, B, and C in that order. If at any point while displaying the letters I press the 'A' key, I should be able to go into a manual control which allow the user to manual display the value 1 or 2. I'm using the keypad library found on the Arduino sketch. I'm able to choose to see the letters A,B, and C but can never go into manual mode which seeing the letters.

#include <SPI.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <TinyGPS++.h>
#include <nRF24L01.h>
#include "RF24.h"
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {A3, A2, A1, A0};
byte colPins[COLS] = {A7, A6, A5, A4};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(13, 12, 8, 9, 10, 11);
byte ledPin = 7;
const int gpsLED = 22;
const int compassLED = 23;
const int rfLED = 24;
const int autonomousLED = 25;
const int manualLED = 26;
int LeftJoyStickVertical = 2; // This pin will control Vertical motions
int LeftJoyStickSpin = 3; // This pin will control the Spin motions
int RightJoyStickBackForth = 4; // This pin will control Back and Forth motions
int RightJoyStickLeftRight = 5; // This pin will control Left and Right motions
boolean blink = false;
boolean ledPin_state;
boolean autonomous = false;
//gps
//compass
//course
//sonarDistance

static char keyPress = 0;
enum State{initialize,chooseMode,displayA,displayB,displayC,manualMode,display1,display2}state;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
pinMode(gpsLED, OUTPUT); // Sets the digital pin as output.
digitalWrite(gpsLED, HIGH); // Turn the LED on.
pinMode(compassLED, OUTPUT); // Sets the digital pin as output.
digitalWrite(compassLED, HIGH); // Turn the LED on.
pinMode(rfLED, OUTPUT); // Sets the digital pin as output.
digitalWrite(rfLED, HIGH); // Turn the LED on.
pinMode(autonomousLED, OUTPUT); // Sets the digital pin as output.
digitalWrite(autonomousLED, HIGH); // Turn the LED on.
pinMode(manualLED, OUTPUT); // Sets the digital pin as output.
digitalWrite(manualLED, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
//keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
lcd.begin(16, 2);
customKeypad.setDebounceTime(20);
customKeypad.setHoldTime(50);
}

void loop()
{

switch(state)//Transitions**********************************************************************************************************
{
keyPress = customKeypad.getKey();
case initialize:
keyPress = customKeypad.getKey();
state = chooseMode;
break;

case chooseMode:
keyPress = customKeypad.getKey();
if(keyPress == 'C')
{
state = displayA;
}
else if(keyPress == 'A')
{
state = manualMode;
}
else
{
state = chooseMode;
}
break;

case displayA:
keyPress = customKeypad.getKey();
if(keyPress == 'A')
{
state = manualMode;
}
else
{
state = displayB;
}
break;

case displayB:
keyPress = customKeypad.getKey();
if(keyPress == 'A')
{
state = manualMode;
}
else
{
state = displayC;
}
break;

case displayC:
keyPress = customKeypad.getKey();
if(keyPress == 'A')
{
state = manualMode;
}
else
{
state = displayC;
}
break;

case manualMode:
keyPress = customKeypad.getKey();
if(keyPress == '1')
{
state = display1;
}
else if(keyPress == '2')
{
state = display2;
}
else
{
state = manualMode;
}
break;

case display1:
keyPress = customKeypad.getKey();
state = manualMode;
break;

case display2:
keyPress = customKeypad.getKey();
state = manualMode;
break;

default:
keyPress = customKeypad.getKey();
state = initialize;
break;
}

switch(state)//Actions**********************************************************************************************************
{
case initialize:
GetChar();
break;

case chooseMode:
GetChar();
chooseFunction();
break;

case displayA:
GetChar();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" A ");
delay(1500);
break;

case displayB:
GetChar();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" B ");
delay(1500);
break;

case displayC:
GetChar();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" C ");
delay(1500);
break;

case manualMode:
GetChar();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ManualModeAction");
break;

case display1:
GetChar();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("display1");
break;

case display2:
GetChar();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("display2");
break;
}
}
void GetChar() {
char key = customKeypad.getKey();

if (key != NO_KEY)
{
keyPress = customKeypad.getKey();
}
}
void chooseFunction()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press C for ");
lcd.setCursor(0, 1);
lcd.print("displayA,B,C");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press A for");
lcd.setCursor(0,1);
lcd.print("ManualMode");
delay(1500);
}
Topic archived. No new replies allowed.