running light with potentiometer and push-button switch

hi, my teacher gave us project that uses 10 led light to be used as running light(or knight rider just shaped as circle) that loops and stops when the push button was pushed, and the speed of the running light can be controlled with the use of potentiometer. here’s what i have so far:

int potPin = A2; //potentiometer
int ledPin[]={13,12,11,10,9,8,7,6,5,4,};
int val = 0;

void setup() {
  for(int i=0;i<10;i++);
  {
    pinMode(ledPin[i],OUTPUT)
  }
  //Serial.begin(9600);
}

void loop() {
  //val = analogRead(potPin);
  
  for(int i =0; i<10; i++)
  {
    digitalWrite(ledPins[i],HIGH);
    delay(80);  //delay(val)
    digitalWrite(ledPins[i],LOW);
  }

  for(int i =10; i>0; i--)
  {
    digitalWrite(ledPins[i],HIGH);
    delay(80); //delay(val)
    digitalWrite(ledPins[i],LOW);
  }
}

i have very little knowledge about this, can you please help me understand the codes that you used when posting

What exactly do you Need help with?

let me correct myself, here’s what i have so far:

int ledPin[]= {13,12,11,10,9,8,7,6,5,4,};
int buttonPin= 2;
//int val = 0;
int buttonState = 0;

void setup() {
  pinMode(buttonPin,INPUT);
  for(int i=0;i<10;i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
}

void loop() {
  buttonState = digitalRead(buttonPin);
  for(int i =0; i<10; i++)
  {
    digitalWrite(ledPin[i],HIGH);
    delay(100);  //delay(val)
    digitalWrite(ledPin[i],LOW);
  }
}

my problem is how to insert the code for potentiometer to control the speed and the button switch to stop the led at some point

With delay()'s in the code it’s going to be hard. You can’t check a button in a delay() so whatever delay() you use will reduce the response time.

So two steps to get to the point where you can implement a button.

First, get rid of the for loop. The loop is already looping so you can use that.

After you done that, get rid of the delay(). See Blink without delay how to do that.

Now you can often check the button without interfering with the animation of the leds you can try to implement a start/stop button.

here’s the revised code that I used that is using the potentiometer and so far it’s still using the for loop. This time I disregarded the button switch:

int potPin = A3; //potentiometer
int ledPin[]={13,12,11,10,9,8,7,6,5,4,};
int val = 0;

void setup() {
  pinMode(potPin, INPUT);
  for(int i=0;i<10;i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }
 }

void loop() {
  for(int i=0; i<10; i++)
  {
    val = analogRead(potPin);
    digitalWrite(ledPin[i],HIGH);
    delay(val);
    digitalWrite(ledPin[i],LOW);
  }
}

what im thinking is that maybe i could use an ‘if’ and ‘while’ for the push button inside the for loop, the problem is that i still don’t know how to insert that code for the push button

An if is indeed the way to go. But with a delay() the button is still going to be unresponsive for a big part. So take it from us, you want to get rid of the delay() ;)