# Multiple, independent delays

A newby question......

I have four outputs that are dependent on a trigger event. The first output would start anywhere between 10 and 100 milliseconds after the trigger, and the next three would happen between 0 and 999 microseconds after the first output, each of the three having the option of a different delay, i.e.;

Trigger
Output 1: 10 - 100 msec after trigger
Output 2: 0 - 999 usec after Output 1
Output 3: 0 - 999 usec after Output 1
Output 3: 0 - 999 usec after Output 1

I'm in an endless loop trying to figure this out. Any suggestions? Is this even possible?

Is there a tiping error? Shouldnt it say output 1, 2 , 3 and 4?

Anyway, it is possible and it's not complex. Check the example "blink withouth delay" and learn the use of millis().

Is this even possible?

Microsecond accuracy - tricky.
Post what you've got.

mart256:
Anyway, it is possible and it's not complex. Check the example "blink withouth delay" and learn the use of millis().

sp. " learn the use of micros()."

this is with millis() not micros()

but you get the point:

untested

``````int led[4] = {2,3,4,5};
int trigger = 0;
int oldTrigger = 1;
unsigned long startTime;
int out[4];
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 4; i++)
{
pinMode(i, OUTPUT);
}
}

void loop()
{
if (trigger!=oldTrigger)
{
startTime = millis();
out[0] = random(10,100);
out[1] = random(0,999);
out[2] = random(0,999);
out[3] = random(0,999);
for (int i = 0; i < 4; i++)
{
Serial.println(out[i]);
}
}
oldTrigger = trigger;
for (int i = 0; i < 4; i++)
{
ledON(led[i], out[i]);
}
}

void ledON(int myLed, int myDelay)
{
if (millis() - startTime > myDelay)
{
digitalWrite(myLed, HIGH);
}
}
``````

I found this site to be really helpful with projects like this.

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

This demo several things at a time is an extended example of the BWoD technique.

Micros() has a minimum resolution of 4 usecs.

...R

mart256:
Is there a tiping error?

My little bit of code:

``````// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
boolean timeout(unsigned long *marker, unsigned long interval) {
if (millis() - *marker >= interval) {
*marker += interval;    // move on ready for next interval
return true;
}
else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
case 0: // Button up so far,
if (button == HIGH) return false; // Nothing happening!
else {
*butnstate = 2;                 // record that is now pressed
*marker = millis();             // note when was pressed
return false;                   // and move on
}

case 1: // Button down so far,
if (button == LOW) return false; // Nothing happening!
else {
*butnstate = 3;                 // record that is now released
*marker = millis();             // note when was released
return false;                   // and move on
}

case 2: // Button was up, now down.
if (button == HIGH) {
*butnstate = 0;                 // no, not debounced; revert the state
return false;                   // False alarm!
}
else {
if (millis() - *marker >= interval) {
*butnstate = 1;               // jackpot!  update the state
return true;                  // because we have the desired event!
}
else
return false;                 // not done yet; just move on
}

case 3: // Button was down, now up.
if (button == LOW) {
*butnstate = 1;                 // no, not debounced; revert the state
return false;                   // False alarm!
}
else {
if (millis() - *marker >= interval) {
*butnstate = 0;               // Debounced; update the state
return false;                 // but it is not the event we want
}
else
return false;                 // not done yet; just move on
}
default:                            // Error; recover anyway
{
*butnstate = 0;
return false;                   // Definitely false!
}
}
}

void setup() {
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}

void loop() {
// Act if the latter time (ms) has now passed on this particular counter,
if (timeout(&count1, 500UL )) {
if (led1State == LOW) {
led1State = HIGH;
}
else {
led1State = LOW;
}
digitalWrite(led1Pin, led1State);
}

if (timeout(&count2, 300UL )) {
if (led2State == LOW) {
led2State = HIGH;
}
else {
led2State = LOW;
}
digitalWrite(led2Pin, led2State);
}

if (timeout(&count3, 77UL )) {
if (led3State == LOW) {
led3State = HIGH;
}
else {
led3State = LOW;
}
digitalWrite(led3Pin, led3State);
}
}
``````

Make of it what you will.

JimboZA:

mart256:
Is there a tiping error?

I don't understand the joke, but seems funny. 8)

mart256:
I don't understand the joke, but seems funny.

You indeed had a spelling error in the word "typing".

It is rather funny. I am sure there is a word for that, but it quite escapes me at this time of evening.

Paul__B:
I am sure there is a word for that, but it quite escapes me at this time of evening.

Irony?

Thanks, I'll practice my english.

Thank you all much for your help, yeah I meant to have outputs 1-4. I'll try these suggestions. Cheers!