trouble applying blink without delay type code

I’m very new to this programming lark so please be gentle! I am having so much trouble re-applying the blink without delay code to my application…

I have read and re-read the millis and blink without delay pages but I can’t seem to make it do what I want.

It seems like it should be simple… when a variable gets to 1700 (it’'s a servo input) I need to ‘fire’ and IR signal and flash an LED for 1 second.

I can of course do this easily with delay but I can’t figure out how to do it without… below is a snippet of my code with ‘delay’, would anybody be kind enough to help me add the millis ‘delay without delay’ bit?
Thanks in advance,

// a small snippet of my sketch...

/* here there is code for acquiring, processing and outputting servo signals,
 this needs to keep running while the led below flashes for 1 second after chF gets to 1700
 // 'Fire' routine.
 if (chF >= 1700) {                    // if variable chF gets higher than 1700.....:
 for (i = 0; i < n; i++) {             // do this little send routine n times (n is 4 normally)
 My_Sender.IRsendRaw::send(Signal, sizeof(Signal)/sizeof(int), 38); //code to send raw signal to an IR emitter
 delay (10);                               // need this very short delay each time to separate pulses, in practice it's so short it does not seem to effect things
digitalWrite(led, HIGH);       //Turn the LED on
 delay (1000);                         // wait for 1 second
 digitalWrite(led, LOW);        //turn it back off again
 // carry on with the rest of the sketch etc etc.....

I’m giving you just a clock, a piece of paper, and a pencil. Figure out how YOU (not the Arduino) would cook a three minute egg, a two minute piece of toast, and a 10 minute strip of bacon ALL AT THE SAME TIME.

It really isn’t rocket science. You need to record when some event happened - the egg went in the water, the bacon went in the pan, the toast went in the toaster, or the LED quit sending the IR signal.

Periodically, you need to see if it it time to do something else - take the egg out of the water, take the toast out of the toaster, take turn the bacon, take the bacon out of the pan, or turn the LED off. Of course, part of that test is whether it is necessary to do anything. If the egg is no longer in the water, it doesn’t matter what time it is. If the toast has burnt to a crisp, it doesn’t matter what time it is. If you’ve already turned the LED off (after toggling its state often enough), it doesn’t mater what time it is.

Start with a sketch that is as simple as possible and just lights one or two LEDs to simulate the complex parts of your project. When you figure out the timing process it will be easy to integrate the other stuff. At the moment you seem to be trying to integrate the timing (which you haven't mastered) into stuff that is more complex. That's a recipe for sleepless nights and too much coffee.

Take the bit you don't know and do it as simply as you can until you understand it. Avoid all other distractions and complexities.

@PaulS has described the process nicely.


Many thanks both for your good advice! I am perhaps trying to run before I can walk.... I already have gone through lots of coffee so I will go back to basics and do as you suggest. The breakfast analogy has really helped clear it conceptually in my mind!

Thanks again,

May be of some use:

const unsigned long TaskAtime  = 500UL;  //Runs TaskA every 1/2 second
const unsigned long TaskBtime  = 1000UL; //Runs TaskB every 1 second
const unsigned long TaskCtime  = 2000UL; //Runs TaskC every 2 seconds
const unsigned long TaskDtime  = 4000UL; //Runs TaskD every 4 seconds

unsigned long TimeA;                     //Times up, Task A time
unsigned long TimeB;                     //Times up, Task B time
unsigned long TimeC;                     //Times up, Task C time
unsigned long TimeD;                     //Times up, Task D time
// etc.

void setup()
  TimeA = millis();                      //Initailize  
  TimeB = TimeA;                         //Initialize
  TimeC = TimeA;                         //Initialize
  TimeD = TimeA;                         //Initialize

  pinMode(13,OUTPUT);                    //
  pinMode(12,OUTPUT);                    //
  pinMode(11,OUTPUT);                    //
  pinMode(10,OUTPUT);                    //

} //        >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<

void loop()
  unsigned long millisNow = millis();

  //==================  Cooking Eggs
  if (millisNow - TimeA >= TaskAtime) //Is it time to run Task A?
    TimeA = millis();                 //Re-initialize

  //==================  Making Toast
  if (millisNow - TimeB >= TaskBtime) //Is it time to run Task B?
    TimeB = millis();                 //Re-initialize

  //==================   Cooking Bacon
  if (millisNow - TimeC >= TaskCtime) //Is it time to run Task C?
    TimeC = millis();                 //Re-initialize

  //==================   Doing something else
  if (millisNow - TimeD >= TaskDtime) //Is it time to run Task D?
    TimeD = millis();                 //Re-initialize


  //Other stuff goes here

} //        >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<


void TaskA()
  digitalWrite(13,!digitalRead(13));   //Toggle pin 13
  //Other stuff


void TaskB()
  digitalWrite(12,!digitalRead(12));   //Toggle pin 12
  //Other stuff


void TaskC()
  digitalWrite(11,!digitalRead(11));   //Toggle pin 11
  //Other stuff


void TaskD()
  digitalWrite(10,!digitalRead(10));   //Toggle pin 10
  //Other stuff


//                             END OF CODE