Hi,
I was trying to create a code that would cause a LED diode to light up every time a push button is pressed. But when I uploaded the code to Arduino Nano the diode was lightening all the time. I don't know, whether the problem is in the code or in the wiring.
Thanks for reply in advance.
int cteni = 0;
int led = 5;
int tlacitko = 6;
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);
pinMode(tlacitko,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
cteni = digitalRead(tlacitko);
if (cteni == HIGH) {
digitalWrite (led, HIGH);
}
else {
digitalWrite (led, LOW);
}
Tl;dr
const byte led = 5;
const byte tlacitko = 6;
void setup()
{
pinMode(led,OUTPUT);
pinMode(tlacitko,INPUT);
}
void loop()
{
digitalWrite (led, digitalRead(tlacitko));
}
(uncompiled, untested)
Please remember to use code tags when posting code
I'll bear that in mind. I tried your code but the diode is on all the time as it was with mine.
Wiring the tactile momentary switch.
You had better to use INPUT_PULLUP.
See how to control LED by button
Thank you all for the tips, I tried it, but it did't function. I guess there is a problem with the button, because when I checked the serial monitor there were zeros all the time, no matter whether the button was being pressed or not.
strouhanka:
Thank you all for the tips, I tried it, but it did't function. I guess there is a problem with the button, because when I checked the serial monitor there were zeros all the time, no matter whether the button was being pressed or not.
Then just try a jumper from GND to the input.
Did you see the important part about wiring to diagonal terminals of the switch? If wired straight across, there is a 50% chance that the wiring is a dead short.
Thank you, now it functions