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:

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();


Arduino Forum > Using Arduino > Programming Questions > When taking input from a button and using digitalWrite() result if flip-floped


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support