replacing delay with mills()

I currently have a chasing LED effect working using delay(), i would now like to do this without using the delay() function, however after looking at the blink without delay examples and many others i'm stuck because my delays are between do and while statements, where as all the examples are to change one LED high/low, could someone give me an example of how to adjust my code to not use the delay() and still keep the functionality.

I should add the reason for ditching delay() is i want to control the speed using a pot and so reading this value whilst using the delay() is very slow.

byte led1 = 3;
byte led2 = 9;
byte led3 = 10;
byte led4 = 11;
int x = 0;
int y = 0;
int steps = 1; //change if needed, defines the steps between 0 and 255, a lower number is smoother
//make sure the variable "steps" is a factor of 255; any of the below numbers
//factors of 255 are : 1,3,5,15,17,51,85,255
//sorry for a lot of notes, but remember to change variable "delaytime" according to variable "steps"

//delay is in milliseconds for below
int delaytime; //change if needed, delay between increments of PWM
//850 milliseconds is on-off/off-on time, in 17 step increments of brightness
int delaytime2; //change if needed, delay between switching of leds


void setup (){
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (led4, OUTPUT);

/*  do {
    x = x + steps;
    analogWrite (led1, x);
    analogWrite (led2, x);
    delay (delaytime);
  }
  while (x != 255);*/
}
void loop (){
   
  dspeed();
  
  
  y = 0;
  x = 255;
  delay (delaytime);
  do{
    y = y + steps;
    x = x - steps;
    analogWrite (led3, y);
    analogWrite (led1, x);
    delay (delaytime);
  }
  while (y != 255 && x != 0);
  delay (delaytime2);
  y = 0;
  x = 255;
  
  
  do{
    y = y + steps;
    x = x - steps;
    analogWrite (led1, y);
    analogWrite (led3, x);
    delay (delaytime);
  }
  while (y != 255 && x != 0);
  delay (delaytime2);
  y = 0;
  x = 255;
  
}

You need to completely rethink things - delays within a complex structure of code are implicitly using state in possibly quite complex ways.

When you code using millis() rather than delay() all state has to be explicit, so you'll have to add various state variables and manage the transitions between states - this means constructing state machine(s) in the code, rather than using control structures like loops.

So have a good read about state machines.

thats actually exactly what I’ve just been doing :-). Although i’m still struggling, i still cade get the intensity of the led to fade, i can get it to change between on and off within a specified time but the fade doesn’t work. Do you know why this is?

byte ledPin=9;

void setup()
{
  pinMode(9,OUTPUT);
}

void loop()
{
  static int state = 2; 
  static unsigned long ts;  

  switch(state)
  {
  case 1:

    break;

  case 2:
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue ++) { 
      analogWrite(ledPin, fadeValue);      
    }   

    ts = millis(); 
    state = 3;
    break;

  case 3:
    if(millis() > ts + 1000)
    {
      state = 4;
    }
    break;

  case 4:
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue --) { 
      analogWrite(ledPin, fadeValue);    
    }     
    ts = millis();
    state=5;
    break;

  case 5:
    if (millis() > ts + 1000)
    {
      state = 2;
    }

    break; 

  }
}

I hope the sketch in the first post of this Thread will help to explain things.

In your code each case statement performs the whole of (or a large part of) an action (as far as I can see). So it can only do one action at a time.

In the code I have linked to one small piece of each action is carried out by each iteration of loop(). That gives the appearance that all of the actions are happening in parallel.

...R

I'm still not following how this will help me fade a value, i can see how it will 'pause/hold' then do something else......is there a library that will do it?

joshbailz:
I’m still not following how this will help me fade a value, i can see how it will ‘pause/hold’ then do something else…is there a library that will do it?

Think about what a fade is - it is mostly waiting around, and only a very, very brief update and bounds check.

just found another way of doing things......

float something = millis()/delayspeed; int value = 128.0 + 128 * sin( something * 2.0 * PI ); analogWrite(led1,value);

by using this i am able to dim the LED using a sine wave :-)