0

I have the following build:

enter image description here

And the following code:

const int buttonPin = 3;
const int ledPin = 2;
int buttonState = 0;

void setup() {
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

}

void loop() {
  while (digitalRead(ledPin) != LOW){
    buttonState = digitalRead(buttonPin);

    if (buttonState == LOW){
      digitalWrite(ledPin, LOW);
    }
  }
}

But it doesn't seem like anything is registered, as my program doesn't respond to the shut down of the LED. What have I done wrong?

7
  • Please post an actual schematic. Why do you have constants like buttonPin and ledPin and then ignore them and have pinMode(2, INPUT); and pinMode(3, OUTPUT);?
    – Nick Gammon
    Jan 14, 2017 at 7:13
  • I've tried to post an actual schematic now + changed the code
    – andrew
    Jan 14, 2017 at 9:50
  • On what did you base your wiring? I think/see that there are multiple wiring issues.
    – aaa
    Jan 14, 2017 at 10:52
  • Try again. R1 (at the top) doesn't seem to be connected to anything. And as for the switch, is it supposed to short out the LED?
    – Nick Gammon
    Jan 14, 2017 at 10:56
  • Yes, it's supposed to turn it off
    – andrew
    Jan 14, 2017 at 10:56

2 Answers 2

0

Looking at your schematic, I get a bit confused. You should start out with creating the schematic, try to base it on existing schematics on the web/tutorials.

I believe you're trying to read out a button and set an led accordingly?

So start out with the schematic and then try to build a breadboard setup for this. Randomly wiring things up on your breadboard doesn't really make sense, you need a plan.

I've created a shematic of which I believe should work a treat.

schematic

simulate this circuit – Schematic created using CircuitLab

Using internal pull-ups on the SW1, to avoid the input floating. Using 220ohms resistor, based on "Basic Red 5mm - Sparkfun LED", 5V source, 2V forward 16mA (http://led.linear1.org/1led.wiz). Be carefull not to set the SW1 as an output (high) and push the button, this theoretically burns out the I/O pin. But even then, this is only basic, you may want to consider input protection (http://www.thebox.myzen.co.uk/Tutorial/Protection.html)

It's very easy to mess something up or do something incorrectly in electrical engineering. To be honest, it may help to watch a lot of tutorials or read books on electronics. It's very usefull to have a basic understanding before wiring things up.

At least base your actions on a (good) tutorial or reference material from Arduino self.

Try to wire up the circuit according to my schematics

Mouseover for example (but please try it yourself before checking!)

enter image description here

The same actually counts for programming, first try to make it clear what you want to achieve. Maybe even write out the steps that your program should do on a functional level.

I have no idea how you program could actually compile, the while and if statements don't have any closing brackets.

const int buttonPin = 2;//I switched the LED and button in my design.
const int ledPin = 3;//So the LED is on a PWM pin, which makes more sense.

void setup() {
  //Serial.begin(9600); //We're not using serial, so why start serial?
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
   int value = digitalRead(buttonPin);
   digitalWrite(ledPin, value);
   //or digitalWrite(ledPin,digitalRead(buttonPin));//for short
}
5
  • Thank you for this elaborate answer ... Seems like I made everything more difficult, hah. Sorry everyone. Anyway, I forgot to add the closing brackets, I've added them now.
    – andrew
    Jan 14, 2017 at 12:13
  • @andrew no point, starting out with something can be hard/confusing. That's why it helps to read some tutorials, they often include wiring diagrams (and sometimes pictures) and put the code in the end, so you can verify if it's working.
    – aaa
    Jan 14, 2017 at 12:18
  • Indeed, thanks. I have a question though, why does it make more sense to put the LED on a PWM pin?
    – andrew
    Jan 14, 2017 at 13:12
  • Ah, pwm can be used to dim the LED, it's nothing critical, but it's nice to have such a feature.
    – aaa
    Jan 14, 2017 at 13:21
  • Alright, I'll keep that in mind. Thanks for all the help, doesn't seem like my badly-formulated question deserves an answer.
    – andrew
    Jan 14, 2017 at 13:21
0

The diagram is open to several different interpretations, depending on what's inside the four-terminal switch. On many such SPST switches, two adjacent terminals are connected to each other inside it, and the other two likewise. Which are which in the diagram isn't indicated.

That aside, what is clear from the diagram is that pin 2 is connected by wires to +5V, and will always read in as high.


Edit 1: The schematic [posted later] is unclear. It should be arranged so that solid lines represent wires or pins, with solid lines between things that are connected. Place items in the drawing to reduce unnecessary and confusing crossing lines. For example, supposing that the dashed line from pin 2 of R1 represents a wire between pin 2 of R1 and pin 2 of LED1, it is unclear whether the wire bends around the top right pin of S1 (can't read number) or connects with that pin on its way to LED1. For another example, nothing is shown connecting to pin 1 of R1, and only one long dashed line connects the R1/S1/LED1 group to the Uno.

3
  • Please take another look, I added a new diagram.
    – andrew
    Jan 14, 2017 at 9:49
  • I don't get a plausible understanding of the wiring from either the diagram or the schematic. Also see edit 1. Jan 14, 2017 at 10:32
  • Sorry ... I'm new at this. Pin3 is connected to the LED. It is the crooked wire. Pin2 is connected to the switch. It is wire no. 2 from the left. The first wire from the left connects the resistor to the switch
    – andrew
    Jan 14, 2017 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.