Week 7: Embedded Programming
Writing Code
Admittedly, I took this week as a lower-energy rest week due to other things going on in my life in parallel. As it turns out, I had technically fulfilled the requirements for this week back in Week 4 when I completed the group assignment for our section. Using the same board, I modified the code a little just for practice, however, to turn on and off the blinking light with the button (including debouncing).const int LED = 4;
const int button = 2;
int lastButtonState;
int currentButtonState;
int onOffState = LOW;
long time = 0;
long debounce = 100;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED, OUTPUT);
pinMode(button, INPUT);
}
// the loop function runs over and over again forever
void loop() {
lastButtonState = currentButtonState;
currentButtonState = digitalRead(button);
if (currentButtonState == HIGH && lastButtonState == LOW && millis() - time > debounce)
{
onOffState = !onOffState;
}
if (onOffState == HIGH)
{
// digitalWrite(LED, onOffState);
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
}
}
It worked. ¯\_(ツ)_/¯