volatile int i = 0; //zero-point counter
int cycleInterval1 = 7; //duty cycle interval 1
int cycleInterval2 = 11; //duty cycle interval 2
//(initial values of 7 and 11 will
//default the on/off cycle to 70% duty)
void setup(){
Serial.begin(28800); //begin serial communication
pinMode(1, OUTPUT); //connected to SSR TRIAC
pinMode(7, INPUT_PULLUP); //connected to hall sensor
attachInterrupt //interrupt at zero-point
(0, atZeroPoint, RISING); //pin connected to zero-point detector
attachInterrupt //interrupt when hall sensor is activated
(7, atHallPass, FALLING); //pin connected to hall effect sensor
}
void atZeroPoint(){ //function called at each zero-point
i++; //increment counter
}
void atHallPass(){ //function called at each hall-pass
Serial.print(1, BIN); //message indicating revolution complete
}
void loop(){
if(i == 1){ //at first zero-point:
digitalWrite(1, HIGH); //send voltage to TRIAC
}
if(i == cycleInterval1){ //if interval 1 is reached:
delay(1); //(TRIAC on for remainder of half-cycle)
digitalWrite(1, LOW); //cease voltage to TRIAC
}
if(i == cycleInterval2){ //if interval 2 is reached:
i = 1; //reset counter to one (restart on/off cycle)
}
if(Serial.available() == 2){ //if two messages exist in buffer:
cycleInterval1 = Serial.read(); //set the first to interval 1
cycleInterval2 = Serial.read(); //set the second to interval 2
i = 0; //restart on/off cycle
}
}