Hello everybody and sorry for my english
I'm trying to solve the first exercise at page 46 of the Eartshine Design Manual for Arduino.
Here's the text of the exercise:
Get the LED始s at BOTH ends of the strip to start as on, then to both move towards each other, appear then move back to the end.
Unfortunatly for now it doesn't work. The leds at both ends light up, they move towards each other, then they go back when they meet but when they both reach the end, they "disappear" and no other led lights up. It's like they do just 1 cicle. The loop doesn't keep going on.
Where's the problem?
Here's the code
// BOUNCING LED
// Create array for LED pins
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10,
11, 12, 13};
int ledDelay; // delay between changes
int direction = 1;
int currentLED = 0;
int counterDirection = -1;
int secondLED = 9;
unsigned long changeTime;
int potPin = 2; // select the input pin for the potentiometer
void setup() {
// set all pins to output
for (int x=0; x<10; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
// read the value from the pot
ledDelay = analogRead(potPin);
// 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);
// turn on the second current LED
digitalWrite (ledPin[secondLED], HIGH);
// increment by the direction value
currentLED += direction;
secondLED += counterDirection;
// change direction if we reach the end
if (currentLED == 4) {direction = -1;}
if (currentLED == 0) {direction = 1;}
if (secondLED == 9) {direction = -1;}
if (secondLED == 5) {direction = 1;}
}
I attach the schemof the circuit too.
Thanks everyone and sorry for my english