LED/Pressbutton woes

In Chapter 4, Example 2, I've got my code and wiring exactly like the book describes, but my LED stays lit, and won't change whether I press the button or not. This seems really straightforward, other than the fact it doesn't work as expected. Any ideas what I'm doing wrong?

Chris

I've got my code and wiring exactly like the book describes

Perhaps you haven't.... 8)

You should post the actual code here, copied from the ide not the book. Also post a diagram or clear photo of the actual circuit.

Absolutely, thank you!

// Hello world

const int LED = 13;
const int BUTTON = 7;
int val = 0;

void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);

}

void loop() {

val = digitalRead(BUTTON);

if (val == HIGH) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}

}

I'm not at home where I have access to one of those switches and my multi-meter, but you might have that switch rotated by 90 degrees.

The wires might be on the links that are always connected, not across the "open" part which the switch closes. If you have a meter you can check, but in anycase just re-orientate it and see if that works.

I don't have one of those switches... thought I did :stuck_out_tongue_closed_eyes:

But if you have a look at the Fritzing pic on this tut, it looks to me like your switch is the wrong way round?

The switch joins the pins on the same side when it's pushed, not the opposite pins which is more intuitive and is the way you have yours: if I'm right then your red and yellow are always joined, not switched.

Edit.... I dug around and these switches seem to be (or are similar to) Omron B3Fs. Have a look at the pic attached from their datasheet, which indeed shows that opposite pins are always connected, and pushing the button connects the pins on the same side; in fact connects all 4 pins.

switch2.jpg

A good quick test to see if you have the switch the wrong way is to just pull it out and see if the LED goes out. If it does go out then just rotate your switch 90 deg.

Yep, that was it! :slight_smile:

Thank you!

Switch pin layout level: FAIL