0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« on: January 19, 2010, 08:53:22 am » |
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); } }
|
|
|
|
« Last Edit: January 19, 2010, 08:57:58 am by jman31 »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #1 on: January 19, 2010, 10:10:14 am » |
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; // }
Can I introduce you to Mr Switch statement? http://arduino.cc/en/Reference/SwitchCaseor even Ms Modulo? http://arduino.cc/en/Reference/ModuloOne other minor thing: // 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;
As per the comment, does this mean you plan on dynamically moving these led and switch pins?
|
|
|
|
« Last Edit: January 19, 2010, 10:11:20 am by GrooveFlotilla »
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #2 on: January 19, 2010, 01:22:28 pm » |
Hi Groove,
I have looked over the switch/case tutorial and googled everything I can find on it and I am just not sure how to implement it. I am confused as to what all needs to be included. Everything I tried kicked back errors.
I am not sure what you mean by dynamically moving the led's and switch pins, but I don't think so. I guess they could all be constants. I think I just had some remnants left over from something else I tried which may lead to confusion with the variables. I will have three led's in each drum. Each a different color. I want to be able to select one of these three colors. I should have probably labeled it "Snare", "Tom" or "Cymbal" instead of LED1, LED2 and LED3 and so forth. The buttons will be there to access each drum light and pick one of three colors mounted in that drum.
Thanks for taking the time to look this over!
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #3 on: January 19, 2010, 01:33:00 pm » |
switch (lightMode) { case 0: lightMode = 1; break; case 1: lightMode = 2; break; case 2: lightMode = 3; break; case 3: lightMode = 4; break; case 4: lightMode = 5; break; case 5: lightMode = 0; break; } or even more succinct: lightMode = (lightMode + 1) % 6;
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #4 on: January 19, 2010, 02:58:06 pm » |
OK, I got it to actually do something. However, I am not sure how to implement the button, so it is not waiting for a button push. It is simply looping through the LED menu. I am assuming an "if" statement of some sort within each case? Here is the revised code. Thanks for your helpfulness. It means a lot!! /* 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);
// set pin numbers:
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 = 1;
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; // 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() {
switch (lightMode){ // Now do whatever the lightMode indicates case 0: lightMode = 1; digitalWrite(led1Pin, HIGH); lcd.clear(); lcd.print("LED 1"); delay (100); break;
case 1: lightMode = 2; 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 break;
case 2: lightMode = 3; digitalWrite(led1Pin, HIGH); lcd.clear(); lcd.print("LED 3"); delay (100); break; case 3: lightMode = 4; digitalWrite(led1Pin, HIGH); lcd.clear(); lcd.print("LED 4"); delay (100); break; case 4: lightMode = 5; digitalWrite(led1Pin, HIGH); lcd.clear(); lcd.print("LED 5"); delay (100); break; case 5: lightMode = 6; digitalWrite(led1Pin, HIGH); lcd.clear(); lcd.print("LED 6"); delay (100); case 6: lightMode = 0; break; } }
|
|
|
|
« Last Edit: January 19, 2010, 03:18:16 pm by jman31 »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #5 on: January 20, 2010, 08:31:20 am » |
Wow, that was brutal, but I finally got a switch case situation to work. I believe I can start actually coding something now! :  Here is the very basic code: // 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);
//first define states with obvious names and different values #define START 0 #define RED 1 #define BLUE 2 //next define which pin is which #define RED_PIN 9 #define BLUE_PIN 10
int buttonState = 0; int switchPin = 11; // switch is connected to pin 2 int val; // variable for reading the pin status int val2; int lightMode = 0; // What mode is the light in? int state = START; //create state variable and initialize it to START state
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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); } { delay (300); lcd.clear(); //clear screen and reset cursor lcd.setCursor(1, 9); lcd.print("Select LED < >"); //Select desired LED delay(2000); } pinMode(RED_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,LOW); //turn red led off pinMode(switchPin, INPUT); // Set the switch pin as input
Serial.begin(9600); // Set up serial communication at 9600bps
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop(){ buttonState = digitalRead(switchPin); if (buttonState == LOW){ } else { //FSM time switch(state){ case START: lcd.clear(); lcd.print("Snare"); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,LOW); //turn red led off delay(100); //wait 100ms state = RED; //transition to red state break; //end of START case case RED: lcd.clear(); lcd.print("TOM1"); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,HIGH); //turn red led on delay(200); //wait 200ms state = BLUE; //transition to blue state break; //end of RED case case BLUE: lcd.clear(); lcd.print("TOM2"); digitalWrite(RED_PIN,LOW); //turn red led off digitalWrite(BLUE_PIN,HIGH); //turn blue led on delay(200); //wait 200ms state = START; //transition to start state break; //end of BLUE case } } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: January 20, 2010, 09:00:00 am » |
You have this: if (buttonState == LOW){ } else {
Why not simply: if(buttonState == HIGH){
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #7 on: January 20, 2010, 09:18:20 am » |
Ha Ha, yep that works too. Thanks! 8-) I am now trying to nest a second switch/case into the first case "Start" and I a can't get any reaction from it. I think I have some of the {} in the wrong places. Any thoughts? #include <Button.h>
// 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);
//first define states with obvious names and different values #define START 0 #define RED 1 #define BLUE 2 #define START2 0 #define RED2 1 #define BLUE2 2 //next define which pin is which #define RED_PIN 9 #define BLUE_PIN 10
int buttonState = 0; int buttonState2 = 0; int switchPin = 11; // switch is connected to pin 11 int switchPin2 = 12; // switch is connected to pin 12
int state = START; //create state variable and initialize it to START state int state2 = START2; //create state variable and initialize it to START state
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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); } { delay (300); lcd.clear(); //clear screen and reset cursor lcd.setCursor(1, 9); lcd.print("Select LED < >"); //Select desired LED delay(2000); } pinMode(RED_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,LOW); //turn red led off pinMode(switchPin, INPUT); // Set the switch pin as input pinMode(switchPin2, INPUT); // Set the switch pin as input
Serial.begin(9600); // Set up serial communication at 9600bps
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void loop(){ buttonState = digitalRead(switchPin); buttonState2 = digitalRead(switchPin2);
if (buttonState == HIGH){ //FSM time switch(state){ case START: lcd.clear(); lcd.print("Snare"); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,LOW); //turn red led off delay(100); //wait 100ms if (buttonState2 == HIGH) switch(state2){ case START2: lcd.clear(); lcd.print("Color 1"); state2 = RED2; //transition to red state break; //end of START case case RED2: lcd.clear(); lcd.print("Color 2"); state2 = BLUE2; //transition to blue state break; //end of RED case case BLUE2: lcd.clear(); lcd.print("Color 3"); state2 = START2; //transition to start state break; //end of BLUE case } state = RED; //transition to red state break; //end of START case
case RED: lcd.clear(); lcd.print("TOM1"); digitalWrite(BLUE_PIN,LOW); //turn blue led off digitalWrite(RED_PIN,HIGH); //turn red led on delay(200); //wait 200ms state = BLUE; //transition to blue state break; //end of RED case case BLUE: lcd.clear(); lcd.print("TOM2"); digitalWrite(RED_PIN,LOW); //turn red led off digitalWrite(BLUE_PIN,HIGH); //turn blue led on delay(200); //wait 200ms state = START; //transition to start state break; //end of BLUE case } } }
|
|
|
|
« Last Edit: January 20, 2010, 09:18:52 am by jman31 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #8 on: January 20, 2010, 09:22:34 am » |
if (buttonState2 == HIGH) [glow]{ [/glow] switch(state2){ case START2: lcd.clear(); lcd.print("Color 1"); state2 = RED2; //transition to red state break; //end of START case case RED2: lcd.clear(); lcd.print("Color 2"); state2 = BLUE2; //transition to blue state break; //end of RED case case BLUE2: lcd.clear(); lcd.print("Color 3"); state2 = START2; //transition to start state break; //end of BLUE case } [glow]}[/glow]
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #9 on: January 20, 2010, 09:33:39 am » |
I tried that, but nothing changed. Any other ideas or possible errors in my code?
Thank you for looking at this for my PaulS!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: January 20, 2010, 10:14:50 am » |
The code, as you have it written, requires that you have both buttons pressed. Do you?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #11 on: January 20, 2010, 10:35:21 am » |
No, I wasn't. I guess I need to change "buttonState" back to low before I can use "buttonState2" by itself?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #12 on: January 20, 2010, 10:35:28 am » |
I tried that, but nothing changed That's because they're not required.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 55
Arduino rocks
|
 |
« Reply #13 on: January 20, 2010, 10:46:28 am » |
Is there another way I can write "buttonState" at the beginning of the loop so that I can bring it back to LOW during the second switch/case?
|
|
|
|
« Last Edit: January 20, 2010, 10:46:48 am by jman31 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: January 20, 2010, 11:05:13 am » |
What you could do is move the button 2 code after the end of the button 1 switch statement, and execute the code if state == RED.
|
|
|
|
|
Logged
|
|
|
|
|
|