int currentState = 0, previousState = 0;
int btnPin = 10, ledPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(btnPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int currentState = digitalRead(btnPin);
if (currentState == 1 && previousState == 0) {
int ledPinVal = digitalRead(ledPin);
if (ledPinVal == 1) {
digitalWrite(ledPin, 0);
}
if (ledPinVal == 0) {
digitalWrite(ledPin, 1);
}
}
//for debugging purpose
Serial.print("currentState: "); Serial.print(currentState);
Serial.print("; previousState: "); Serial.println(previousState);
previousState = currentState;
}
//Lesson on Debouncing
//Assignment:
//Use a sensor in place of the button such that if the sensor senses a presence, the light will come on and vice versa.