/*
Name: Mason Mills & Louis McEvoy
Section: 311L-03
Date: 10/2/24
*/
//Set pin 2 to the pin with the switch connected to
const int switchPin = 2;
//Set pin 9,10,& 11 to the pin with each color connected to
const int R = 9;
const int G = 10;
const int B = 11;
//Establishing variables used to keep track of the time difference and PWM values
long mill = 0;
long milldiff = 0;
long mill2 = 0;
double i = 0;
float j = 0;
long p = 0;
long c = 0;
//Establish the two varables to hold a 1 or 0 based on the button state
int val = 0;
int val2 = 0;
//Flag Variables
int x = 0;
int buttonpush = 0; //Gets set to 1 when the button is pushed
//Sets the staring postion of the case statement to 1
//State 1 = R,G,B PWM 7
//State 2 = Table 1: 1 loop = (3s)
//State 3 = Table 2: 1 loop = (3s)
int state = 1;
void setup() {
//Begins serial monitor used for debugging
Serial.begin(9600);
//Initialize as an input for the button
pinMode(switchPin, INPUT);
//Initialize as outputs for the LED PWM values
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
}
void loop() {
//Calc. for differecnce in time used throughout Case 2 and 3
milldiff = millis() - mill;
//Reads the buttons state
val = digitalRead(switchPin);
//Debounce delay of 10ms
delay(10);
//Reads the button state again
val2 = digitalRead(switchPin);
//First condition is met if the button is pushed, meaning no button bounces will be considered a button press
if (val == val2) {
//This resets the flag variable when the button is not pressed
if (val == LOW) {
x = 0;
}
//If the button detects a press the buttonpush flag is set to 1
if (val == HIGH) {
buttonpush = 1;
//Condition to set initial time if the button is held
if (x == 0) {
p = millis();
x = 1;
}
c = millis();
//When the time of the button press exceeds 500 ms it is considered a hold and button press resets to 0
if (c - p > 500) {
buttonpush = 0;
//Serial.println("HOLD");
}
}
}
//Switch case progresses through states 1-3 then loops back to 1 when the button is pushed and not held
switch (state) {
//White (kinda)
case 1:
//Reads button state again
val2 = digitalRead(switchPin);
//When the button push flag is triggered and the button is not held the state changes
if (buttonpush == 1 && val != val2) {
//Resets the buttonpush back to 0
buttonpush = 0;
//Establishes new starting mill for Case 2
mill = millis();
//Now jumps to state 2
state = 2;
//Serial.println("Button Press");
}
//LED is set to On
analogWrite(R, 7);
analogWrite(G, 7);
analogWrite(B, 7);
break;
//Case given by Table 1, Red --> White Flash x2 --> Blue repeat
case 2:
//Reads button state again
val2 = digitalRead(switchPin);
//When the button push flag is triggered and the button is not held the state changes
if (buttonpush == 1 && val != val2) {
//LED is set to OFF
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
//Reset buttonpush to 0
buttonpush = 0;
//Establishes new starting mill and mill2 for Case 3
mill = millis();
mill2 = millis();
state = 3;
//Serial.println("Button Press");
}
//If statements are based off the milldiff which avoids the use of delays
else if (milldiff >= 3000 ) {
mill = millis();
}
else if (milldiff >= 2000) {
analogWrite(B, 100);
}
else if (milldiff >= 1850) {
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
}
else if (milldiff >= 1500) {
analogWrite(R, 255);
analogWrite(G, 255);
analogWrite(B, 255);
}
else if (milldiff >= 1350) {
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
}
else if (milldiff >= 1000) {
analogWrite(R, 255);
analogWrite(G, 255);
analogWrite(B, 255);
}
else if (milldiff >= 0 && milldiff < 1000) {
analogWrite(G, 0);
analogWrite(B, 0);
analogWrite(R, 100);
}
break;
case 3:
val2 = digitalRead(switchPin);
if (buttonpush == 1 && val != val2) {
//Reset buttonpush to 0
buttonpush = 0;
//Moves to state 1
state = 1;
//Serial.println("Button Press");
}
//If statements are based off the milldiff which avoids the use of delays
//Only runs every 20ms
else if (millis() - mill2 >= 20) {
mill2 = millis();
if (3000 <= milldiff) {
mill = millis();
}
else if (milldiff >= 2000) {
//Sets j back to a starting value of 0
j = milldiff - 2000;
//Given Function for intensity
i = pow(((980 - j) / 40), 1.732);
analogWrite(R, i);
analogWrite(G, i);
analogWrite(B, i);
}
else if (milldiff >= 1000) {
//Sets j back to a starting value of 0
j = milldiff - 1000;
//Given Function for intensity
i = pow(((980 - j) / 40), 1.732);
analogWrite(R, i);
analogWrite(G, i);
}
else if (milldiff >= 0 && milldiff < 1000) {
//Given Function for intensity
i = pow(((980 - milldiff) / 40), 1.732);
analogWrite(R, i);
}
}
break;
}
}