When taking input from a button and using digitalWrite() result if flip-floped
i trying take input button , turn led on or off based on whether digitalread() returns 1 or 0. when test code press button , led turns off. when don't press button led turns on. here code:
if change line 5 to:
and line 7 to:
if works rather know why behaving in sort of manner. have push button wired pin 2 5v going when close circuit. wired button wired in arduino's button tutorial. have led in pin 3.
thank in advance
code: [select]
void setup() {
serial.begin(9600);
pinmode(2, input);
pinmode(3, output);
}
void loop() {
int value = digitalread(2);
if(value == 1)
{
digitalwrite(3, high);
} else {
digitalwrite(3, low);
}
}
if change line 5 to:
code: [select]
digitalwrite(3, low);
and line 7 to:
code: [select]
digitalwrite(3, high);
if works rather know why behaving in sort of manner. have push button wired pin 2 5v going when close circuit. wired button wired in arduino's button tutorial. have led in pin 3.
thank in advance
your button floating, need pulldown resistor gnd.
also use boolean instead of int getting value digitalread();
also use boolean instead of int getting value digitalread();
Arduino Forum > Using Arduino > Programming Questions > When taking input from a button and using digitalWrite() result if flip-floped
arduino
Comments
Post a Comment