Hello, I have a question about programming an Arduino uno.
I followed a tutorial which had an array in the software to control 10 LED's.
Now I want to make is:
LED 1 and 10 start on, move to the middle and bounce off then head back to where they started.
//LED Chase Effect
// Create array for LED pins
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay(65); // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
void setup() {
// set all pins to output
for (int x=0; x<10; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
// if it has been ledDelay ms since last change
if ((millis() - changeTime) > ledDelay) {
changeLED();
changeTime = millis();
}
}
void changeLED() {
// turn off all LED's
for (int x=0; x<10; x++) {
digitalWrite(ledPin[x], LOW);
}
// turn on the current LED
digitalWrite(ledPin[currentLED], HIGH);
// increment by the direction value
currentLED += direction;
// change direction if we reach the end
if (currentLED == 9) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
// LED Chase Effect
// Create array for LED pins
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay(1000); // delay between changes
int direction = 1;
int currentLED = 0;
int currentLED2 = 10;
unsigned long changeTime;
void setup() {
// set all pins to output
for (int x=0; x<10; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
// if it has been ledDelay ms since last change
if ((millis() - changeTime) > ledDelay) {
changeLED();
changeTime = millis();
}
}
void changeLED() {
// turn off all LED's
for (int x=0; x<10; x++) {
digitalWrite(ledPin[x], LOW);
}
// turn on the current LED for the first column
digitalWrite(ledPin[currentLED], HIGH);
//turn on the current LED for the second column
digitalWrite(ledPin[currentLED2], HIGH);
// increment by the direction value for the first column
currentLED += direction;
currentLED2 += direction;
// change direction if we reach the end the first column
if (currentLED == 0) {direction = 1;}
if (currentLED == 4) {direction = -1;}
// change direction if we reach the end the second colom
if (currentLED2 == 10) {direction = -1;}
if (currentLED2 == 6) {direction = 1;}
}
The first is the code from the tutorial, the second is mine.
I followed some things about arrays and such but I don't quite understand this very wel, could someone explain why the LED's won't start on both sides and then shift to the middle.
I can't figure out how to do this.