I finally done my code in "MY" simpliest way possible. The reason is when I got only one channel program it seem to be execute faster and give less flicker in my Led Gu10 light. When active the 3 other channel thing get just a little bit anoying.
Not a huge problem but here is the question...with the documentation here:
Do you think you can do better then me for the execution of the loop ??? You'll see I send only a number to the arduino to tell wich % of light I want. I give the 1000 for the 1st channel, 2000 for the second and...
the module has a Triac and zero-crossing detector
some background to the Robotdyn dimmer library.
the library makes phase cutting . it uses two interrupts,
first interrupt is an external interrupt for the zero crossing, which only resets counters for output channels.
the second interrupt is timer interrupt fired every 12 microseconds. it handles the phase cutting intervals for the individual channels, based on count of elapsed timer interrupts since reset in zero crossing interrupt.
Juraj:
don't set the value again and again. put everything in if (Serial.available()) {
Good shot, I did it and maybe I got a litlle difference, but it's not perfect...I don't knowwhat I can do more to get it faster....will see the librairy is the cause
PaulMurrayCbr:
I'm a bit surprised by how you are working with temps of 5-95 over a range of 1000. What happens when buftemp is 2345?
The answer at your question is simply nothing
Actualy the only one input is a slider frome Node-red then the risk is null to get bad value and for 2345, it can happen happen with no "if" to take valide it
Juraj:
(I don't like it)
OK, not really sure to get it, but do you have a sugest way to do something else?
EdI_VeDeR:
OK, not really sure to get it, but do you have a sugest way to do something else?
sorry, I don't like the Robotdyn library. The interrupts take too much of the CPU time and most of the occurrences do nothing usefull.
For two channels on ATmega the TriacDimmer library is better. (It uses capture interrupt for zero crossing.) But it doesn't have a solution for more channels.
I use one channel of Robotdyn AC dimmer to regulate a 2kW electric heater. I setup a 100 Hz Fast PWM with Timer1 and reset it in zero crossing interrupt to stay in sync with AC. The period is then set with OCRA. OCRB could be used for second channel. This technique could be enhanced with other timer for additional channels.
Juraj:
sorry, I don't like the Robotdyn library. The interrupts take too much of the CPU time and most of the occurrences do nothing usefull.
For two channels on ATmega the TriacDimmer library is better. (It uses capture interrupt for zero crossing.) But it doesn't have a solution for more channels.
I use one channel of Robotdyn AC dimmer to regulate a 2kW electric heater. I setup a 100 Hz Fast PWM with Timer1 and reset it in zero crossing interrupt to stay in sync with AC. The period is then set with OCRA. OCRB could be used for second channel. This technique could be enhanced with other timer for additional channels.
After many hours I clearly don't get everything in your code but at this point I got a perfect light without flicker on every channel. The dimmer is smoother and linear. My GU10 give me 1500 to 7500 dimming value range.
The last thing that I can't go through is I can't get two, three or four channel open in the same time. Just one at a time.
#include <TimerOne.h>
const byte INTERRUPT_PIN = 2;
const byte TRIAC1_PIN = 3;
const byte TRIAC2_PIN = 4;
const byte TRIAC3_PIN = 5;
const byte TRIAC4_PIN = 6;
const byte TRIAC_PULSE_MICROS = 30;
volatile bool triacOn;
volatile int period ; // microseconds cut out from AC pulse
volatile long periodSerial; //Need a long to get bigger size then 32000
volatile int TRIACtochange_PIN;
void zeroCrossing() {
triacOn = false; // triac tuns off self at zero crossing
Timer1.setPeriod(period); // to call triacPulse() after off period
}
void triacPulse() {
if (triacOn) { // stop pulse
digitalWrite(TRIACtochange_PIN, LOW);
Timer1.stop();
} else { // start pulse
digitalWrite(TRIACtochange_PIN, HIGH);
triacOn = true;
Timer1.setPeriod(TRIAC_PULSE_MICROS);
}
}
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), zeroCrossing/* Fonction*/, RISING);
Timer1.initialize();
Timer1.attachInterrupt(triacPulse);
}
void loop() {
while (!Serial) {}
periodSerial = Serial.parseInt();
if (periodSerial!=0){
if (periodSerial>11499 && periodSerial<17501){
TRIACtochange_PIN = TRIAC1_PIN;
period = periodSerial - 10000;}
if (periodSerial>21499 && periodSerial<27501){
TRIACtochange_PIN = TRIAC2_PIN;
period = periodSerial - 20000;}
if (periodSerial>31499 && periodSerial<37501){
TRIACtochange_PIN = TRIAC3_PIN;
period = periodSerial - 30000;}
if (periodSerial>41499 && periodSerial<47501){
TRIACtochange_PIN = TRIAC4_PIN;
period = periodSerial - 40000;}
}
}
Sorry to hijack, but just a quick couple questions on these Triac modules as the documentation is almost non-existent (typical for cheap electronics from China).
Sorry to hijack, but just a quick couple questions on these Triac modules as the documentation is almost non-existent (typical for cheap electronics from China).
From your code it appears that both the Zero Crossing detect output and the triac turn-on control are Active High logic. Correct?
Is the Zero Crossing detect output driven high or is it Open Drain?
Triac switches AC on with HIGH on gate and turns AC off only at zero crossing (if gate is LOW at the time). So the Triac needs only a 20 microseconds pulse. For AVR I use the WO of Timer so the gate is HIGH until counter reaches top, which is now in my code short before zero crossing. (It could be set to a short pulse by setting the top in setPeriod to period + 20 us.)
Right now I am not sure if the zero crossing detector is active LOW or HIGH and if i is open drain. I already have a point in my TODO list to check this to make sure I detect the zero crossing at the beginning.
gfvalvo:
Is the High level for both of these signals set by the applied VCC (3.3 or 5V)?
I powered it from 5V with SAMD too. The Triac switched with 3.3 V level.
They have on shop page "The logical level is tolerant to 5V and 3.3V, therefore it can be connected to the microcontroller with 5V and 3.3V level logic"
If this is accurate, the Z-C output is indeed a high-going pulse from an open-collector transistor (with pullup resistor that could be removed is necessary).
What’s not clear from this is whether opto-isolator D1 has two back-to-back LEDs (giving two Z-C pulses per AC cycle) or a LED and regular diode (giving one Z-C pulse per AC cycle).