I'm trying to write code to get an LED to turn on when it is off and to turn off when it is on using a tactile push button switch. I've written what I believe to be is the right code with the wiringPi library, but I can only get it to turn on when it is off and can't get it to turn off after that. On very rare instances and after many repeated presses will the LED turn off when it is on and I press the button, but I'm sure that's not how it's supposed to work.
#include <wiringPi.h>
int main (void)
{
wiringPiSetup ();
pinMode (0, OUTPUT);
pinMode (1, INPUT);
digitalWrite (0, LOW);
for(;;)
{
if(digitalRead (1) == LOW)
{
if(digitalRead (0) == HIGH)
digitalWrite (0, LOW);
else if(digitalRead (0) == LOW)
digitalWrite (0, HIGH);
}
}
return 0;
}
I have attached an image of how the circuit is wired.