I changed those to regular variables as well, but the timers still don't sync, so the comparison is always off for the time delay we need as the pwm duty cycle
#include <avr/io.h>
#include <avr/interrupt.h>
bool t1_changed = false;
bool t2_changed = false;
void setup() {
Serial.begin(115200);
noInterrupts();
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS22);
OCR2A = 0;
OCR2B = 0;
TCNT2 = 0;
interrupts();
attachInterrupt(0, t1_ISR, RISING); //enable interrupt service routines on pins 2 and 3
attachInterrupt(1, t2_ISR, RISING);
}
volatile unsigned int t1;
volatile unsigned int t2;
//volatile unsigned int oldt1, oldt2;
void loop() {
while(!(t1_changed && t2_changed))
{
Serial.println(t2 ^ t1);
noInterrupts();
t1_changed = false; t2_changed = false;
if(t1 < t2)
{
OCR2A = t2 ^ t1; //duty cycle 50%
OCR2B = 0;
}
else if(t2 < t1)
{
OCR2B = t1 ^ t2;
OCR2A = 0;
}
// t1_changed=true;
//t2_changed=true;
interrupts();
}}
void t1_ISR()
{
//Serial.println("ISR");
t1 = TCNT2;
t1_changed = true;
// Serial.println(t1);
//oldt1=t1;
}
void t2_ISR()
{
//Serial.println("ISR2");
t2 = TCNT2;
t2_changed = true;
// Serial.println(t2);
// oldt2=t2;
}