// Define pin numbers
const int ledPins[] = {33, 25, 26, 27}; // Pins for LEDs
const int potentiometerPin = A0; // Analog pin for potentiometer
const int buttonPin = 2; // Digital pin for button
// Define variables
int ledState[] = {LOW, LOW, LOW, LOW}; // Current LED states (LOW = off, HIGH = on)
int currentLed = 0; // Current LED index
int potValue = 0; // Potentiometer value (0-4095)
int mappedDelay = 0; // Mapped delay value (50-500)
bool clockwise = true; // LED rotation direction
void setup() {
// Initialize pins
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button
}
void loop() {
// Read the potentiometer value
potValue = analogRead(potentiometerPin);
// Map the potentiometer value to the desired delay range
mappedDelay = map(potValue, 0, 4095, 50, 500);
// Turn on the current LED
digitalWrite(ledPins[currentLed], HIGH);
delay(mappedDelay); // LED on time
// Turn off the current LED
digitalWrite(ledPins[currentLed], LOW);
delay(mappedDelay); // LED off time
// Change LED direction if the button is pressed
if (digitalRead(buttonPin) == LOW) { // Button press detected
// Turn off all LEDs for 500ms
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(500);
// Change rotation direction
clockwise = !clockwise;
// Determine the next LED index based on rotation direction
if (clockwise) {
currentLed = (currentLed + 1) % 4; // Move to the next LED in a clockwise direction
} else {
currentLed = (currentLed + 3) % 4; // Move to the next LED in an anti-clockwise direction
}
} else {
// Move to the next LED in the current direction
if (clockwise) {
currentLed = (currentLed + 1) % 4; // Move to the next LED in a clockwise direction
} else {
currentLed = (currentLed + 3) % 4; // Move to the next LED in an anti-clockwise direction
}
}
}