this is the code
if its 0 I will output to pin 11 as well
#include <avr/io.h>
#include <avr/interrupt.h>
void setup() {
//Serial.begin(9600);
pinMode(2, INPUT); //set pins 2 and 3 as inputs and 10 and 11 as output
pinMode(3, INPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
cli(); // disable all interrupts
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); //set up timer2 to generate a PWM
TCCR2B = _BV(CS20); //set prescalar value to 1
TCCR1A = 0;
TCCR1B = _BV(CS20);
TCCR1B |= (1 << CS12); //256 values
TCNT2 = 0;
sei(); //enable all interrupts
attachInterrupt(0, t1_ISR, RISING); //enable interrupt service routines on pins 2 and 3
attachInterrupt(1, t2_ISR, RISING);
// Serial.print(OCR2A);
}
uint8_t t1, t2;
void loop() { //compare time that one signal goes high to time the other signal goes high
if(t2>t1){
OCR2A = (t2-t1); //output PWM based on phase difference on pin 11
}
if(t2<t1){
OCR2A = (t1-t2); //output PWM on pin 11
}
}
if (OCR2A<0)
{
pin 10;
}
else if(OCR2A>=0)
{
pin 11;
}
void t1_ISR() { //interrupt if signal on pin 2 goes high and store counter
t1 = TCNT2; //value to variable t1
}
void t2_ISR() { //interrupt if signal on pin 3 goes high and store counter
t2 = TCNT2; //value to variable t2
}