/*
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) { // button is pressed again, going to state 2 (slow blinks)
lightState = 2; // set blink mode to long blinks
Serial.println("Button pressed again, long blinks");
currentMillis = 0; // reset start timer to 0
previousMillis = 0; // reset current time to zero
blinkStrength = 0; // set PWM strength to zero
}
else if (lightState == 2) { // button is pressed again, go to state 3 (continuously on)
lightState = 3; // set blink mode to continuously on
Serial.println("Button pressed again, continuously on");
blinkState = HIGH; // ensures LED will be on
}
else { // 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
}
}
else if (lightState == 2) { // do this if long blink mode is active
currentMillis = millis(); // record current time
if ((currentMillis - previousMillis) < interval) { // if it hasn't been 1 second since button press (gradually increasing brightness)
// There is a 10 ms delay built into each loop of this script during the debounce button check, so factoring that in,
// in 1 second this loop will run 1000ms/10ms = 100 times. To go from 0-255 PWM strength in 100 cycles, each cycle must increase
// PWM strength by 255/100 units, as shown beloww
blinkStrength += 255/(interval/10);
}
else if (((currentMillis - previousMillis) >= interval) && ((currentMillis - previousMillis) <= 2*interval)) { // if it has been more than a second but less than 2 seconds (decreasing brightness)
blinkStrength -= 255/(interval/10); // same process as increasing, but decrease strength this time
}
else { // it has been more than 2 seconds, reset long blink cycle
previousMillis = currentMillis; // set timers equal again since interval complete
blinkStrength = 0; // reset PWM strength to zero
}
analogWrite(ledPin, blinkStrength); // actually dim or brighten the light
}
else { // covers states 3 and 4
digitalWrite(ledPin, blinkState);
}
// BLINKING SECTION END //
buttonState = val; // save the new state in our variable
}