const int switchPin = 2; //pin connection is 2 for button
int buttonState; //state of button press
int lightState = 0; //which state of blinking used
long previousMillis = 0; //previous time when a state starts
unsigned long currentMillis = 0; //time on ardiuno board when a state change starts
int I = 0; //value of nonlinear equation of state 2
int time = 0; //value of time used in the nonlinear equation of state 2
// PINS USED
int LED_Red = 9;
int LED_Green = 10;
int LED_Blue = 11;
void setup() {
// put your setup code here, to run once:
// initialize pin switch is connected to as input
pinMode(switchPin, INPUT);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_Green, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(LED_Red, OUTPUT);
// output to computer
Serial.begin(9600);
// buttonstate variable equal to input of button
buttonState = digitalRead(switchPin);
}
void loop() {
// put your main code here, to run repeatedly:
int val = digitalRead(switchPin); // val is equal to button press
delay(10); //wait 10 millsec
int val2 = digitalRead(switchPin); // val2 is equal to button press
if (val == val2) { // if the values of value 1 and 2 are the same, means no debounce
if (val != buttonState){ //button state changed
if (val == LOW) { //check if button being pressed
if (lightState == 0) { // check if light state is 0
lightState = 1; // if so, since button pressed change state to 1
Serial.println("Button just pressed, state 1"); // output testing
currentMillis = 0; //reset current time
previousMillis = 0; //reset prev time
}
else{
if (lightState == 1) { //if button was pressed and state is 1
lightState = 2; //change the state to 2
Serial.println("Button just pressed, state 2"); // output testing
currentMillis = 0; //reset current time
previousMillis = 0; //reset prev time
}
else{
lightState = 0; //if button was pressed, and state wasn't 0 or 1, reset the state value to 0
Serial.println("Button just pressed, light state 0"); // output testing
}
}
}
}
}
// state 0 if button is not pressed or pressed after state 2
if (lightState == 0) {
// Set LED to white at a PWM value of 7
analogWrite(LED_Red, 7);
analogWrite(LED_Blue, 7);
analogWrite(LED_Green, 7);
}
// State 1 PWM Light pattern
if (lightState == 1) {
currentMillis = millis();// set time to current millisecond count on arduino
if (currentMillis - previousMillis < 1000) { //if current time minus previous is less than one sec
// Turn on red to value of 100 for one sec {Change based on the time required}
analogWrite(LED_Red, 100);
Serial.println("PMW Red ON"); // output testing
}
if (currentMillis - previousMillis < 1350 && currentMillis - previousMillis > 1000) {
// if time is less than 1.2 sec and greater than 1 sec {Change based on the time required}
// turn on white at full value
// 1st white blink
analogWrite(LED_Red, 255);
analogWrite(LED_Green, 255);
analogWrite(LED_Blue, 255);
Serial.println("PMW White ON"); // output testing
}
if (currentMillis - previousMillis < 1500 && currentMillis - previousMillis > 1350) {
// if time is less than 1.3 and greater than 1.2 sec {Change based on the time required}
// turn off LED
analogWrite(LED_Red, 0);
analogWrite(LED_Green, 0);
analogWrite(LED_Blue, 0);
Serial.println("PMW White Off"); // output testing
}
if (currentMillis - previousMillis < 1850 && currentMillis - previousMillis > 1500) {
// if time is less than 1.5 sec and greater than 1.3 sec {Change based on the time required}
// turn on white at full value
// second white blink
analogWrite(LED_Red, 255);
analogWrite(LED_Green, 255);
analogWrite(LED_Blue, 255);
Serial.println("PMW White ON"); // output testing
}
if (currentMillis - previousMillis < 2000 && currentMillis - previousMillis > 1850) {
// if time is less than 1.6 sec and greater than 1.5 sec {Change based on the time required}
// turn off LED
analogWrite(LED_Red, 0);
analogWrite(LED_Green, 0);
analogWrite(LED_Blue, 0);
Serial.println("PMW White Off"); // output testing
}
if (currentMillis - previousMillis < 3000 && currentMillis - previousMillis > 2000) {
// if time is less than 2.9 and greater than 1.9 sec {Change based on the time required}
// turn on blue for value 100 for one sec
analogWrite(LED_Blue, 100);
Serial.println("PMW Blue ON"); // output testing
} }
// State 2 PWM Light pattern
if (lightState == 2) { //if button set is 2
currentMillis = millis(); // set current time to time on arduino board
if (currentMillis - previousMillis < 1000) {
// if time is less than one second
analogWrite(LED_Green, 0); //reset LED
analogWrite(LED_Red, I); //RED PWM value of I
analogWrite(LED_Blue, 0); //reset LED
time = currentMillis-previousMillis; //time variable is set to time since start of state 2
I = pow(((time)/40), (1.732)); //Value of I according to nonlinear equation using time variable
// output testing
Serial.println("PMW RED ON"); // output testing
Serial.println(LED_Red); // output testing
}
if (currentMillis - previousMillis < 2000 && currentMillis - previousMillis > 1000) {
// if time is less than 2 sec and greater than 1 sec
analogWrite(LED_Blue, 0); //Make sure Blue is 0
analogWrite(LED_Green, I); //Green PWM value of I
analogWrite(LED_Red, I); //Red PWM value of I
time = currentMillis-previousMillis-1000; // time evaluated for equation subtracts previous second
I = pow(((time)/40), (1.732)); //Value of I according to nonlinear equation using time variable
// output testing
Serial.println("PMW Red ON");
Serial.println("PMW Green ON");
Serial.println(I);
}
if (currentMillis - previousMillis < 3000 && currentMillis - previousMillis > 2000) {
// if time is less than 3 sec and greater than 2 sec
analogWrite(LED_Red, I); //Red PWM value of I
analogWrite(LED_Blue, I); //Blue PWM value of I
analogWrite(LED_Green, I); //Green PWM value of I
time = currentMillis-previousMillis-2000; // time evaluated for equation subtracts previous two seconds
I = pow(((time)/40), (1.732)); //Value of I according to nonlinear equation using time variable
// output testing
Serial.println("PMW Red ON");
Serial.println("PMW Green ON");
Serial.println("PMW Blue ON");
Serial.println(I);
}
// sets prev time to current time if arduino time is over 3 second
if (currentMillis - previousMillis > 3000){
previousMillis = currentMillis; //sets time of prev variavle if over 3000 miliseconds
}
}
/////// END ///////
buttonState = val;
}