Hannah Jiang



Get in touch if you want to chat about design or just simply want to say HI!

haiyuanj@gmail.com

Week 1 – Turn On/Off the Led Light

The code:

int buttonPin = 2;
int stateLED = LOW;
int buttonState;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && previous == LOW && millis()-time > debounce){
if(stateLED == HIGH){
stateLED = LOW;
}else{
stateLED = HIGH;
}
time = millis();
}
digitalWrite(LED_BUILTIN, stateLED);
previous == buttonState;
}

The demo: