int ledPin = 7; // pin for the LED signal
int buttonPin = 2; // pin for the button signal
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) { // != is Arduino code for Not Equal to
if (buttonState == HIGH) { // == Checks if the value of two operands is equal or not, if yes then condition becomes true.
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the LED
}
}
lastButtonState = buttonState;
}