Continually updating a variable without repeating code

Let's say I have a series of 20 LEDs in series that light up one at a time in order. Now, let's say that say that I am using a potentiometer to control the speed at which this occurs.

I currently have the variable SPEED = analogRead(Potentiometer) continually updating the delay time as the LEDs light up in sequence:

digitalWrite(led1, HIGH); SPEED = analogRead(Potentiometer) ; delay(SPEED);

digitalWrite(led1, LOW); digitalWrite(led2, HIGH); SPEED = analogRead(Potentiometer) ; delay(SPEED);

digitalWrite(led1, LOW); digitalWrite(led2, HIGH); SPEED = analogRead(Potentiometer) ; delay(SPEED);

digitalWrite(led2, LOW); digitalWrite(led3, HIGH); SPEED = analogRead(Potentiometer) ; delay(SPEED);

etc.

But I am repeating this code "SPEED = analogRead(Potentiometer)" many times during this process. Is there any way to avoid this? Parallel processing would theoretically solve it, but I'm not sure Arduino is capable of this. What is the best solution to this issue?

Hello and welcome,

See the Blink Without Delay example

Does it need to be able to vary as it goes thru the 20, or can it just be read once at the start of the sequence and then not changed until the sequence starts again?

The snippet of code you have posted in your original Post would work just as well if you remove all but the first

SPEED = analogRead(Potentiometer) ;

In future please post the entire program and use the code button ‘</>’ as that makes it much easier to read.

It is not a good idea to use delay() in anything but a short test program. See how to manage timing using millis() in the demo several things at a time.

If you find yourself repeating the same code it is likely that you need to use a FOR or WHILE loop or put the code into a function so it can be called several times without needing to write it several times.

…R

If you want it to change with each LED that lights up, then there’s no avoiding reading the pot between each one.

If you just want to reduce the amount of typing you could use a for loop to light up the LEDs.

Since you didn’t show all the code, I’m just guessing at the pin numbers for the LEDs

// array holding the pin numbers for the different LEDs.

byte ledPins[5] = {2, 3, 6, 7, 9};  // this goes at global scope before setup


// This code in your loop function where you are lighting things up.

for (int i = 0; i < 5; i++){
     digitalWrite(ledPins[i] , HIGH);
     SPEED = analogRead(Potentiometer);
     delay(SPEED);
     digitalWrite(ledPins[i] , LOW);
}

Need to learn about using arrays too.

byte ledArray[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,}; // ledArray[0] to [16]
byte pot = A5;
int potvalue;
byte x;

void setup(){
for (x=0; x<17; x=x+1){
pinMode (ledArray[x], OUTPUT);
}
Serial.begin(115200); // for debug, just in case
}

void loop(){
potValue = analogRead(A5);
for (x=0; x<17; x=x+1){
  digitalWrite (ledArray[x], HIGH);
  delay(potValue);
  digitalWrite(ledArray[x], LOW);
  }
}

So what Delta_G said ;), but typed faster than me 8)

Thank you everyone, for the helpful responses.