Hi,
I have been searching the internet for two solid days now to get an idea of how to do menu options with an LCD. I have included a flow chart of basically what I am trying to accomplish. I am very new to programming, so I am learning as I go. I have also included the code that I have written so far and tried to make notations of what it does and what I am trying to do. So far I have one button doing sort of what I am after. Still a long way off though I am afraid.
The program eventually will basically choose an LED and some options for the LED that will light up a drum when struck through a midi input. Most of the midi stuff has been programmed by someone else and I a trying to interact a 16x2 LCD (4 bit) with it.
It will have five buttons. up, down, right, left and center (store). I don't think that I am on the right path for the buttons and accessing menu options, so if any of you has some advice I could sure use some help.
Thanks
Jeremy
HERE is a mostly completed schematic. Still have some tweaking to do on it.
/*
LCD portion of the code. Sets up the pins
and intro screen.
The circuit:
* LCD RS pin to digital pin 2
* LCD Enable pin to digital pin 3
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the 4 bit LCD library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
// constants won't change. They're used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
int switchPin1 =12; // switch1 is connected to pin 12
int switchPin2 =11; // switch2 is connected to pin 11
int switchPin3 =10; // switch3 is connected to pin 10
int switchPin4 =9; // switch4 is connected to pin 9
int switchPin5 =8; // switch5 is connected to pin 8
int led1Pin = 13;
int led2Pin = 8;
int led3Pin = 9;
int led4Pin = 10;
int led5Pin = 12;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int lightMode = 0; // What mode is the light in?
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup() {
//LCD SETUP
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("**MIDI NIGHTS**");
delay (1000);
lcd.setCursor(0, 4);
lcd.print("LED Menu Options");
delay (1000);
lcd.clear();
// resets cursor position
lcd.setCursor(0, 4);
lcd.print(" Use right/left buttons to select LED ");
delay (20);
// scroll 150 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 27; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(50);
}
lcd.clear(); //clear screen and reset cursor
lcd.setCursor(1, 9);
lcd.print("Select LED < >"); //Select desired LED
delay (4000); // delays on this screen then goes to led choices
//BUTTON SETUP
pinMode(switchPin1, INPUT); // Set the switch pin as input for menu right
pinMode(switchPin2, INPUT); // Set the switch pin as input for menu left
pinMode(switchPin3, INPUT); // Set the switch pin as input for menu up
pinMode(switchPin4, INPUT); // Set the switch pin as input for menu down
pinMode(switchPin5, INPUT); // Set the switch pin as input for menu store
pinMode(led1Pin, OUTPUT); // leds outputs that will eventually become the LEDs 1-33 (11 LED selections with three colors each)
pinMode(led2Pin, OUTPUT); // right now they each button press lights up the same light
pinMode(led3Pin, OUTPUT); // ultimate goal is to charlieplex with decoder and latches
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin1); // read the initial state
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop() {
val = digitalRead(switchPin1); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin1); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode == 0) { // if its off
lightMode = 1; // turn light on!
} else {
if (lightMode == 1) { //
lightMode = 2; //
} else {
if (lightMode == 2) { //
lightMode = 3; //
} else {
if (lightMode == 3) { //
lightMode = 4; //
} else {
if (lightMode == 4) { //
lightMode = 5; //
} else {
if (lightMode == 5) { //
lightMode = 0; //
}
}
}
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // LED on
digitalWrite(led1Pin, HIGH);
lcd.clear();
lcd.print("LED 1");
delay (100);
}
if (lightMode == 1) { // LED on
digitalWrite(led1Pin, HIGH); //turns on LED
lcd.clear(); // clears screen
lcd.print("LED 2"); // name of led
delay (100); // delay to keep the lCD from flashing so much while looping, not exactly what I am after
}
if (lightMode == 2) { // LED on
digitalWrite(led1Pin, HIGH);
lcd.clear();
lcd.print("LED 3");
delay (100);
}
if (lightMode == 3) { // LED on
digitalWrite(led1Pin, HIGH);
lcd.clear();
lcd.print("LED 4");
delay (100);
}
if (lightMode == 4) { // LED on
digitalWrite(led1Pin, HIGH);
lcd.clear();
lcd.print("LED 5");
delay (100);
}
if (lightMode == 5) { // LED on
digitalWrite(led1Pin, HIGH);
lcd.clear();
lcd.print("LED 6");
delay (100);
}
}