[solved] Potvalue-dependent delay doesnt work

im using a potentiometers stored value in order to control the delay for the leds to turn on one by one with analogwrite (planning to make them turn on and off back and forth but i wanna try and get this fixed first)

but no matter what values i map for potval all the leds turn on in succession pretty much instantly

i cant tell if im just really stupid or not but i have no idea what the problem is

int leds[] = {3, 5, 6, 9, 10, 11};
int pot = A0;
int potVal;
int delayTime = potVal;

void setup()
{
   for (int i = 0; i < 6; i++) {
    pinMode(leds[i], OUTPUT);
  } 
  pinMode(pot, INPUT);
}

void loop()
{
  
  for (int l = 0; l < 6; l++) {
    for (int i = 0; i < 256; i++) {
      potVal = analogRead(pot);
      potVal = map(potVal, 0, 1023, 100, 1000);
      analogWrite(leds[l], i);  
      delay(delayTime);
    } 
  }
}

Globally declared variables are set to 0 if no value is specified. The value of potVal will be 0. So when it is declared delayTime given a value of 0. The delayTime value will not follow the value of potVal, you must assign the value of potVal to delaytrime in the code (for loop).

The analogRead() function outputs 0 through 255, not 0 - 1000.

You could do:

      potVal = analogRead(pot);
      delayTime = map(potVal, 0, 1023, 100, 255);
      analogWrite(leds[l], i);  
      delay(delayTime);

Welcome to the forum

You never change the value of delayTime in your sketch so why would the delay ever change ?

int delayTime = potVal;

Does not bind the value of delayTime to potValue so once the value is set it never changes

Hi,
Sorry. I didn't understand this statement.

As far as I know in Arduino UNO the function
analogRead() returns values from 0 to 1023.

Sorry, I meant that analogWrite() outputs 0 - 255, not 0 - 100. I apologize for my confusion.

1 Like

thanks for the clarification

thanks

Have you corrected the sketch ?

yeah it works well now

If your question is answered, it is polite to mark your post solved so that other members will not open the post to help only to find it solved.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.