Reputation: 33
I have a series of relays I'm controlling with an arduino connected to solenoid valves which in turn control the flow of water around a system of pipes. The relays are to be switched at regular intervals along the lines of this: Relay 1 and 3 high Wait 13s Relay 2 and 4 high Wait 17s Relay 1 and 3 low Wait 13s Relay 2 and 4 low Wait 300s Repeat
I started with a simple series of delays commands like this:
#include <DmxMaster.h>
void setup() {
DmxMaster.maxChannel(8);
}
void loop() {
delay(300000);
DmxMaster.write(1,HIGH);
DmxMaster.write(7,HIGH);
delay(13000);
DmxMaster.write(2,HIGH);
DmxMaster.write(8,HIGH);
delay(17000);
DmxMaster.write(1,LOW);
DmxMaster.write(7,LOW);
delay(13000);
DmxMaster.write(2,LOW);
DmxMaster.write(8,LOW);
}
Most of the time this works, but I will see instances where it'll skip one or more delay lines and jump to the next step. I started to look at the millis function for the longest delay (300s) and came up with the following, but I'm wondering how to implement this for the shorter delays and if this would be of any improvement:
#include <DmxMaster.h>
unsigned long currentTime;
unsigned long loopTime;
void setup()
{
DmxMaster.maxChannel(8);
currentTime = millis();
loopTime = currentTime;
}
void loop()
{
currentTime=millis();
if(currentTime >= (loopTime + 300000)){
DmxMaster.write(1,HIGH);
DmxMaster.write(7,HIGH);
delay(13000);
DmxMaster.write(2,HIGH);
DmxMaster.write(8,HIGH);
delay(17000);
DmxMaster.write(1,LOW);
DmxMaster.write(7,LOW);
delay(13000);
DmxMaster.write(2,LOW);
DmxMaster.write(8,LOW);
loopTime = currentTime;
}
}
Thanks in advance, Cameron
Upvotes: 0
Views: 1813
Reputation: 3070
In my own project, I have the same kind of constraints: I need to check sensors, get the data, analyze them and choose what to do at different intervals.
The best solution I've found is ChibiOS/RT. It's a Real Time Operating System (RTOS) developed by Giovanni Di Sirio from STMicroelectronics.
It has been ported on Arduino by Bill Greiman and is available on Github : ChibiOS-Arduino.
It is very easy to use, very well documented and has a low memory and size footprint.
I'm using it in a robotic project, Moti, you can take a look if you want but Bill has a lot of great examples.
It might look like overkill at first, but once you get used to it, you'll wonder why you did not use it earlier and every Arduino project you'll be working on will have a new dimension.
Hope this helps :)
Upvotes: 0