Hello,
Quick Background:
I have been slowing working on a project that has to do with my profession which is cathodic protection. It involves switching multiple high DC current rectifiers On and OFF every few seconds in order to obtain structure-to-electrolyte voltage potential data. When multiple rectifiers are being used to cathodically protect a structure, synchronized interruption is required.
I have made several "dumb" interrupters based on 555 circuits and ICs for single current source applications. I would now like to simply switch out the "dumb" package for an Arduino so I can add selectable timing cycles and an LCD display. I have most of this already worked out and will be posting in the project section of the forums in the next few days. However, I have a programing issue I cannot solve (I am unskilled at programming) and would like some help with that part before I post.
Hardware:
Arduino UNO
Ulitmate GPS Sheild
The issues are these:
The user will select an "ON" and "OFF" timing cycle; this is usually a something like 3 ON (onVar) 2 OFF (offVar) in seconds ( each ON + OFF = Cycle time). I would like to the UNIX time (Seconds from Jan 1 1970) from the Arduino after the GPS time has been synched as the reference point so each interrupter unit will know where to begin.
I could start counting up from 0 using the total "Cycle" time until I exceed the now() time. Once I an one increment passed the now() time I can begin the toggling function. I wrote a while function that does counting but it take forever and will need to simplify this. I only want this to run once during the initial " time synching" phase of the program.
void counterSetup() {
if (x == 0);
while (x < now()) {
x = x + onVar + offVar;
}
The second issue I am having is making the counter work correctly to know when the LED (pin 13) should be ON and OFF. This is where my failure as a programmer shows through as I am sure there are several elegant solutions that I cannot see.
I had tried some "if" statements to achieve this and they have failed horribly. What I need to have is this:
Whatever the final value of x from the counterSetup is will be where my first "ON" should start. I need it to stay on for the entire value of the onVar (3 in this case) and then switch off for the offVar (2 in this case) and so on. I tried something like this:
if (now() > x + onVar)
digitalWrite(LED, HIGH);
if ((now() < x + onVar) && (now() > x + onVar + offVar))
digitalWrite(LED, LOW);
...which did not work correctly. I didn't even get to the point where it would increment x to the next cycle.
Anyways, I am finding it difficult to properly explain myself without writing a novel. If someone has an idea of what I am trying to achieve and can point me in the right direction please do. I have been doing a lot of reading before posting this and will continue to experiment with If, While, Do, Do - While statements to try and get this going. I will post in the project section once I get a little further.
Regards,
Stephan