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