#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
#define RED_LED 5
#define GREEN_LED 6
#define BLUE_LED 9
#define NO_LED 3
int counter = 0;
int ledNumber = 1;
int buttonState = HIGH; // The current debounced state of the switch
int lastReading = HIGH; // The previous raw reading from the switch pin
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
unsigned long lastDebounceTime = 0; // The last time the output pin was toggled
const unsigned long debounceDelay = 50; // The debounce time in ms (adjust as needed)
void setup() {
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter = counter + 5; // Clockwise
}
if (dtValue == LOW) {
counter = counter - 5; // Counterclockwise
}
Serial.println(counter);
}
// Get the counter value, disabling interrupts.
// This makes sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void loadCounter(int num) {
noInterrupts();
if (num == 1) {
counter = redValue;
}
else if (num == 2) {
counter = greenValue;
}
else if (num == 3) {
counter = blueValue;
}
counter = constrain(counter, 0, 255);
interrupts();
}
void flashLed(int num) {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
for (int i = 0; i < 2; i++) {
if (num == 1) {
digitalWrite(RED_LED, HIGH);
}
else if (num == 2) {
digitalWrite(GREEN_LED, HIGH);
}
else if (num == 3) {
digitalWrite(BLUE_LED, HIGH);
}
delay(100);
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(BLUE_LED, LOW);
delay(100);
}
analogWrite(RED_LED, redValue);
analogWrite(GREEN_LED, greenValue);
analogWrite(BLUE_LED, blueValue);
}
void loop() {
int reading = digitalRead(ENCODER_SW);
// If the reading has changed (due to noise or pressing), reset the debouncing timer
if (reading != lastReading) {
lastDebounceTime = millis();
}
// If the state has been stable for longer than the debounce delay
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed from the debounced state
if (reading != buttonState) {
buttonState = reading;
// Only trigger the action if the new button state is LOW (pressed)
if (buttonState == LOW) {
ledNumber = ledNumber + 1;
if (ledNumber > NO_LED) {
ledNumber = 1;
}
loadCounter(ledNumber); // Load the NEW LED's value after switching
flashLed(ledNumber);
Serial.print("ledNumber: ");
Serial.println(ledNumber);
}
}
}
// Save the reading. Next time through the loop it'll be the lastReading
lastReading = reading;
if (ledNumber == 1) {
redValue = constrain(getCounter(), 0, 255);
analogWrite(RED_LED, redValue);
}
else if (ledNumber == 2) {
greenValue = constrain(getCounter(), 0, 255);
analogWrite(GREEN_LED, greenValue);
}
else if (ledNumber == 3) {
blueValue = constrain(getCounter(), 0, 255);
analogWrite(BLUE_LED, blueValue);
}
}