const byte _zeroCrossPin = 2; //AC Zero-cross input
const byte SSR_PIN = 4; //SSR control output
const byte SSS_PIN = 7; //SSR control output
const byte POT_PIN = A2; //SSR control output
volatile bool interruptOK = false;
volatile bool burstfiring = false;
volatile byte targetPower = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {};
pinMode(SSR_PIN, OUTPUT);
digitalWrite(SSR_PIN, LOW);
pinMode(SSS_PIN, OUTPUT);
digitalWrite(SSS_PIN, LOW);
pinMode(12, INPUT_PULLUP);
pinMode(_zeroCrossPin, INPUT);
attachInterrupt(digitalPinToInterrupt(_zeroCrossPin), zeroCrossISR, RISING);
}
void zeroCrossISR() {
interruptOK = true;
static bool fullPeriod = false;
fullPeriod = !fullPeriod;
if (fullPeriod == false) return;
//Bresenham-Based Burst Firing Control
static uint16_t accumulator = 0;
accumulator += targetPower;
if (accumulator >= 100) {
accumulator -= 100;
digitalWrite(SSR_PIN, burstfiring);
} else
digitalWrite(SSR_PIN, LOW);
//Time-Based Burst Firing Control
uint32_t currentTime = micros();
static uint32_t previousTime = currentTime;
const uint32_t windowUs = 1000000UL;
if (currentTime - previousTime >= windowUs)
previousTime += ((currentTime - previousTime) / windowUs) * windowUs;
uint32_t onTimeUs = (windowUs * targetPower) / 100;
if (currentTime - previousTime < onTimeUs)
digitalWrite(SSS_PIN, burstfiring);
else
digitalWrite(SSS_PIN, LOW);
}
void loop() {
targetPower = map(analogRead(POT_PIN), 0, 1023, 0, 100);
burstfiring = digitalRead(12);
watchdog(1000);
}
void watchdog(const uint32_t timeout) {
uint32_t currentTime = millis();
static uint32_t previousTime = currentTime;
if (interruptOK) {
interruptOK = false;
previousTime = currentTime;
}
if (currentTime - previousTime >= timeout) {
digitalWrite(SSR_PIN, LOW);
digitalWrite(SSS_PIN, LOW);
}
}
STOP