A
Arduino•3w ago
Zer

Rookie Mistake

so im trying to code on arduino droid using a push button to trigger led, why isnt this code working for turning on the led but the button is working as usual checking the monitor
No description
4 Replies
Reeshav_Ghosh200
Reeshav_Ghosh200•3w ago
the loop should go like this:
if (digitalRead(button)) digitalWrite(led, 1)
else digitalWrite(led, 0)
delay(100) // To prevent timer issues
if (digitalRead(button)) digitalWrite(led, 1)
else digitalWrite(led, 0)
delay(100) // To prevent timer issues
(digitalWrite and if else staments should be in the same line) @Zer
Zer
ZerOP•3w ago
i have a few question, is using "if" mandatory in this scenario, and is 1 and 0 indicate the output led state? thanks for answering ill try it llater im still new on coding and arduino so mb if i look oblivious because i am 💀 ill try to understand
Reeshav_Ghosh200
Reeshav_Ghosh200•3w ago
yes, 'if' you want the button to decide LED state, the using 'if' is mandatory and also, 1 mean 'HIGH' and 0 means 'LOW' basically, 'if' button pressed, then turn on led (1) 'else' turn it off if and else are the absolute bare basics, you should learn them, else you might not be able to write higher programs
Zer
ZerOP•3w ago
ohh okay, thankyou for your guidance, i thought that it isn't mandatory okay thanks

Did you find this page helpful?