Logic to Sort timed actions

Dear All,

I have 3 relays connected to Arduino Uno. These relays are used to trigger valves. I need a logic to sort the sequence.

User will specify a series of input to control the timing of each relay multiple times. like.. all values are in milliseconds from T0 (button press)

Relay | Open | Close

Relay 1 | 500 | 600 Relay 1 | 950 | 1100 Relay 1 | 1400 | 1575

Relay 2 | 1050 | 1200 Relay 2 | 1600 | 1700

Relay 3 | 1900 | 2000

each time user may specify different values and different steps. What would be best way to trigger each on and off at the correct timing!

I was thinking of converting this input to another table with RELAY, TIMING, ACTION as below.. Further sort it based on timing and follow the sequence. Is that good idea? If so what would be easy way to code it in Arduino, something like an array would be the way?

Relay | timing | action

Relay 1 | 500 | Open Relay 1 | 600 | Close Relay 1 | 950 | Open Relay 1 | 1100 | Close … …


Thank you...

Best Regards Joe

I'd model it as an array of structs. Each struct to specify the pin that controls the relay, a time and an action.

wildbill: I'd model it as an array of structs. Each struct to specify the pin that controls the relay, a time and an action.

Thank you wildbill… ok, then bubble sort it! let me try to figure out how to do it.. thanks

I'd have a Next field in each record, that refers to the next action after this one, forming a linked list. After an action has occured its next schedule time is determined, and it ripples up the list until the next event of later time, or to the end of the list. A Head item refers to the first pending event. See linked lists for algorithms and implementations.

Sorry, I don't understand the user interface here:

User will specify a series of input to control the timing of each relay multiple times. like.. all values are in milliseconds from T0 (button press)

It doesn’t need to be sorted.
Expanding: For three items, manipulating them in a way that allows them to be sorted (ie the linked list that someone suggested) is likely to be more expensive than testing the times in all of the items. You don’t really even need a loop, though that might be “structurally nice.”

Consider:

void loop() {
  static unsigned long ledtime = 0;
  static unsigned long atime, btime, ctime, nltime;
  static int ledstate = false;
  if (delay_without_delaying(ledtime, 500)) {
    ledstate = !ledstate;
    digitalWrite(13, ledstate);
  }
  if (delay_without_delaying(atime, 100)) {
    Serial.print("A");
  }
  if (delay_without_delaying(btime, 200)) {
    Serial.print("B");
  }
}

Three timed actions, trivial amounts of code taken and CPU cycles used…

You might want to think about sorting timed events when you get to someting like 50 events (but you’d probably run out of other resources, first…)