Hello everybody
I'm trying to make an AC dimmer using a tric and with the same arduino write on the LCD every 1 second and read a temp every 1 second
LET'S SAY FOR NOW I JUST WANT A CONSTANT DIMMING, SO THE LIGHT STAYS AT A STEADY LUX
The dimmer works great but when I add something else like reading a temperature or writing on the LCD the light flickers (right in the time I do something else)
I've avoided delays and used a timer interrupts but it looks not enough.
I would like to repeat that everything works great, the only think is that the light flickers, every 1 sec when I do something else.
Can someone help me to understand how to better use timers in order to always prioritize the triac timing ?
thanks a lot
#include <TimerOne.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x3F for a 16 chars and 2 line display
const byte Interrupt_PIN = 2; //Zero-crossing circuit pin
//Temp measurement
float Temp = 0; //Variabile temperatura
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
//Triac settings
volatile bool triacOn; //Triac state support variable
const byte Triac_PIN = 12; //Triac pin
int Tric_ON_Logic_State = HIGH; //Logic state that drives the Triac to turn the load ON
const byte Triac_Pulse_Micros = 50; //Time to keep the triac enable signal ON
//Loop times settings
const int PID_Loop_Period = 1000; //Loop time of the PID routine
const int Serial_Loop_Period = 1000; //Loop time of the Serial debug routine
const int Temp_Reading_Loop_Period = 3000; //Loop time of the Temp_Reading routine
const int LCD_Loop_Period = 1000; //Loop time of the LCD routine
//Timers variables setting
unsigned long Current_Millis = 0; //Loop timer support variable
unsigned long PID_Loop_Time; //Support variable for PID loop timer
unsigned long LCD_Loop_Time; //Support variable for LCD loop timer
unsigned long Temp_Reading_Loop_Time; //Support variable for Temp_Reading loop timer
unsigned long Serial_Loop_Time; //Support variable for Serial loop timer
volatile int Triac_ON_Delay = 9000; //THIS VALUE CONTROL THE LOAD POWER. Delay to turn on the TRIAC after a zero crossing. The greater it is the lower it is the power (9000 means low power)
//----------------------------------------------------------------------------------------------------------------------------------- Setup
void setup() {
pinMode(Triac_PIN, OUTPUT); //Set the Triac PIN as an output pin
attachInterrupt(digitalPinToInterrupt(Interrupt_PIN), zeroCrossing, FALLING); //Set up an interrupt on pin 2, when happens execute zeroCrossing routine, Falling trigger
Timer1.initialize(); //Initialize the Timer 1 interrupt
Timer1.attachInterrupt(triacPulse); //Set up the Timer 1 interrupt, when happens execute triacPulse routine
Serial.begin(9600); //Setup the Serial port for dbg purposes
lcd.init(); //LCD init
lcd.backlight(); //Turn the LCD backlight ON
lcd.clear(); //Clear the LCD screen
sensor.begin(); //Dallas temp sensor begin
lcd.setCursor(0,0); //Write first line
lcd.print("Dimmer"); //Write first line
delay (1000); //Wait a sec before start
}
//----------------------------------------------------------------------------------------------------------------------------------- Zero crossing interrupt routine
void zeroCrossing() {
triacOn = false; // Triac tuns off self at zero crossing
Timer1.setPeriod(Triac_ON_Delay); // To call triacPulse() after off period
}
//----------------------------------------------------------------------------------------------------------------------------------- Triac activation interrupt routine
void triacPulse() {
if (triacOn) { // stop pulse
digitalWrite(Triac_PIN, Tric_ON_Logic_State);
Timer1.stop();
} else { // start pulse
digitalWrite(Triac_PIN, !Tric_ON_Logic_State);
triacOn = true;
Timer1.setPeriod(Triac_Pulse_Micros);
}
}
//----------------------------------------------------------------------------------------------------------------------------------- PID routine
void LCD(){
if (Current_Millis > LCD_Loop_Time) { //If the millis timer reached LCD_Loop_Time...
lcd.setCursor(0,1); //Set LCD cursor on line 2
lcd.print(Temp); //Write temp on line 2
LCD_Loop_Time = Current_Millis + LCD_Loop_Period; //Set the next LCD write = Current millis time + LCD_Loop_Period
}
}
//----------------------------------------------------------------------------------------------------------------------------------- PID routine
void PID(){
if (Current_Millis > PID_Loop_Time) { //If the millis timer reached PID_Loop_Time...
Triac_ON_Delay = 9000;
PID_Loop_Time = Current_Millis + PID_Loop_Period; //Set the next PID calculation = Current millis time + PID_Loop_Period
}
}
//----------------------------------------------------------------------------------------------------------------------------------- Serial debug loop
void Serial_Debug_Loop(){
if (Current_Millis > Serial_Loop_Time) { //If the millis timer reached Serial_Loop_Time...
Serial.print(Triac_ON_Delay); //Write on the serial port
Serial.print(" ");
Serial.println(Temp, 1); //Write on the serial port
Serial_Loop_Time = Current_Millis + Serial_Loop_Period; //Set the next serial write = Current millis time + Serial_Loop_Period
}
}
//----------------------------------------------------------------------------------------------------------------------------------- Temps Reading
void Temp_Reading(){
if (Current_Millis > Temp_Reading_Loop_Time) { //If the millis timer reached Temp_Reading_Loop_Time...
sensor.setResolution(9);
sensor.requestTemperatures();
Temp = sensor.getTempCByIndex(0);
Temp_Reading_Loop_Time = Current_Millis + Temp_Reading_Loop_Period; //Set the next Temp_Reading = Current millis time + Temp_Reading_Loop_Period
}
}
//----------------------------------------------------------------------------------------------------------------------------------- Main Loop
void loop(){
Current_Millis = millis(); //Get the current time
Serial_Debug_Loop(); //Execute the Serial_Debug_Loop
Temp_Reading(); //Read the temperatures
PID(); //Execute the PID routine
LCD(); //Execute the LCD routine
}