External clock signal /

Hey all, I'm back again! "So soon?" you ask. Well, let me assure you that I'm cranking along on a very fun project, but I have so many questions left unanswered. P.S. - thanks again to AWOL and davekw7x for the help on the PROGMEM conundrum :)

So here is the setup: Assume I have a 60Hz square wave coming into any of the digital pins, 0-5v. Can anyone think of a way to make a function wait for the rise and then delay a preset amount and then commit a permanent loop of delayed output signals, used as triggers, to some very nifty SCR units that are time synced to that input?

My 2 theories:

1- Tell one of the timers in charge of the PWM on a given digital pin that it should follow that external signal instead of the internal timer, then set it to both the appropriate duty cycle, and make it invert the ON/OFF to OFF/ON for output. I'm sure thats not the hardest thing to do, but I don't even know where to begin.

2- Work out which pins are on which timers. I believe there are three timers, hence there being 6 PWM pins on my UNO. Figure out which timer both millis() and delay() function on, so as not to disturb them. Then program the other timers to run at 60Hz and use an external interrupt from my clock signal to set the PWM on my output pins starting at the next change of state, which should sync them.

Side question - is there any way to pulse the PWM output so that in a cycle it would run:

OFF(for some duration)/ON(for some other duration)/Off(for a third duration)

As it stands now, the PWM on my OUTPUT pins are hard coded to a time delay calculated in the program and then run continuously.

//this program defines the firing sequence for the TCR branch of a 3 phase SVC
//the delays are hard programmed to operate with a 50% duty ratio over the
//period of approx ~ .01667sec

void FiringSequence(unsigned long delayTime){
  delayMicroseconds(delayTime);
  do{
    digitalWrite(10, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(10, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(9, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(9, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(6, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(6, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(10, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(10, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(9, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(9, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(6, HIGH);
    delayMicroseconds(1389);
    //delay(1000);
    digitalWrite(6, LOW);
    delayMicroseconds(1389);
    //delay(1000);
    //Serial.println("cycle complete");
  }

I'm a bit confused what you want to do, and why the frequency of the other signal is relevant. If you just want to wait for the LOW-HIGH transition of the other signal, why not do:

my curstate = digitalRead (somePin);
my laststate = curstate;
// wait for LOW-HIGH transition
while ( curstate != HIGH && laststate != LOW) {
    laststate = curstate;
   curstate = digitalRead (somePin);
}
// Delay some time and start your show.
delay (...);

If you want to measure the length of the other signal for some purpose, use the pulseIn() function.

Korman

I'm not sure, but it sounds like you're trying to implement some kind of PLL.

If you look carefully at the comparator interrupts, I think it would be possible to implement such a thing relatively simply from the source 60Hz sine signal.

Hey Korman, thanks for that, but I think AWOL has it more right on. The reason the phase hase to be generated by the input is that the resultant code is for a series of pulses from digital pins to fire thyristors.

They wait so many microseconds and then should stay there, relationally to the input wave. The input wave is unfortunately not perfectly at 60 Hz, so there is the slightest time variance from cycle to cycle and a high precision is required to fire the thyristors.

AWOL - I already have a square wave following the sine wave and its totally analog, so I essentially have a 5V clock into the system. really the thing of it is that I need to start a specific firing sequence at the right time, relational to the square wave that I'm reading in.

To all who posted on this thread:

Just wanted to say thanks again for all the good input. I appreciate the help a lot. I have finally got my sequence timing issue sorted. It's always the easiest answer thats the hardest one to find. I'm so stuck in what I've been taught for using C++ in visual basic that I'm not thinking like a microprocessor. I finally figured out that I can do the bulk of my programs work in the setup and then leave only the firing sequence as the main loop, which is the way it should be after all. The whole loop is now just a series of on off commands to some switches, no major calculations and it's working super smooth.

Cheers!