Mechatronics LAB
Published

Arduino Workshop-Interactive LED Chase Effect

Arduino Workshop-Interactive LED Chase Effect

BeginnerFull instructions provided1 hour6,423
Arduino Workshop-Interactive LED Chase Effect

Things used in this project

Story

Read more

Schematics

Arduino Workshop-Interactive LED Chase Effect

Code

Code snippet #1

Arduino
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins
int ledDelay; // Delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2; // Select the input pin for the potentiometer

void setup() {
  for (int x = 0; x < 10; x++) { // Set all pins to output
    pinMode(ledPin[x], OUTPUT);
  }
  changeTime = millis();
}

void loop() {
  ledDelay = analogRead(potPin); // Read the value from the potentiometer
  if ((millis() - changeTime) > ledDelay) { // If it has been ledDelay ms since last change
    changeLED();
    changeTime = millis();
  }
}

void changeLED() {
  for (int x = 0; x < 10; x++) { // Turn off all LEDs
    digitalWrite(ledPin[x], LOW);
  }
  digitalWrite(ledPin[currentLED], HIGH); // Turn on the current LED
  currentLED += direction; // Increment by the direction value
  
  // Change direction if we reach the end
  if (currentLED == 9) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.