Delay problem

Hi,I did blinker circuit with rgb led and I’ve used the potentiometer for delay 0-1000.But when the new value of the potentiometer instant isn’t working waiting for the time delay.How do I run an instant.

const int red =9;
const int green =10;
const int blue =11;
int set;
int r,g,b;
int wait;
void setup() {

Serial.begin(9600);
}

void loop() {
set= analogRead(A0);
set=map(set,0,1023,0,1000);
wait=set;
Serial.println(wait);

for(int i=1;i<=3;i++){
r=0;
g=0;
b=255;
analogWrite(red,255-r);
analogWrite(green,255-g);
analogWrite(blue,255-b);

delay(wait);
r=0;
g=0;
b=0;
analogWrite(red,255-r);
analogWrite(green,255-g);
analogWrite(blue,255-b);

delay(wait);
}

for(int i=1;i<=3;i++){
r=255;
g=0;
b=0;
analogWrite(red,255-r);
analogWrite(green,255-g);
analogWrite(blue,255-b);

delay(wait);
r=0;
g=0;
b=0;
analogWrite(red,255-r);
analogWrite(green,255-g);
analogWrite(blue,255-b);

delay(wait);
}

}

You need, really, really need to get used to not using "delay()"

Have a look at the blink without delay example in the IDE

By posting code in code tags not quotes. Get rid of the calls to delay - see blink without delay.

Mark

ozer90:
Hi,I did blinker circuit with rgb led and I’ve used the potentiometer for delay 0-1000.But when the new value of the potentiometer instant isn’t working waiting for the time delay.How do I run an instant.

you can try something like this:

const int red    = 9;
const int green  = 10;
const int blue   = 11;

int r, g, b;



void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  flashLed(map(analogRead(A0), 0, 1023, 0, 1000)); // real-time update the delayTime argument
}

void flashLed(int delayTime)
{
  static unsigned long lastTime = 0;
  if (millis() - lastTime < delayTime)
  {
    r = 0;
    g = 0;
    b = 255;
    analogWrite(red, 255 - r);
    analogWrite(green, 255 - g);
    analogWrite(blue, 255 - b);
  }
  else if (millis() - lastTime < delayTime * 2)
  {
    r = 0;
    g = 0;
    b = 0;
    analogWrite(red, 255 - r);
    analogWrite(green, 255 - g);
    analogWrite(blue, 255 - b);
  }
  else if (millis() - lastTime < delayTime * 3)
  {
    r = 255;
    g = 0;
    b = 0;
    analogWrite(red, 255 - r);
    analogWrite(green, 255 - g);
    analogWrite(blue, 255 - b);
  }
  else if (millis() - lastTime < delayTime * 4)
  {
    r = 0;
    g = 0;
    b = 0;
    analogWrite(red, 255 - r);
    analogWrite(green, 255 - g);
    analogWrite(blue, 255 - b);
  }
  else 
  lastTime = millis();
}

(compiles but not tested)

Yes it works :slight_smile: thank you all for your answers