RF Decode and AC Phase Control

My project uses an RF remote to control a 60Hz AC motor speed using phase control. I want to use Arduino to both decode the RF remote signal and provide the phase control for the AC motor. I wrote code to decode the RF remote codes (30 bit code 1.5 ms/bit; 45 ms frame length), which works. I borrowed code from the AC Phase Control (Arduino Playground) that generates the phase delays and that also works. I am using an H11aa1 zero crossing detector into interrupt 1 (pin 3) to interrupt the 120 Hz zero crossings and a second timer controlled ISR to generate the delay. The sketch on the Arduino Playground uses dual ISR’s selected in the main loop. This doesn’t work I think because of timing considerations with reading the 45 ms frame. I have attempted to modify this code so it does not use the main loop. I am using Timer1.attachInterrupt(NowIsTheTime, offTime) to generate the phase delay. I find an interesting thing. No matter what value of offTime I use to set the delay, including a fixed number, the phase delayed output pulse (pin 9) is always 100% synchronized with the zero crossing input pulse. I have tried an actual value in addition to volatile variable offTime. I have reproduced the relevant section of code along with a scope picture of the input (yellow) and output (blue) showing the blue supposed to be delayed pulse. I have also attached a scope trace when the delay is working properly when it is configured as in the Arduino Playground sketch. I would appreciate it if anyone could critique my code or suggest a way that I can use an ISR to delay the zero crossing pulse for phase control while allowing the main loop to decode the 45 ms serial bit signal.

void setup() {
  // put your setup code here, to run once:
offTime = 4000;
attachInterrupt(1,zero_cross_detect,RISING); //interrupt 1 (pin 3, IRQ1), on rising pulse
period = 8333;// define period as integer, must specify maximum period when timer is reset, max period=8333 µsec 1000000/60*2
Timer1.initialize(period);
Timer1.disablePwm(9);
Timer1.disablePwm(10);  
pinMode(in,INPUT);
pinMode(out,OUTPUT);
Serial.begin(9600);
pinMode(learndone,OUTPUT);
digitalWrite(out,HIGH);
pinMode(learn,INPUT);
digitalWrite(learndone,LOW);

}
void zero_cross_detect()
{
Timer1.restart();//reset timer
Timer1.attachInterrupt(nowIsTheTime,offTime);//when counter hits offtime jump to nowIsThe Time

}   
      
void nowIsTheTime (){
    {
      
        digitalWrite(AC_PIN,HIGH);
        wait = sqrt(wait);    //delay won’t work in an interrupt.
        if (!wait)                      // this takes 80uS or so on a 16Mhz processor to compute sqrt.
        {
            wait = 3276700000;
        }
        digitalWrite(AC_PIN,LOW);
        //state = B00000010;
        Timer1.detachInterrupt();
        attachInterrupt(1, zero_cross_detect, RISING);
    }

    }

DS0017.jpg

DS0018.jpg

I would appreciate it if anyone could critique my code

The code you posted is incomplete.

Thank you here is complete code

#include <EEPROM.h>
#include <TimerOne.h>
#define AC_PIN 9
int period;
volatile int offTime;
double wait = 3276700000;
volatile byte state=255;//have to know which interrupt to attach and detach
int in = 8;
//moved input to pin 8 11-4-15 for Trinket pro;
int learn=5;
int val;
int val1;
int i;
int remoteArray1[31];
int remoteArray2[31];
boolean result;
boolean result1;
boolean bittest1;
boolean bittest2;
int test1;
int test2;
int out=13;
int learndone=6;
int zerocross=10;


void setup() {
  // put your setup code here, to run once:
offTime = 4000;
attachInterrupt(1,zero_cross_detect,RISING); //interrupt 1 (pin 3, IRQ1), on rising pulse
period = 8333;// define period as integer, must specify maximum period when timer is reset, max period=8333 µsec 1000000/60*2
Timer1.initialize(period);
Timer1.disablePwm(9);
Timer1.disablePwm(10);  
pinMode(in,INPUT);
pinMode(out,OUTPUT);
Serial.begin(9600);
pinMode(learndone,OUTPUT);
digitalWrite(out,HIGH);
pinMode(learn,INPUT);
digitalWrite(learndone,LOW);

}
void zero_cross_detect()
{
Timer1.restart();//reset timer
//state=B00000011;
Timer1.attachInterrupt(nowIsTheTime,offTime);//when counter hits offtime jump to nowIsThe Time

}   
      
void nowIsTheTime (){
 // if (state==1)    //the interrupt has been engaged and we are in the dwell time....
    {
      
        digitalWrite(AC_PIN,HIGH);
        wait = sqrt(wait);    //delay won’t work in an interrupt.
        if (!wait)                      // this takes 80uS or so on a 16Mhz processor to compute sqrt.
        {
            wait = 3276700000;
        }
        digitalWrite(AC_PIN,LOW);
        //state = B00000010;
        Timer1.detachInterrupt();
        attachInterrupt(1, zero_cross_detect, RISING);
    }

    }


void loop() {

result=true;
result1=true;
bittest1=false;
bittest2=false;
while(bittest1==false){while(digitalRead(in)==LOW) {};//keeps reading until sync is obtained
while(digitalRead(in)==HIGH) {};for(i=0;i<31;i=i+1)
{delayMicroseconds(700);remoteArray1[i]=digitalRead(in);delayMicroseconds(500);while(digitalRead(in)==HIGH){};}if((remoteArray1[0]==1) && (remoteArray1[1]==1) && (remoteArray1[2]==1) && (remoteArray1[3]==1)&& (remoteArray1[17]==1) && (remoteArray1[18]==0)) {bittest1=true;};} 
if(digitalRead(learn)==LOW){digitalWrite(learndone,HIGH);{for (i=0; i < 31; i=i+1)EEPROM.write(i,remoteArray1[i]);}}
for (i=0; i < 31; i=i+1){Serial.print(remoteArray1[i]);}
Serial.print("Array");Serial.println();
for (i=0; i < 31; i=i+1){Serial.print(EEPROM.read(i));}
//Serial.print("EEPROM");
Serial.println();
for (i=0; i<20; i++) {if (remoteArray1[i]!=EEPROM.read(i)) result=false;}//compare remote 20 bit code to 20 bit code from EEPROM
if((result==true) && (remoteArray1[30]==1) && (remoteArray1[29]==1) && (remoteArray1[28]==1)) {Serial.print("fire On");Serial.println();digitalWrite(out,LOW);}
if((result==true) && (remoteArray1[30]==0) && (remoteArray1[29]==0) && (remoteArray1[28]==0)){Serial.print("fire Off");Serial.println();digitalWrite(out,HIGH);}
digitalWrite(learndone,LOW);}

Could you please auto format that code and repost, please?
As it is, loop() is virtually illegible.

Thank you. Here is the auto format version. Hope this is easier to read.

#include <EEPROM.h>
#include <TimerOne.h>
#define AC_PIN 9
int period;
volatile int offTime;
double wait = 3276700000;
volatile byte state = 255; //have to know which interrupt to attach and detach
int in = 8;
//moved input to pin 8 11-4-15 for Trinket pro;
int learn = 5;
int val;
int val1;
int i;
int remoteArray1[31];
int remoteArray2[31];
boolean result;
boolean result1;
boolean bittest1;
boolean bittest2;
int test1;
int test2;
int out = 13;
int learndone = 6;
int zerocross = 10;


void setup() {
  // put your setup code here, to run once:
  offTime = 4000;
  attachInterrupt(1, zero_cross_detect, RISING); //interrupt 1 (pin 3, IRQ1), on rising pulse
  period = 8333;// define period as integer, must specify maximum period when timer is reset, max period=8333 µsec 1000000/60*2
  Timer1.initialize(period);
  Timer1.disablePwm(9);
  Timer1.disablePwm(10);
  pinMode(in, INPUT);
  pinMode(out, OUTPUT);
  Serial.begin(9600);
  pinMode(learndone, OUTPUT);
  digitalWrite(out, HIGH);
  pinMode(learn, INPUT);
  digitalWrite(learndone, LOW);

}
void zero_cross_detect()
{
  Timer1.restart();//reset timer
  //state=B00000011;
  Timer1.attachInterrupt(nowIsTheTime, offTime); //when counter hits offtime jump to nowIsThe Time

}

void nowIsTheTime () {
  // if (state==1)    //the interrupt has been engaged and we are in the dwell time....
  {

    digitalWrite(AC_PIN, HIGH);
    wait = sqrt(wait);    //delay won’t work in an interrupt.
    if (!wait)                      // this takes 80uS or so on a 16Mhz processor to compute sqrt.
    {
      wait = 3276700000;
    }
    digitalWrite(AC_PIN, LOW);
    //state = B00000010;
    Timer1.detachInterrupt();
    attachInterrupt(1, zero_cross_detect, RISING);
  }

}


void loop() {
  result = true;
  result1 = true;
  bittest1 = false;
  bittest2 = false;
  while (bittest1 == false) {
    while (digitalRead(in) == LOW) {}; //keeps reading until sync is obtained
    while (digitalRead(in) == HIGH) {}; for (i = 0; i < 31; i = i + 1)
    {
      delayMicroseconds(700);
      remoteArray1[i] = digitalRead(in);
      delayMicroseconds(500);
      while (digitalRead(in) == HIGH) {};
    } if ((remoteArray1[0] == 1) && (remoteArray1[1] == 1) && (remoteArray1[2] == 1) && (remoteArray1[3] == 1) && (remoteArray1[17] == 1) && (remoteArray1[18] == 0)) {
      bittest1 = true;
    };
  }
  if(digitalRead(learn)==LOW){digitalWrite(learndone,HIGH);{for (i=0; i < 31; i=i+1)EEPROM.write(i,remoteArray1[i]);}}
  for (i=0; i < 31; i=i+1){Serial.print(remoteArray1[i]);}
  Serial.print("Array");Serial.println();
  for (i=0; i < 31; i=i+1){Serial.print(EEPROM.read(i));}
  Serial.print("EEPROM");
  Serial.println();
  for (i = 0; i < 20; i++) {
    if (remoteArray1[i] != EEPROM.read(i)) result = false; //compare remote 20 bit code to 20 bit code from EEPROM
  }
  if ((result == true) && (remoteArray1[30] == 1) && (remoteArray1[29] == 1) && (remoteArray1[28] == 1)) {
    Serial.print("fire On");
    Serial.println();
    digitalWrite(out, LOW);
  }
  if ((result == true) && (remoteArray1[30] == 0) && (remoteArray1[29] == 0) && (remoteArray1[28] == 0)) {
    Serial.print("fire Off");
    Serial.println();
    digitalWrite(out, HIGH);
  }
  digitalWrite(learndone,LOW);

}