Simple button-LED circuit

I've been making a sketch where if the button is pressed the light turns on. Can you guys see what is wrong in my code please?

void setup() {
  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  pinMode(3, INPUT_PULLDOWN);
}

void loop() {
  // put your main code here, to run repeatedly:
  b = digitalRead(3);
  if (b == 1){
    digitalWrite(2, HIGH);
  }
  else {
    digitalWrite(2, LOW);
  }
}

It would likely help to state what is wrong, from what I can see,

You are writing to a variable, without declaring it, that would be a problem.
For some reason, INPUT_PULLDOWN looks weird to me…

Compile errors are useful.

1 Like

should be
pinMode(3, INPUT_PULLUP);

note that this may reverse the logic from what you expect to see

1 Like

here are my error messages:
C:\Users\merch\AppData\Local\Temp.arduinoIDE-unsaved202435-16560-i1nbc.1v5hd\BareMinimum\BareMinimum.ino: In function 'void setup()':
C:\Users\merch\AppData\Local\Temp.arduinoIDE-unsaved202435-16560-i1nbc.1v5hd\BareMinimum\BareMinimum.ino:4:14: error: 'INPUT_PULLDOWN' was not declared in this scope
pinMode(3, INPUT_PULLDOWN);
^~~~~~~~~~~~~~
C:\Users\merch\AppData\Local\Temp.arduinoIDE-unsaved202435-16560-i1nbc.1v5hd\BareMinimum\BareMinimum.ino:4:14: note: suggested alternative: 'INPUT_PULLUP'
pinMode(3, INPUT_PULLDOWN);
^~~~~~~~~~~~~~
INPUT_PULLUP
C:\Users\merch\AppData\Local\Temp.arduinoIDE-unsaved202435-16560-i1nbc.1v5hd\BareMinimum\BareMinimum.ino: In function 'void loop()':
C:\Users\merch\AppData\Local\Temp.arduinoIDE-unsaved202435-16560-i1nbc.1v5hd\BareMinimum\BareMinimum.ino:9:3: error: 'b' was not declared in this scope
b = digitalRead(3);
^

exit status 1

Compilation error: 'INPUT_PULLDOWN' was not declared in this scope

Why doesn't PULLDOWN work?

Input PULL-DOWN doesn’t even appear to exist for Uno R3…

Thanks

Thank you

So what I and @v205 mentioned aren't the only problems with that code.
This style is unworkable once you get past something more complicated than this.
Your variable declarations/names should be easy to identify later, something like

boolean redButtonState = digitalRead(redButtonPin);
boolean greenButtonState = digitalRead(greenButtonPin);

but thought out as to how large the variable need be, to what button you can identify on a breadboard six months from now if you put it down and come back to it later, etc

1 Like

Thanks

Very true, made that mistake several times too many.
booleans would be better, as a true/false state is only what is required for a button press

1 Like

Believe me, I have learned the hard way and I still am just ok at this.
Good thing Arduino tickles my brain just right.
What I love about these Arduinos is both how they can interact with the real world in ways that look like magic to the untrained observer, but also how simple they are in that everything they can do is within the reach of an average Joe like me. Just three hours or so ago, @ShermanP posted a code for some Arduinos to be able to read their own supply voltage by referencing the ATmega328P ADMUX register! Like, what?!
I just edited the post you commented on to make my the button state variables more consistent with the pin declaration variables. It would drive me bonkers to leave it the way I had it originally, for a button press routine!

1 Like

Exactly, the ATMega doesn't support it and the same is true for that Renesas chip.
I became accustomed to using internal pull-ups which are widely supported on the chips I've been using over the years

1 Like

Most common types of Arduino have only INPUT_PULLUP and do not have INPUT_PULLDOWN. You did not say what type of Arduino you are using, and you did not show a schematic showing how the button and led are connected.

As @v205 mentions, your code will not upload because it contains an error

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.