GPS Synchronized DC Current Switches

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

I say that the code IS NOT FINISHED YET. Of course it cant be compiled

A $50 GPS so you can avoid using a $5 RTC? Doesn't make sense to me.

  if (x == 0);

Do you really think that ; belongs there?

Your code make little sense because x means nothing. Perhaps if you used a name that means something, you would not be having so much trouble.

First

  if (x == 0);

The semi colon causes this test to be ignored

Second
Put all the code to be executed when the if returns true in curly brackets. It is much easier to see what is going on.

UKHeliBob,

Thank you for the semi colon tip, that solved one of the issues. The rest of the code is:

if (x == 0)
counterSetup();

void counterSetup() {
while (x < now()) {
x = x + onVar + offVar;
Serial.print(x);
Serial.println();
Serial.print(now());
Serial.println();
y = now() - x;
Serial.print(y);
}

I have some debugging code in there for me to see whats going on. I will have to change the math to something faster than counting from zero to 1.4 billion. Something like:

now() / (onVar + offVar) = variable (an integer that I round down to remove any remainders, not sure how I round yet)
x = (onVar + offVar) * variable
start counting from there with the existing code

PaulS,

Sorry my code doesn't make much sense as I am admittedly a poor programer and am just trying to hammer out the bones of the program before annotating it properly. The reason I am using GPS timing and not RTC is I require consistent timing over a period of weeks, with several interrupter units, in a varying temperature/climate and the units are sometime several miles from each other (so linking them isn't an option). GPS UTC time is the way the super expensive commercial units perform the function.

Thanks for the help. I will re-post the entire code once I get it making more sense I suppose. Anymore input or berating is welcomed in the mean while.