int AC_LOAD = 3; // Output to Opto Triac pin
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
void setup()
{
Serial.begin(9600);
pinMode(AC_LOAD, OUTPUT); // Set the AC Load as output
attachInterrupt(digitalPinToInterrupt(2), zero_crosss_int, RISING); // Choose the zero cross interrupt
}
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Firing angle calculation :: 50Hz-> 10ms (1/2 Cycle)
// (10000us - 10us) / 128 = 75 (Approx)
int dimtime = (75*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(AC_LOAD, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // triac Off
}
void loop()
{
dimming = 90;
delay(7000);
dimming = 50;
delay(7000);
dimming = 20;
delay(7000);
}