#include <Servo.h> // Included as allowed (unused in this example)
const int btnPin1 = 22;
const int btnPin2 = 23;
const int btnPin3 = 24;
const int ledPin1 = 44; // PWM-capable pin
const int ledPin2 = 45; // PWM-capable pin
const int ledPin3 = 46; // PWM-capable pin
const int potPin1 = A0;
const int potPin2 = A1;
const int potPin3 = A2;
const unsigned long stableDelay = 2000; // 2 seconds stable period
const unsigned long debounceDelay = 50; // 50 ms debounce delay
// To track the last time each LED was updated
unsigned long lastUpdate1 = 0;
unsigned long lastUpdate2 = 0;
unsigned long lastUpdate3 = 0;
// To save the last button states for edge detection
int lastButtonState1 = HIGH;
int lastButtonState2 = HIGH;
int lastButtonState3 = HIGH;
void setup() {
// Initialize LED pins as outputs; start with LEDs off
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
analogWrite(ledPin1, 0);
analogWrite(ledPin2, 0);
analogWrite(ledPin3, 0);
// Initialize button pins as INPUT_PULLUP
pinMode(btnPin1, INPUT_PULLUP);
pinMode(btnPin2, INPUT_PULLUP);
pinMode(btnPin3, INPUT_PULLUP);
}
void loop() {
unsigned long currentTime = millis();
// Process Button 1 for led1
int reading1 = digitalRead(btnPin1);
if (reading1 == LOW && lastButtonState1 == HIGH) {
// Wait a little to debounce
delay(debounceDelay);
// Only update if 2 seconds have passed since last update for led1
if (currentTime - lastUpdate1 >= stableDelay) {
int potValue = analogRead(potPin1);
int brightness = map(potValue, 0, 1023, 0, 255);
analogWrite(ledPin1, brightness);
lastUpdate1 = millis();
}
}
lastButtonState1 = reading1;
// Process Button 2 for led2
int reading2 = digitalRead(btnPin2);
if (reading2 == LOW && lastButtonState2 == HIGH) {
delay(debounceDelay);
if (currentTime - lastUpdate2 >= stableDelay) {
int potValue = analogRead(potPin2);
int brightness = map(potValue, 0, 1023, 0, 255);
analogWrite(ledPin2, brightness);
lastUpdate2 = millis();
}
}
lastButtonState2 = reading2;
// Process Button 3 for led3
int reading3 = digitalRead(btnPin3);
if (reading3 == LOW && lastButtonState3 == HIGH) {
delay(debounceDelay);
if (currentTime - lastUpdate3 >= stableDelay) {
int potValue = analogRead(potPin3);
int brightness = map(potValue, 0, 1023, 0, 255);
analogWrite(ledPin3, brightness);
lastUpdate3 = millis();
}
}
lastButtonState3 = reading3;
// No further action is needed if no button is pressed.
// The current brightness levels continue displayed.
}