Execute two functions simultaneously on separate thread?

New Photon user here.

I’m a .net C# developer by trade but am having some trouble finding a starting path for the project described below:

I have a Photon connected to a 2 channel relay board. Relay 1 controls an LED strobe. Relay 2 controls a cse buzzer. The relays will activate at the same time from separate functions and stay active for approximately 30 secs. They will be activated by a single function published to the cloud. Normally I would handle this with background threads to perform the work of manipulating the relays so that the main thread can return a result value to the cloud caller immediately. My problem is that I’m not exactly sure if this translates into embedded code. Any help or guidance would be greatly appreciated!!

While you can split off seperate FreeRTOS threads I’d still ask how important is the actualy synchronicity of the relay response?
I’m sure there are cases where that extra effort is required, but is this such a case?

The really simple common solution would be to let the relay control happen in loop() or a Software Timer callback or even a HW timer interrupt depending on two (volatile) flags that’d be set from your respective functions and reset by another Software Timer for the 30sec duration.

If this won’t work for you, could you provide more background?

Also if you happen to go for two seperate “relay” threads, be aware that these will not run parallel but will get 1ms timeslices each - also sharing with the system and application threads.

1 Like

How about execute a function to activate the relays and have a callback to come back and switch them off? You want to do this without interfering with the normal program flow?

Whoops, I posted this not seeing @ScruffR 's comments…

1 Like

Thanks for the quick reply @ScruffR (and @BulldogLowell) ! A little more background on the project and the operation of the relays:

The LED strobe and buzzer are an alerting mechanism for a fire station. When a call is received I make a call to the cloud function which will activate the relays. As you eluded the synchronicity of the relay activation is not that important. They can be activated in a linear fashion. Where the complication is introduced is that, while the led relay will stay on for 30 secs, the buzzer relay needs to be able to pulse and will finish its cycle prior to the led relay turning off. This last part is why I had originally thought of using a thread to manage the pulsing of the buzzer.

I may be able to handle it with multiple volatile flags with a combination of the loop() and Software Timers. Just still trying to work my way through the logic…

1 Like

Looks like I was just over-complicating the issue in epic fashion :slight_smile:

@ScruffR - I followed your suggestion of of activating the relays in the loop() with flags and using a System Timer with a callback. Works like a charm!!!