Pages: 1 [2] 3   Go Down
Author Topic: LCD menu options (best route)  (Read 8883 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hope you guys aren't getting sick of me yet. I really appreciate your time and input. I am learning as I go and wearing my google button out! :smiley

Here is how I tried to implement your idea PaulS. I still get no entry into the second switch/case. I changed up some of the variable names to make more sense with the project. I hope that doesn't throw too much confusion into the mix.

If I can get this last issue, then maybe I can leave you guys alone for awhile! smiley-grin

Code:
void loop(){
  buttonState = digitalRead(switchPin);
  buttonState2 = digitalRead(switchPin2);

  if (buttonState == HIGH){
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      ledstate = LED2; //transition to red state    
      break; //end of START case              

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to blue state
      break; //end of RED case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to start state
      break; //end of BLUE case
    }

    {
      if (ledstate == LED2)
        if (buttonState2 == HIGH)
          if (buttonState == LOW){
            switch(colorState){
            case COLOR1:
              lcd.clear();
              lcd.print("Color 1");
              colorState = COLOR2; //transition to red state
              break; //end of START case
            case COLOR2:
              lcd.clear();
              lcd.print("Color 2");
              colorState = COLOR3; //transition to blue state
              break; //end of RED case
            case COLOR3:
              lcd.clear();
              lcd.print("Color 3");
              colorState = COLOR1; //transition to start state
              break; //end of BLUE case
            }
          }

    }
  }
}
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
     if (ledstate == LED2)
        if (buttonState2 == HIGH)
          if (buttonState == LOW){

is the same as writing:
Code:
     if  ( (ledstate == LED2) &&
            (buttonState2 == HIGH) &&
            (buttonState == LOW)) {
Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, but will that change anything to make it work?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, but it may make it easier to read.
Logged

Per Arduino ad Astra

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK    smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it safe to assume that you pushed button 1, and then pushed button 2?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes and although you were correct in that, It is definitely not what I am after. I tried moving it outside of the case, but think it is still expecting a "high" on button one to make it function when ledstate == led2

I made some changes to the variable names as I mentioned.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You aren't debouncing the buttons, or toggling behavior only when the button state changes.

Your behavior is based on the button state being HIGH.

This is fine if you want something to happen while a button is pressed, like a doorbell ringing. It's not fine, if you want to implement a state machine, as you are (trying to).

You need to keep track of the previous state of the buttons, and only act when the state changes:

Code:
int previousButtonOneState = LOW;
int previousButtonTwoState = LOW;

void loop()
{
   int buttonOneState = digitalRead(buttonOnePin);
   int buttonTwoState = digitalRead(buttonTwoPin);

   if(buttonOneState == HIGH && previousButtonOneState == LOW)
   {
       // Button One was just pushed. Make the state changes...
   }
   previousButtonOneState = buttonOneState;

   if(buttonTwoState == HIGH && previousButtonTwoState == LOW)
   {
       // Button Two was just pushed. Now, we can rely on the state
   }
   previousButtonTwoState = buttonTwoState;
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for that. I think maybe I am biting off more than I can chew with my limited knowledge. I added the code you gave me before the switch statements, but I obviously didn't do it correctly.

Here is what I did. I deleted some of the code that didn't pertain to what we are talking about.

Code:
int previousButtonOneState;
int previousButtonTwoState;
int buttonOneState;
int buttonTwoState;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
void setup() {



  pinMode(RED_PIN, OUTPUT);      // Set the pin as output
  pinMode(BLUE_PIN, OUTPUT);    // Set the pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

  digitalWrite(BLUE_PIN,LOW);   //turn blue led off
  digitalWrite(RED_PIN,LOW);     //turn red led off

  Serial.begin(9600);           // Set up serial communication at 9600bps

  previousButtonOneState = LOW;
  previousButtonTwoState = LOW;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


void loop(){
  {
    if(buttonOneState == HIGH && previousButtonOneState == LOW){
      // Button One was just pushed. Make the state changes...
    }
    previousButtonOneState = buttonOneState;

    if(buttonTwoState == HIGH && previousButtonTwoState == LOW){
      // Button Two was just pushed. Now, we can rely on the state
    }
    previousButtonTwoState = buttonTwoState;
  }

    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      switch(colorState){
      case COLOR1:
        lcd.clear();
        lcd.print("Color 1");
        colorState = COLOR2; //transition to color2 state
        break; //end of color1 case
      case COLOR2:
        lcd.clear();
        lcd.print("Color 2");
        colorState = COLOR3; //transition to color3 state
        break; //end of color2 case
      case COLOR3:
        lcd.clear();
        lcd.print("Color 3");
        colorState = COLOR1; //transition back to color1 state
        break; //end of color3 case
      }
      ledstate = LED2; //transition to led2 state    
      break; //end of START case              

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of RED case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of BLUE case
    }
  }
}
« Last Edit: January 20, 2010, 04:08:30 pm by jman31 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At least, you're trying. Boy, are you trying. smiley-wink

The whole switch statement, where you change the state goes inside the {} where the comment says

// Button One was just pushed. Make the state changes...
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Man, I sure appreciate you hanging in there with me. I figure the only way for me to learn it since I live in a small town (no resources available) is to jump in and try to code something.  :-?

Ok, does the nested code go in the second {} or does it remain in line like this? I'm still missing something. :-/

Code:
void loop(){


   if(buttonOneState == HIGH && previousButtonOneState == LOW)
   {
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      switch(colorState){
      case COLOR1:
        lcd.clear();
        lcd.print("Color 1");
        colorState = COLOR2; //transition to color2 state
        break; //end of color1 case
      case COLOR2:
        lcd.clear();
        lcd.print("Color 2");
        colorState = COLOR3; //transition to color3 state
        break; //end of color2 case
      case COLOR3:
        lcd.clear();
        lcd.print("Color 3");
        colorState = COLOR1; //transition back to color1 state
        break; //end of color3 case
      }
      ledstate = LED2; //transition to led2 state    
      break; //end of START case              

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of RED case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of BLUE case
    }


   previousButtonOneState = buttonOneState;

   if(buttonTwoState == HIGH && previousButtonTwoState == LOW)
   {
       // Button Two was just pushed. Now, we can rely on the state
   }
   previousButtonTwoState = buttonTwoState;
}

« Last Edit: January 20, 2010, 04:24:58 pm by jman31 » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I got it. I am sure I will be back, but that will get me started for now. Thanks to everyone that lent me a helping hand!!!!!  8-)
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Man I am so close! But yet so far away. Here is what I have. By changing the {} around I can get it to enter the second Switch/Case, but I never have any control with the second button. It cycles through the color cases every time I get back around to the LED1 case.

Code:
void loop(){

  buttonOneState = digitalRead(switchPin);
  buttonTwoState = digitalRead(switchPin2);

  if(buttonOneState == HIGH && previousButtonOneState == LOW){
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      if(buttonTwoState == HIGH && previousButtonTwoState == LOW){        
        switch(colorState){
        case COLOR1:
          lcd.clear();
          lcd.print("Color 1");
          colorState = COLOR2; //transition to color2 state
          break; //end of color1 case
        case COLOR2:
          lcd.clear();
          lcd.print("Color 2");
          colorState = COLOR3; //transition to color3 state
          break; //end of color2 case
        case COLOR3:
          lcd.clear();
          lcd.print("Color 3");
          colorState = COLOR1; //transition back to color1 state
          break; //end of color3 case
          previousButtonTwoState = buttonTwoState;
        }  
      }
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      ledstate = LED2; //transition to led2 state    
      break; //end of led1 case

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of led2 case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of led3 case

    }
  }
  previousButtonOneState = buttonOneState;
  previousButtonTwoState = buttonTwoState;
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On any given pass through loop, which probably happens far oftener than you think, only one button is pressed. Only one button can have changed state.

Therefore, you need to treat the buttons, and their states, as completely independent.

In the loop function, see if button 1 is being presses, but was not before. If that is the case, set ledState.

Then, see if button 2 is being pressed, but was not before. If that is the case, set colorState.

The value of colorState has nothing to do with whether button 1 is being pressed, or not. It depends on (or changes as a result of) ledState, not whether button 1 is being pressed.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, this time I got it. I moved the entire colorstate switch out of the first loop and then added "ledstate == LED2" to the first line as was mentioned before. Thanks for the help again PaulS! 8-)

Here is the code as it works for me.

Code:

// include the library code:
#include <LiquidCrystal.h>
#include <Button.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 LED1 0
#define LED2 1
#define LED3 2
#define COLOR1 3
#define COLOR2 4
#define COLOR3 5

//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 ledstate = LED1; //create state variable and initialize it to START state
int colorState = COLOR1; //create state variable and initialize it to START state

int previousButtonOneState;
int previousButtonTwoState;
int buttonOneState;
int buttonTwoState;


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
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 27 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 LCD and reset cursor
    lcd.setCursor(1, 9);
    lcd.print("Select LED < >");  //Select desired LED from menu
    delay(2000);
  }

  pinMode(RED_PIN, OUTPUT);      // Set the pin as output
  pinMode(BLUE_PIN, OUTPUT);    // Set the pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

  digitalWrite(BLUE_PIN,LOW);   //turn blue led off
  digitalWrite(RED_PIN,LOW);     //turn red led off

  Serial.begin(9600);           // Set up serial communication at 9600bps

  previousButtonOneState = LOW;
  previousButtonTwoState = LOW;
  buttonOneState = LOW;
  buttonTwoState = LOW;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


void loop(){

  buttonOneState = digitalRead(switchPin);
  buttonTwoState = digitalRead(switchPin2);

  if(buttonOneState == HIGH && previousButtonOneState == LOW){
    switch(ledstate){
    case LED1:
      lcd.clear();
      lcd.print("Snare");
      digitalWrite(BLUE_PIN,HIGH); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led off
      delay(100); //wait 100ms
      ledstate = LED2; //transition to led2 state    
      break; //end of led1 case

    case LED2:
      lcd.clear();
      lcd.print("TOM 1");
      digitalWrite(BLUE_PIN,LOW); //turn blue led off
      digitalWrite(RED_PIN,HIGH); //turn red led on
      delay(200); //wait 200ms
      ledstate = LED3; //transition to led3 state
      break; //end of led2 case

    case LED3:
      lcd.clear();
      lcd.print("TOM 2");
      digitalWrite(RED_PIN,LOW); //turn red led off
      digitalWrite(BLUE_PIN,HIGH); //turn blue led on
      delay(200); //wait 200ms
      ledstate = LED1; //transition to led1 state
      break; //end of led3 case

    }
  }
        if(buttonTwoState == HIGH && previousButtonTwoState == LOW && ledstate == LED2){      
          
          switch(colorState){
        case COLOR1:
          lcd.clear();
          lcd.print("Color 1");
          colorState = COLOR2; //transition to color2 state
          break; //end of color1 case
        case COLOR2:
          lcd.clear();
          lcd.print("Color 2");
          colorState = COLOR3; //transition to color3 state
          break; //end of color2 case
        case COLOR3:
          lcd.clear();
          lcd.print("Color 3");
          colorState = COLOR1; //transition back to color1 state
          break; //end of color3 case
          previousButtonTwoState = buttonTwoState;
        }  
      }

  previousButtonOneState = buttonOneState;
  previousButtonTwoState = buttonTwoState;
}
Logged

Pages: 1 [2] 3   Go Up
Jump to: