Can somebody help debug this?

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Geeksurgeon
 
Posts: 3
Joined: Mon May 09, 2011 9:53 pm

Can somebody help debug this?

Post by Geeksurgeon »

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.

Image

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....

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Can somebody help debug this?

Post by adafruit_support_bill »

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
}
In the loop you are saving the button state as the result of the last digitalRead. To get an alternating action, you only want to change buttonState when the button is pressed.
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.

Geeksurgeon
 
Posts: 3
Joined: Mon May 09, 2011 9:53 pm

Re: Can somebody help debug this?

Post by Geeksurgeon »

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!

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Can somebody help debug this?

Post by mtbf0 »

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
}
when you start the sketch both leds will be off. at the first button press led2 will come on. at each subsequent button press the leds will toggle. since you are not debouncing the switch input you'll probably get multiple presses each time you push the button. whether that turns out to be an odd or even number of presses will determine whether or not it looks like the sketch is working. see tutorial 5 for switch debouncing.

Geeksurgeon
 
Posts: 3
Joined: Mon May 09, 2011 9:53 pm

Re: Can somebody help debug this?

Post by Geeksurgeon »

Thank you so much for the help...

Sigh... I think my brain was not meant for coding. :cry: 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.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”