noob question

im working through a beginning arduino book. i am stuck on an exercise to modify a code. the original code controlled an array of leds sweeping from one end to the other, the speed being set by a pot. my task was to change the programme so that each time it sweeps through one less led is lit. my code is:

byte ledpin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay;
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2;
int led = 9;

void setup() {
  for (int x=0; x<10; x++) {
    pinMode (ledpin[x], OUTPUT); }
    changeTime = millis();
  }
 void loop() {
   ledDelay = analogRead(potPin);
   if ((millis() - changeTime) > ledDelay) {
     changeLED();
     changeTime = millis();
   }
 }
 void changeLED() {
   for (int x=0; x<10; x++) {
     digitalWrite(ledpin[x], LOW);
   }
     digitalWrite (ledpin[currentLED], HIGH);
     currentLED += direction;
     
     if (currentLED == led) {direction = -1; led = led -1;}
     if (currentLED == 0) {direction = 1;}
     
     }

this code compiles fine and i upload it to arduino and runs as normal untill the second pass when it stops working as it should. before my modification the code read at the bottom:

if (currentLED == 9) {direction = -1;}
     if (currentLED == 0) {direction = 1;}

my modification was to reduce the number “9” each pass by naming and int led and reducing it if currentLED = 9. if i add 1 to the led the code works as i expect but when i try taking 1 away from 9 it does not work, any ideas where i am going wrong? thanks calum

One less led per iteration means a maximum value is decreasing.
rewrite the code so that instead of a hard coded 10 it uses a variable called max that has the initial value 10

Then create some loop around the relevamt block in wwhich max is assigned 9,8,7,6,5,4,3,2,1, (Houston, we have a lift off :wink: