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]
I tried that, but nothing changed. Any other ideas or possible errors in my code?
Thank you for looking at this for my PaulS!
The code, as you have it written, requires that you have both buttons pressed. Do you?
No, I wasn't. I guess I need to change "buttonState" back to low before I can use "buttonState2" by itself?
I tried that, but nothing changed
That's because they're not required.
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?
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.
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! ::)
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! :D
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
}
}
}
}
}
This:
if (ledstate == LED2)
if (buttonState2 == HIGH)
if (buttonState == LOW){
is the same as writing:
if ( (ledstate == LED2) &&
(buttonState2 == HIGH) &&
(buttonState == LOW)) {
OK, but will that change anything to make it work?
No, but it may make it easier to read.
OK :)
Is it safe to assume that you pushed button 1, and then pushed button 2?
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.
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:
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;
}
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.
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
}
}
}
At least, you're trying. Boy, are you trying. ;)
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...
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. :-/
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;
}
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-)
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.
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;
}