Using millis

I understand how to use millis to wait a certain amount of time before doing “something”, what i’m struggling with now is i want to do “something” and wait for an amount of time. In my case the time is a random generated number and the something is light a LED at a certain PWM value.

I didn’t write this original code, it appears to be done by mcreefer in this thread. http://forum.arduino.cc/index.php?topic=119686.0

I am trying to rewrite his code to remove the delay’s and as posted above am having trouble. In the for loop it selects a random amount of times to light the led at a certain pwm using diming then after turns the led off for a random delay of dark.

His original code

int ledPin11 = 11;
int burst; //short burst of light
int big;
int srumble;
int brumble;
int mixer;
int diming;
int delaymix;
int dark;
int lightningtimer;
int lightningtime;

void setup (){}
void loop (){
  //30 Min sunrise
  for(int fadeValue = 0; fadeValue < 191; fadeValue +=1)
  {
  analogWrite(ledPin11, fadeValue);
  delay(9411);
  }
  
  //8 hours of solid day light
  analogWrite(ledPin11, 191);
  delay(28800000);
  
  //2 Min cloud cover (Dark)
  for(int fadeValue = 191; fadeValue > 0; fadeValue -=1)
  {
  analogWrite(ledPin11, fadeValue);
  delay(628);
  }

//Random lighting storm of bursts and flickers
unsigned long now = millis();
  lightningtime = 120000;
  lightningtimer = now;

if (now - lightningtimer < lightningtime)
{
  burst = random(10,500); //Short burst of lightning value
  big = random(1000,4000);//long burst of lightning value
  srumble = random(10,40);//lower light lighting value
  brumble = random(10,150);//higher light lightning value
  mixer = random(burst,big);//random pick of short or long bursts of lightning
  dark = random(1000,12000);//random pick of time gap for lightning
  for(int i = 0; i < mixer; i += diming++)
  {
  diming = random(1,150);
  analogWrite(ledPin11, random(srumble,brumble));//Turn on LED for short or long burst
  delay(diming);//random number delay to remain at set brightness value
}
  digitalWrite(ledPin11, LOW);    // turn the LED off
delay(dark);
  }
  //2 min cloud clearing (light)
  for(int fadeValue = 0; fadeValue < 191; fadeValue +=1)
  {
  analogWrite(ledPin11, fadeValue);
  delay(628);
}
  
  //30 min sunset//
  for(int fadeValue = 191; fadeValue > 0; fadeValue -=1)
  {
  analogWrite(ledPin11, fadeValue);
  delay(9411);
}
  
  //Delay to reach the end of psu timer.
  delay(500000);

}

I am most interested in this part

//Random lighting storm of bursts and flickers
unsigned long now = millis();
  lightningtime = 120000;
  lightningtimer = now;

if (now - lightningtimer < lightningtime)
{
  burst = random(10,500); //Short burst of lightning value
  big = random(1000,4000);//long burst of lightning value
  srumble = random(10,40);//lower light lighting value
  brumble = random(10,150);//higher light lightning value
  mixer = random(burst,big);//random pick of short or long bursts of lightning
  dark = random(1000,12000);//random pick of time gap for lightning
  for(int i = 0; i < mixer; i += diming++)
  {
  diming = random(1,150);
  analogWrite(ledPin11, random(srumble,brumble));//Turn on LED for short or long burst
  delay(diming);//random number delay to remain at set brightness value
}
  digitalWrite(ledPin11, LOW);    // turn the LED off
delay(dark);
  }
  //2 min cloud clearing (light)
  for(in

It would be helpful to set things up as a Finite State Machine.

Ok thanks, I'm still new to all things arduino are there any good tutorials you could point me towards?

See these:

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

http://www.gammon.com.au/statemachine

.