First off, you freakin' ROCK! Arduino rocks, and AdaFruit and LadyAda Rock even more! I'm an old dude and you're teaching me new tricks.
I'm embarrassed to ask a probably stupid question, but here goes.... I thought I'd get cute and instead of using the serial monitor output to do the "alternating action switch" project, I thought I'd combine this two-LED circuit below (from the project just before) and change to code to "latch" the LED's back and forth.
I used this code (which is shamelessly hacked together from the tutorial code):
int led1Pin = 12; // LED #1 is connected to pin 12
int led2Pin = 11; // LED #2 is connected to pin 11
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the last button state
void setup() {
pinMode(led1Pin, OUTPUT); // Set the LED #1 pin as output
pinMode(led2Pin, OUTPUT); // Set the LED #2 pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
digitalWrite(led1Pin, HIGH); // turn LED #1 on
digitalWrite(led2Pin, LOW); // turn LED #2 off
}
else { // check if the button is not pressed
digitalWrite(led1Pin, LOW); // turn LED #1 off
digitalWrite(led2Pin, HIGH); // turn LED #2 on
}
}
buttonState = val; // save the new state in our variable
}
So WHY, oh WHY, isn't this working? It still works like the old two LED project did, and doesn't remain in the last state read... Methinks I'm doing something SO stupid that I can't even fathom the answer....
Can somebody help debug this?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Can somebody help debug this?
Code: Select all
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
digitalWrite(led1Pin, HIGH); // turn LED #1 on
digitalWrite(led2Pin, LOW); // turn LED #2 off
}
else { // check if the button is not pressed
digitalWrite(led1Pin, LOW); // turn LED #1 off
digitalWrite(led2Pin, HIGH); // turn LED #2 on
}
}
buttonState = val; // save the new state in our variable
}
To keep the debouncing behavor you currently have, you may want to keep buttonState as is and add another variable to store the alternating 'toggle' action.
-
- Posts: 3
- Joined: Mon May 09, 2011 9:53 pm
Re: Can somebody help debug this?
OK...will poke around a bit tonight after work to see if I can wrap my brain around your advice and make it work. Thank you so much!
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
Re: Can somebody help debug this?
Code: Select all
int led1Pin = 12; // LED #1 is connected to pin 12
int led2Pin = 11; // LED #2 is connected to pin 11
int led1State = HIGH;
int led2State = LOW;
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the last button state
void setup() {
pinMode(led1Pin, OUTPUT); // Set the LED #1 pin as output
pinMode(led2Pin, OUTPUT); // Set the LED #2 pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
led1State ^= HIGH; // toggle led 1
led2State ^= HIGH; // toggle led 2
digitalWrite(led1Pin, led1State);
digitalWrite(led2Pin, led2State);
}
}
buttonState = val; // save the new state in our variable
}
-
- Posts: 3
- Joined: Mon May 09, 2011 9:53 pm
Re: Can somebody help debug this?
Thank you so much for the help...
Sigh... I think my brain was not meant for coding. The electronics come pretty easy. Coding, not so much.
But again, I'll try it out and do some pondering upon it when I get a few moments to play.
Sigh... I think my brain was not meant for coding. The electronics come pretty easy. Coding, not so much.
But again, I'll try it out and do some pondering upon it when I get a few moments to play.
Please be positive and constructive with your questions and comments.