// Burst Firing Control (Zero-Crossing with SSR40-DA)
const byte SSR_PIN = 4; //SSR40-DA
const byte SSS_PIN = 7; //SSR40-DA
const byte REL_PIN = 8; //Relay
const byte LDR_PIN = 9; //LDR Senor
const byte LED_PIN = 13; //LED
const byte TMP_PIN = A0; //Thermistor
const byte POT_TMP = A1; //Potentiometer TMP
const byte POT_SSR = A2; //Potentiometer SSR
float frequency = 50.0;
bool burstfiring = false;
byte targetPower = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {};
pinMode(REL_PIN, OUTPUT);
digitalWrite(REL_PIN, LOW);
pinMode(SSR_PIN, OUTPUT);
digitalWrite(SSR_PIN, LOW);
pinMode(SSS_PIN, OUTPUT);
digitalWrite(SSS_PIN, LOW);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
digitalWrite(LED_PIN, TemperatureControl(TMP_PIN, POT_TMP));
unsigned long timer = millis();
static unsigned long LDR = timer;
if (digitalRead(LDR_PIN) == LOW) LDR = timer;
bool RelayOn = timer - LDR > 1000;
digitalWrite(REL_PIN, RelayOn);
static unsigned long PWR = timer;
if (digitalRead(LED_PIN) == LOW) PWR = timer;
bool PowerOn = timer - PWR > 1000;
targetPower = map(analogRead(POT_SSR), 0, 1023, 0, 100);
burstfiring = (RelayOn == 1 && PowerOn == 1 || 0);
BresenhamBurstFiringControl(SSR_PIN);
TimeBasedBurstFiringControl(SSS_PIN);
}
void BresenhamBurstFiringControl(byte SSR_PIN) {
//Bresenham-Based Burst Firing Control
if (frequency <= 0.0 || targetPower > 100) {
digitalWrite(SSR_PIN, LOW);
return;
}
uint32_t periodUs = 1e6 / frequency;
static uint16_t accumulator = 0;
uint32_t currentTime = micros();
static uint32_t previousTime = currentTime;
if (currentTime - previousTime >= periodUs) {
previousTime += ((currentTime - previousTime) / periodUs) * periodUs;
accumulator += targetPower;
if (accumulator >= 100) {
accumulator -= 100;
digitalWrite(SSR_PIN, burstfiring);
} else
digitalWrite(SSR_PIN, LOW);
}
}
void TimeBasedBurstFiringControl(byte SSR_PIN) {
//Time-Based Burst Firing Control
if (frequency <= 0.0 || targetPower > 100) {
digitalWrite(SSR_PIN, LOW);
return;
}
uint32_t periodeUs = 1e6 / frequency;
uint32_t long currentTime = micros();
static uint32_t previousTime1 = currentTime;
static uint32_t previousTime2 = currentTime;
if (currentTime - previousTime1 >= periodeUs) {
previousTime1 += ((currentTime - previousTime1) / periodeUs) * periodeUs;
const uint32_t windowUs = 1000000; // 1 sec
if (currentTime - previousTime2 >= windowUs)
previousTime2 += ((currentTime - previousTime2) / windowUs) * windowUs;
uint32_t onTimeUs = (windowUs * targetPower) / 100;
if (currentTime - previousTime2 < onTimeUs)
digitalWrite(SSR_PIN, burstfiring);
else
digitalWrite(SSR_PIN, LOW);
}
}
bool TemperatureControl(byte TEMP_PIN, byte POTI_PIN) {
static bool init = false;
if (init == false) {
init = true;
pinMode(TEMP_PIN, INPUT_PULLUP);
}
const float BETA = 3950;
static float setting = 0.0;
static float celsius = 0.0;
uint32_t currentTime = millis();
static uint32_t previousTime = currentTime;
if (currentTime - previousTime > 500) {
previousTime = currentTime;
uint16_t TEMP = analogRead(TEMP_PIN);
uint16_t POTI = analogRead(POTI_PIN);
celsius = 0.0;
setting = 0.0;
if (TEMP > 0 && TEMP < 1023) {
celsius = 1.0 / (log(1.0 / (1023.0 / TEMP - 1.0)) / BETA + 1.0 / 298.15) - 273.15;
setting = -24.0 + (POTI / 1023.0) * (80.0 + 24.0); // -24°C to 80°C
}
if (0) {
Serial.print("celsius = ");
Serial.println(celsius);
Serial.print("setting = ");
Serial.println(setting);
Serial.println("");
}
}
return celsius < setting;
}
< --24°C
< +80°C
< +20°C
< +50°C
< +00°C
< +30°C
< +40°C
< +10°C
< +60°C
< +70°C
< --20°C
< --20°C
POWER
TEMP