//step 1, make one light light up without a button
//variables
//PINS
#define redled 8
#define blueled 12
#define btn A0 //pin that reads the button
//last time red was high
unsigned long lastTimeRedHigh = 0; //this declaration needs to go outside the loop function. it is declared once.
//last time blue was high
unsigned long lastTimeBlueHigh = 0;
//last time blue was high
unsigned long lastTimeBlueDelayed = 0;
//red blink time
const int redBlinkTime = 2000; //changes the frequency that it blinks
//blue blink time
const int blueBlinkTime = 700; //changes the frequency that it blinks
//blue on duration time
const int blueOnDurationTime = 400; //change how long the light is on before turning off
int lastbtnstate = HIGH;
void setup() {
//setup pins
pinMode(redled, OUTPUT);
pinMode(blueled, OUTPUT);
pinMode(btn, INPUT_PULLUP); //sets the pin to input
Serial.begin(9600);
}
void loop() {
int btnstate = digitalRead(btn);
Serial.println(btnstate);
//current time variable
unsigned long currentTime = millis();
if (btnstate == LOW) {
//blink red light
if (currentTime - lastTimeRedHigh >= redBlinkTime) { //checks if it is time to turn on the Red led
// Serial.print(currentTime - lastTimeRedHigh);
// Serial.print(" ");
lastTimeRedHigh = currentTime;
digitalWrite(redled, HIGH);
// Serial.print(currentTime);
// Serial.print(" red ");
// Serial.println(lastTimeRedHigh);
}
else {
digitalWrite(redled, LOW);
}
//blink blue light
if (currentTime - lastTimeBlueHigh >= blueBlinkTime) { //checks if it is time to turn on the Red led
/*
Serial.print("blue: difference= ");
Serial.print(currentTime - lastTimeBlueHigh);
Serial.print(" ");
Serial.print(" current time= ");
Serial.print(currentTime);
Serial.print(" last time= ");
Serial.println(lastTimeBlueHigh);
*/
lastTimeBlueHigh = currentTime; //increments the time
lastTimeBlueDelayed = currentTime; //increments the time
digitalWrite(blueled, HIGH); //turns on the blue led
}
else {
if (currentTime - lastTimeBlueDelayed >= blueOnDurationTime) {
digitalWrite(blueled, LOW); //turns off the blue led
}
} lastbtnstate = btnstate;
}
else { //if the difference is larger than the blue on duration time, then the blue led turns off
digitalWrite(blueled, LOW);
digitalWrite(redled, LOW);
}
}