/*
Lab 2 Challenging Content
Vraj Patel, Makeda Morrison
Lab Section: MAE 311-01
Due Date: June 23, 2025
*/
const int switchPin = 2; // monitors pin 2
const int ledPin = 11; // The LED is on pin 11
int buttonState; // Remembers state of the button
int lightState = 0; // Remembers state of the LED (starts off)
int blinkState = LOW; // stores initial state of blinking LED
long previousMillis = 0; // stores last time the LED was updated
long interval = 1000; // blinking interval in milliseconds
unsigned long currentMillis = 0; // start the timer at 0
int blinkStrength; // PWM strength variable for State 2
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Set up serial communication at 9600 bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop() {
int val = digitalRead(switchPin); // read input value and store it in val
delay(10); // waits 10 milliseconds to read the switch again
int val2 = digitalRead(switchPin); // second read of the switch
if (val == val2) { // ensure it was really a press
if (val != buttonState) { // The button state has changed
if (val == LOW) { // check if the button is being pressed
if (lightState == 0) { // check if the light is off
lightState = 1; // set the blink mode to ON
Serial.println("Button just pressed, Blinking");
currentMillis = 0; // sets the start time to 0
previousMillis = 0; // sets the current time to zero
blinkState = LOW; // ensures that the LED variable is off
}
else if (lightState == 1) {
lightState = 2;
Serial.println("Button pressed again, long blinks");
}
else if (lightState == 2) {
lightState = 3;
digitalWrite(ledPin, HIGH);
Serial.println("Button pressed again, continuously on");
Serial.print(lightState);
}
else if (lightState == 3) { // button pressed in State 3, go back to initial off state
lightState = 0; // sets the state to OFF
digitalWrite(ledPin, LOW); // turn LED off
blinkState = LOW; // set the led variable off
Serial.println("Button just pressed, light off");
}
}
}
}
// BLINKING SECTION START //
if (lightState == 1) { // if the blink mode is active, do this
currentMillis = millis(); // record the current time
if (currentMillis - previousMillis > interval) { // compare start and current time, only execute if greater than set blink interval time
previousMillis = currentMillis; // set the timer equal since blink interval is reached
if (blinkState == LOW) // if the led is off, then
blinkState = HIGH; // turn it on
else
blinkState = LOW; // turn it off
digitalWrite(ledPin, blinkState); // actually turns it off or on
}
}
// BLINKING SECTION END //
buttonState = val; // save the new state in our variable
}