Hi all,
I have been trying to simulate the 50Hz signal to control a LED dimming Zero Cross Triac device. Still not have the Triac but was trying to simulate the signal and get into the grips of the programming. This will be implemented in a ESP8266.
First, tried to simulated the attachInterrupt() function, replacing the pin entry on the function by int variables ON/OFF, but did not work...
Tried the internal timer, but this runs too fast and not for my purposes. So tried the Ticker library...
The issue that I am experimenting with my program, see below code, is in the loop function when I call the FOR function to get the dimming effect, this blocks my previous function of calculating the times for each variable.
Debugging the code, when muting the FOR you can see that the times are calculated but when FOR is working, I cannot see any results of the times...
Any ideas? Thanks,
/*
Passing paramters to Ticker callbacks
Apart from void(void) functions, the Ticker library supports
functions taking one argument. This argument's size has to be less or
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
This sample runs two tickers that both call one callback function,
but with different arguments.
The built-in LED will be pulsing.
*/
#include <Ticker.h>
Ticker tickerSetHigh;
Ticker tickerSetLow;
const float t3 = 10; // In a 50Hz signal, 50 times will cross the 0. Full wave will last 0.02sec (20millis) and will cross 2 the 0 cross. Half cycle of a wave of 50Hz is 10 millis.
float t1 = 0; // OFF time, simulating a Triac this time will be off, millis to wait after zero cross.
float t2 = 0; // ON trigger,simulating a Triac this time will be on till next zero cross, millis after ON trigger. t3=t1+t2
// IN THE TICKER FUNCTION, MILLIS ARE NOT TRIGGERED WHEN ZERO CROSS OCCURS, THEY ARE ABSOLUTE VALUES MEANING NOT WHEN TRIGGERS BUT HOW LONG IT LASTS!!!
float scale = 50; // 0-100 power
void setPin(int state) { //Zero cross function to call, simulates time (t1) SetLow (OFF) then it trigger the Triac with SetHigh (ON)(t2)
digitalWrite(LED_BUILTIN, state);
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(1, HIGH);
tickerSetLow.attach_ms(t2, setPin, 0); // every t1 ms, call setPin(0). This will simulate OFF time on Triac
tickerSetHigh.attach_ms(t1, setPin, 1); // every t2 ms, call setPin(1). This will simulate ON time on Triac
}
void loop() {
t2 = (scale/100)*t3; // Period of ON time according to scale power.
t1 = t3-t2; // Period of OFF time according to scale power.
Serial.println(scale); //For debugging purposes
Serial.println(t1); //For debugging purposes
Serial.println(t2); //For debugging purposes
for (int i=5; i<=100; i++){ // Dimming effect
scale=i;
delay(1000); // Time for the dimming effect
Serial.println(scale);
}
}