Pages: [1]   Go Down
Author Topic: LED/Pressbutton woes  (Read 1681 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Perhaps you haven't....  smiley-cool

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

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
 
}




* photo.JPG (855.23 KB, 1632x1224 - viewed 27 times.)
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have one of those switches... thought I did  smiley-yell

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 (21.11 KB, 382x442 - viewed 22 times.)
« Last Edit: February 08, 2013, 08:39:28 am by JimboZA » Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Wahiawa, Hawaii
Offline Offline
God Member
*****
Karma: 27
Posts: 582
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, that was it! smiley

Thank you!
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Switch pin layout level: FAIL
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Pages: [1]   Go Up
Jump to: