Untitled

 avatar
unknown
plain_text
20 days ago
1.7 kB
3
Indexable
// Define pins
const int ledPins[] = {2, 3, 4, 5}; // Digital pins for LEDs
const int buttonPin = 6;           // Push button pin
const int potPin = A0;             // Potentiometer analog pin

// Variables
int buttonState = 0;  // To store the push button state
int potValue = 0;     // To store the potentiometer value
int delayTime = 0;    // To adjust the blink rate

void setup() {
  // Initialize LED pins as OUTPUT
  for (int i = 0; i < 4; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

  pinMode(buttonPin, INPUT_PULLDOWN); // Set button pin with pull-down resistor
  pinMode(potPin, INPUT);             // Set potentiometer pin as INPUT

  // Start Serial Monitor for debugging
  Serial.begin(9600);
}

void loop() {
  // Read the button state
  buttonState = digitalRead(buttonPin);

  // Read the potentiometer value
  potValue = analogRead(potPin);

  // Map the potentiometer value to a delay range (100 ms to 1000 ms)
  delayTime = map(potValue, 0, 1023, 100, 1000);

  // If button is pressed, blink LEDs
  if (buttonState == HIGH) {
    for (int i = 0; i < 4; i++) {
      digitalWrite(ledPins[i], HIGH); // Turn on LED
      delay(delayTime);              // Wait
      digitalWrite(ledPins[i], LOW);  // Turn off LED
      delay(delayTime);              // Wait
    }
  } else {
    // Turn off all LEDs if button is not pressed
    for (int i = 0; i < 4; i++) {
      digitalWrite(ledPins[i], LOW);
    }
  }

  // Debugging information in Serial Monitor
  Serial.print("Potentiometer Value: ");
  Serial.println(potValue);
  Serial.print("Delay Time: ");
  Serial.println(delayTime);
}
Leave a Comment