0
\$\begingroup\$

I have a board that have 3 relay. I have 3 function for Set / Reset / Toggle a relay that get a special relay number and do action on that as below:

void SetRelay(byte RelayNo) //index 1 { switch (RelayNo) { case 0: S_R1; break; case 1: S_R2; break; case 2: S_R3; break; } } void ResetRelay(byte RelayNo)//index 2 { switch (RelayNo) { case 0: R_R1; break; case 1: R_R2; break; case 2: R_R3; break; } } void ToggleRelay(byte RelayNo)//index 3 { switch (RelayNo) { case 0: T_R1; break; case 1: T_R2; break; case 2: T_R3; break; } } 

I send a packet to board something like:

Packet Data

Relay Index: Index for activate a special relay.

Function Name: A index for witch function must execute for special relay index.

Schedule Time: Is is a time in millisecond that a function must be fire for special relay index.

A sample packet:

Sample packet

That mean the board must active

  • Relay index 1 must be fire in function number 3(ToggleRelay) in 2000 millisecond
  • Relay index 3 must be fire in function number 1(SetRelay) in 1000 millisecond
  • Relay index 1 must be fire in function number 2(ResetRelay) in 2200 millisecond

If just want to fire a relay with one function its must work with system timer, BUT anybody have suggestion for how to do that with more than one function?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Look into the construction of a "Scheduler". I think that there are two main varieties: cooperative, and pre-emptive. You must decide which strategy works best for you, and then find a good example to start from. Once you have the framework in place, it becomes very straight-forward. \$\endgroup\$ Commented Jul 5, 2017 at 19:36
  • \$\begingroup\$ because the scheduler just want to Set / Reset / Toggle a Relay(nothing more) I think must doing in cooperative way. \$\endgroup\$ Commented Jul 6, 2017 at 6:44
  • \$\begingroup\$ There are two classes of scheduler: 1. cooperative. each 'task' runs to completion when it relinquishes the cpu. the scheduler then initiates the next task on the basis of some priority scheme. 2. pre-emptive multi-tasking/threading. each task runs until something causes it to be pre-empted by another task. the pre-emption could be on the basis of priority, cpu usage, entering an event wait state, etc. \$\endgroup\$ Commented Jul 6, 2017 at 7:17

1 Answer 1

-1
\$\begingroup\$

Fairly easy.

Time1 = millis() If (time1minus time0 is more than timeelapsed0) process relay0

And repeat that for two other relays.

Time0 is updated whenever a new packet was received.

\$\endgroup\$
2
  • \$\begingroup\$ My question is how handle this issue woth multiple function index! I say in question that if exist one function for doong action for relay its easy and could handle with your answer \$\endgroup\$ Commented Jul 5, 2017 at 17:17
  • \$\begingroup\$ And other thing is mayby recive new packet when proccess last packet and if update timer0 when recieve new packet it lead to lost last operation \$\endgroup\$ Commented Jul 5, 2017 at 17:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.