Messing Around With Interrupts:
Functionality:
There are two different states that the system can be put into. These states are controlled by an interrupt connected to the switch. When the switch is high, the system goes into adjustment mode where the user can use the potentiometer to adjust the brightness of the LED. This adjustment is only possible while in this state. When the switch is low, the system returns to its normal state where there is a blinking mode and solid mode. These modes are controlled by a second interrupt tied to the button. While in the blinking state, the LED’s flash individually for 0.5s each in a sequence of red to yellow to green and back to red. In the solid state, all of the LED’s remain on. The brightness of the LED’s in this mode is also the same as that set in the previous adjustment state.
Code:
/* Interrupt Assignment * Judson Kyle * judsonk * 01/31/2021 */ #define POT_PIN A0 #define SWITCH 3 #define BUTTON 2 #define RED_LED 11 #define YELLOW_LED 10 #define GREEN_LED 9 #define RED 0 #define YELLOW 1 #define GREEN 2 volatile bool ADJUST_STATE = false; volatile bool BLINK = false; int potVal = 0; double ledBrightness; int currentLED = 0; int waitTime = 500; //ms long long startTime; int buttonDebounceTime = 300; long long buttonDebounceStart = 0; volatile bool BUTTON_DEBOUNCE = false; bool BUTTON_PREV_DEBOUNCE = false; void setup() { pinMode(POT_PIN, INPUT); pinMode(SWITCH, INPUT); //Interrupts attachInterrupt(digitalPinToInterrupt(BUTTON), handleButton, RISING); //LED's pinMode(RED_LED, OUTPUT); pinMode(YELLOW_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT); Serial.begin(9600); } void handleButton() { if (!BUTTON_DEBOUNCE) { BUTTON_DEBOUNCE = true; BLINK = !BLINK; } } void loop() { //Read switch state ADJUST_STATE = digitalRead(SWITCH); Serial.println(ADJUST_STATE); // Serial.print("DEBOUNCE: \t"); // Serial.print(DEBOUNCE); // // Serial.print("\tdebounceStart: \t"); // Serial.print((int)debounceStart); // // Serial.print("\tTime Elapsed: \t"); // Serial.println(millis() - (int)debounceStart); //Process switch logic if (ADJUST_STATE) { potVal = analogRead(POT_PIN); ledBrightness = (256*potVal)/1024; analogWrite(RED_LED, (int)ledBrightness); analogWrite(YELLOW_LED, (int)ledBrightness); analogWrite(GREEN_LED, (int)ledBrightness); } //Change brightness of LED based off potentiometer switch when not in blink mode if (BLINK) { if (millis() - startTime > waitTime) { startTime = millis(); currentLED++; if (currentLED >= 3) { currentLED = 0; } } switch(currentLED) { case RED: analogWrite(RED_LED, (int)ledBrightness); analogWrite(YELLOW_LED, LOW); analogWrite(GREEN_LED, LOW); break; case YELLOW: analogWrite(RED_LED, LOW); analogWrite(YELLOW_LED, (int)ledBrightness); analogWrite(GREEN_LED, LOW); break; case GREEN: analogWrite(RED_LED, LOW); analogWrite(YELLOW_LED, LOW); analogWrite(GREEN_LED, (int)ledBrightness); break; default: break; } } else { //If not in blinking mode make all LED's light up analogWrite(RED_LED, (int)ledBrightness); analogWrite(YELLOW_LED, (int)ledBrightness); analogWrite(GREEN_LED, (int)ledBrightness); } //Handle button debounce if (BUTTON_DEBOUNCE && !BUTTON_PREV_DEBOUNCE) { buttonDebounceStart = millis(); } if (abs(buttonDebounceStart - millis()) > buttonDebounceTime) { BUTTON_DEBOUNCE = false; } BUTTON_PREV_DEBOUNCE = BUTTON_DEBOUNCE; }