const int buttonPin = 8;
int buttonState = LOW;
int lastButtonState = LOW;
bool buttonToggled = false;
bool ledToggled = false;
const int ledPin = 9;
int ledState = LOW;
int dt = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
//Serial.print("Button State = ");
//Serial.println(buttonState);
if(buttonState != lastButtonState){
if(buttonState == LOW)
{
buttonToggled = !buttonToggled;
if(buttonToggled){
Serial.println("Button is Pressed");
digitalWrite(ledPin, HIGH);
}
else{
Serial.println("Button is NOT Pressed");
digitalWrite(ledPin, LOW);
}
}
}
delay(dt);
lastButtonState = buttonState;
}