const byte SSR_PIN = 4;
const byte SSS_PIN = 7;
const byte POT_PIN = A2;
float frequency = 50.0; // min frequency = 30,52 Hz (Prescaler 8)
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);
cli();
TCCR1A = 0;
TCCR1B = 0;
OCR1A = (16000000UL / 8 / frequency) - 1;
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS11); // Prescaler 8 (min frequency = 30,52 Hz)
TIMSK1 |= (1 << OCIE1A);
sei();
float hz = 16000000.0 / (8.0 * (OCR1A + 1));
Serial.print("frequency = ");
Serial.println(hz, 4);
Serial.println("");
}
ISR(TIMER1_COMPA_vect) {
interruptOK = true;
//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() {
burstfiring = digitalRead(12);
targetPower = map(analogRead(POT_PIN), 0, 1023, 0, 100);
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