#include <Wire.h>
#include <LiquidCrystal.h>
#include <MenuBackend.h>
#include <Keypad.h>
//keypad setup row and colums
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins [ROWS] = {6, 4, 3, 2};
byte colPins [COLS] = {10, 9, 8, 7};
Keypad keypad = Keypad(makeKeymap (keys), rowPins, colPins, ROWS, COLS);
//Create the menu, passing in an arguements the functions to call on a use event
MenuBackend menu = MenuBackend(menuUseEvent,menuChangeEvent);
//Menu Items
MenuItem Item1 = MenuItem ("Engine-ID");// set en togine number 3456 four places
MenuItem Item2 = MenuItem ("Direction");
MenuItem Item3 = MenuItem ("Speed MPH");
MenuItem Item4 = MenuItem ("MU");
MenuItem Item5 = MenuItem ("Sound");
MenuItem Item6 = MenuItem ("Lights");
MenuItem setDelay = MenuItem ("Delay,'D');
//Pins
const int keypad_press = 0;
const int keypad_read = 0;
const int keypad_bit0 = 0;
const int keypad_bit1 = 0;
const int keypad_bit2 = 0;
const int keypad_bit3 = 0;
//const int keypad_read = 1
//char key = 'N';
int buttonState = 0;
boolean isUserInputtingData = false;
// Variables to hold our settings
char engineID [5] = ("0000");
unsigned int engineSpeed = 0;
//PINS
//const int keypad_press = 0;
//const int keypad_bit0 = 0;
//set up LCD library,
LiquidCrystal lcd (12, 11, 35, 36, 37, 38);
void setup(){
// Initialize serial port
Serial.begin(9600);
while(!Serial)
}
lcd.begin(16,2);
lcd.print("tom's Train Control");
// setup inputs from keypad
{
//set up our menus
menu.getRoot().add(Item1);
Item1.addBefore(Item6);
Item1.addAfter(Item2);
Item2.addBefore(Item1);
Item2.addAfter(Item3);
Item3.addBefore(Item2);
Item3.addAfter(Item4);
Item4.addBefore(Item3);
Item4.addAfter(Item5);
Item5.addBefore(Item4);
Item5.addAfter(Item6);
Item6.addBefore(Item5);
Item6.addAfter(Item1);
}
boolean readKeypadAgain = true;
void loop()
{
//char key = getKeypad();
//if (key !=NO_KEY)
//Serial.print(key);
// First, handle input from keypad
buttonState = digitalRead(keypad_press);
if(buttonState == HIGH){
if(readKeypadAgain == true)
readKeypadAgain == false;
// Keypad = getKeypad()
char key = keypad.getKey();
if (key !=NO_KEY)
Serial.print(key);
}
char getKeypad() {
int keyRead = 0;
int bito = 0;
int bit1 = 0;
int bit2 = 0;
int bit3 = 0;
char button;
bit0 = digitalRead(keypad_bit0);
bit1 = digitalRead(keypad_bit1);
bit2 = digitalRead(keypad_bit2);
bit3 = digitalRead(keypad_bit3);
keyRead = bit0 +(bit110) + (bit2100) + (bit31000);
}
switch (keyRead){
case 0:
button = '1' ;
break;
case 1:
button = '2' ;
break;
case 10:
button = '3' ;
break;
case 11:
button = 'A' ;
break;
case 100:
button = '4' ;
break;
case 101:
button = '5' ;
break;
case 110:
button = '6' ;
break;
case 111:
button = 'B' ;
break;
case 1000:
button = '7' ;
break;
case 1001:
button = '8' ;
break;
case 1010:
button = '9' ;
break;
case 1011:
button = 'C' ;
break;
case 1100:
button = '' ;
break;
case 1101:
button = '0' ;
break;
case 1110:
button = '#' ;
break;
case 1111:
button = 'D' ;
break;
}
return (button);
}
else
{
readKeypadAgain = true;
keypad ='N';
}
void menuUseEvent(MenuUseEvent used)
{
isUserInputtingData = true;
}
else //if not, we're ptocessing up/down/enter
void menuChangeEvent(MenuChangeEvent changed)
}
//Output to LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print(changed.to.getName());
//Set cursor to start printing on next line
lcd.setCursor(0,1);
// Here's were we check what the current menu item is and display the associated value on the screen
if (Item1 == menu.getCurrent())
{
lcd.print(engineID);
}