/* Arduino Hardware Interrupts Control AC Power https://www.bristolwatch.com/arduino/arduino_power_control.htm by Lewis Loflin lewis@bvu.net */ /* Purpose: to detect zero crossing pulse at INT0 digital pin 2, which after delay switches on a triac. Power output to triac activated by external switch. */ #define triacPulse 5 #define SW 4 #define aconLed 12 int val; void setup() { pinMode(2, INPUT); digitalWrite(2, HIGH); // pull up pinMode(triacPulse, OUTPUT); pinMode(SW, INPUT); digitalWrite(SW, HIGH); pinMode(aconLed, OUTPUT); digitalWrite(aconLed, LOW); } void loop() { // check for SW closed if (!digitalRead(SW)) { // enable power attachInterrupt(0, acon, FALLING); // HV indicator on digitalWrite(aconLed, HIGH); } // end if else if (digitalRead(SW)) { detachInterrupt(0); // disable power // HV indicator off digitalWrite(aconLed, LOW); } // else } // end loop // begin ac int routine // delay() will not work! void acon() { // 10K potentiometer connected to AN0 delayMicroseconds((analogRead(0) * 7) + 200); // read AD0 // change 7 to 9 if 50Hz. digitalWrite(triacPulse, HIGH); delayMicroseconds(200); // delay 200 uSec on output pulse to turn on triac digitalWrite(triacPulse, LOW); }