const int RED_LED = 13;
const int GREEN_LED = 12;
const int BLUE_LED = 11;
const int BUTTON_ONE = 2;
const int BUTTON_TWO = 3;
const int POTENTIOMETER = A0;
volatile bool modeOneActive = false;
volatile bool modeTwoActive = false;
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(BUTTON_ONE, INPUT_PULLUP);
pinMode(BUTTON_TWO, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_ONE), activateModeOne, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_TWO), activateModeTwo, FALLING);
Serial.begin(9600);
}
void loop() {
if (modeOneActive) {
handleModeOne();
} else if (modeTwoActive) {
handleModeTwo();
} else {
turnOffLEDs();
Serial.println("No Mode Active");
}
delay(500);
}
void activateModeOne() {
modeOneActive = true;
modeTwoActive = false;
}
void activateModeTwo() {
modeTwoActive = true;
modeOneActive = false;
}
void handleModeOne() {
int potValue = analogRead(POTENTIOMETER);
Serial.print("Mode One - Pot Value: ");
Serial.println(potValue);
if (potValue < 100) {
turnOffLEDs();
} else if (potValue < 340) {
blinkLED(RED_LED);
} else if (potValue < 680) {
blinkLED(RED_LED);
blinkLED(GREEN_LED);
} else {
blinkLED(RED_LED);
blinkLED(GREEN_LED);
blinkLED(BLUE_LED);
}
}
void handleModeTwo() {
int potValue = analogRead(POTENTIOMETER);
int level = map(potValue, 0, 1023, 0, 7);
digitalWrite(RED_LED, bitRead(level, 0));
digitalWrite(GREEN_LED, bitRead(level, 1));
digitalWrite(BLUE_LED, bitRead(level, 2));
Serial.print("Mode Two - LEDs: R=");
Serial.print(bitRead(level, 0));
Serial.print(" G=");
Serial.print(bitRead(level, 1));
Serial.print(" B=");
Serial.println(bitRead(level, 2));
}
void blinkLED(int led) {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void turnOffLEDs() {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
}