Arduino newb here. I am trying to create a LED chaser on my Uno that incrementally changes each LED to the high state, then, after the final LED is activated at the "top," decrements each LED to the low state in reverse order.
I can accomplish this by fully writing out instructions for each LED in the loop, but I want to do it in a more elegant fashion to conserve storage space and significantly increase the number of LEDs I can control without tons of code (next step is to send each available output to separate relay modules).
Here is what I have right now. Test version has 7 LEDs. No problems getting the LEDs to build incrementally at variable speed using the pot. When I get to the top, I'm not sure what to do next, as that's where decrement commands cause things get squirrelly.
Thanks for any help you can offer!
int const potPin = A0; // select input pin for potentiometer
byte ledPin[] = {1, 2, 3, 4, 5, 6, 7}; // create array for LED pins to output
int ledDelay; // delay between changes
int direction = 1; // value used to increment to next LED
int currentLED = 0; // current led value will start at 0
unsigned long changeTime; // changeTime variable assigned long integer format
void setup() { // runs once on boot
for (int x=0; x<7; x++) { // set 'x' to 0, less than 7, and increment
pinMode(ledPin[x], OUTPUT); // set all ledPin to be outputs
}
changeTime = millis();
}
void loop() { // runs on repeat
ledDelay = analogRead(potPin); // read value from potentiometer
if ((millis() - changeTime) > ledDelay) { // changeTime val > than ledDelay? Yes then
changeLED(); // go to changeLED routine
changeTime = millis(); // Delay
}
}
void changeLED() {
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
if (currentLED == 0) { // starting with initial ledPin 1
direction = 1;
}
}
You are using pin 1 as an led output. This is used by the coms port for uploading sketches so it is best not to use it.
You need to check when the current LED is greater than the number that you have and then switch the direction value to -1, I don't see you doing that.
Thanks, Mike. Do you know what that bit of code should look like? When I tried what you suggested, it would turn off LED 6 only, while all others remain lit.
When I tried what you suggested, it would turn off LED 6 only, while all others remain lit.
The way this forum works is that you post your latest version of the code and we tell you where you are going wrong.
My mistake, Mike. Here is the last iteration of changeLED(). Still trying to get my head around how some of the sequencing works. Only the first LED on the board will light up now.
void changeLED() {
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
if (currentLED == 0) {
direction = 1;
}
if (currentLED == 7) direction = 0;
digitalWrite(ledPin[currentLED], LOW);
currentLED -= direction;
direction = -1;
}
You have not got the hang of it. Look at your code:-
void changeLED() {
digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED
currentLED += direction; // increment by the direction value
if (currentLED == 0) {
direction = 1;
}
if (currentLED == 7) direction = 0;
// this code is always run
digitalWrite(ledPin[currentLED], LOW);
currentLED -= direction;
direction = -1;
}
You always change the direction to -1 no matter what happens, I think you want to do this:-
void changeLED() {
// turn on the current LED
digitalWrite(ledPin[currentLED], HIGH);
// turn off the last LED
digitalWrite(ledPin[currentLED - direction], LOW);
currentLED += direction; // increment by the direction value
// adjust the direction if needed for next time
if (currentLED < 0) {
direction = 1;
currentLED = 1;
}
if (currentLED > 7) {
direction = -1;
currentLED = 6;
}
}
Not tested but see how it goes.
V cool. That does make a nice two-way chaser. This is the effect I am trying to achieve, though (see attached image).

void
changeLED()
{
II
turn
off
all
LED's
for
(int
x=Oj
x<10j
x++)
{
digitaIWrite(ledPin[x],
LOW)j
}
II
turn
on
the
current
LED
digitaIWrite(ledPin[currentLED],
HIGH)j
II
increment by
the
direction
value
currentLED
+=
directionj
II
change
direction
if
we
reach
the
end
if
(currentLED
==
9)
{direction
-lj}
if
(currentLED
==
0)
{direction
=
lj}
}
Welcome brentreader.
Learn to format your code press the combination.
Or click Tools/Auto Format.
Suggest you follow this type of code 'look' as it reminds you what is attached to the 'if' statement.
if (currentLED == 7)
{
direction = 0;
}
The bottom LED is always on.

.