const int button = 11;
const int led = 4;
int currentState = LOW;
int previousState = LOW;
int ledState = LOW;
void setup()
{
pinMode(button, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop()
{
currentState = digitalRead(button);
if ((currentState == HIGH) && (previousState == LOW))
{
if (ledState == LOW) // toggle ledstate from LOW to HIGH
ledState = HIGH; // or HIGH to LOW
else // depending on the initial state
ledState = LOW;
}
previousState = currentState; // moves currentState to previousState
digitalWrite(led, ledState); // for next cycle
delay(10);
}