Error In Lesson 5 Code

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
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Error In Lesson 5 Code

Post by GAHorton »

Hello,

I found an error in the code here:

Code: Select all

/*
 *  Switch and LED test program
 */
 
int ledPin = 12;                // LED is connected to pin 12
int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status


void setup() {
  pinMode(ledPin, OUTPUT);      // Set the LED 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 == LOW) {               // check if the button is pressed
    digitalWrite(ledPin, HIGH);   // turn LED on
  }
  if (val == HIGH) {              // check if the button is not pressed
    digitalWrite(ledPin, LOW);    // turn LED off
  }
}
it should be:

Code: Select all

void loop(){
  val = digitalRead(switchPin);   // read input value and store it in val
  if (val == HIGH) {               // check if the button is pressed
    digitalWrite(ledPin, HIGH);   // turn LED on
  }
  if (val == LOW) {              // check if the button is not pressed
    digitalWrite(ledPin, LOW);    // turn LED off
  }
}
the old code causes the light to stay on when the button is NOT pressed. which is not the meaning of this part of the lesson.

GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: Error In Lesson 5 Code

Post by GAHorton »

i actually think my problem is with the switch, i think normally closed switches were included in my kit because all of the code is backwards for me...

Fjornir
 
Posts: 58
Joined: Fri Nov 28, 2008 3:14 pm

Re: Error In Lesson 5 Code

Post by Fjornir »

Is it possible you're wiring up for pulldown where pullup is expected?

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

Return to “Arduino Starter Pack”