i ask anyone to help me in this project of mine because the current is not detected when the firing pin is triggering the gate of triac to powered the heater
#include "max6675.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Inputs and outputs
int firing_pin = 3;
const int increase_pin = 11;
const int decrease_pin = 12;
int start_pin = 9;
int estop_pin = 10;
int zero_cross = 8;
int so1Pin = 4;
int cs1Pin = 5;
int sck1Pin = 6;
MAX6675 thermocouple(sck1Pin, cs1Pin, so1Pin);
//Variables
int last_CH1_state = 0;
bool zero_cross_detected = false;
int firing_delay = 7400;
int maximum_firing_delay = 7400;
/*We know that the 240V AC voltage has a frequency of around 50-60HZ so the period is between 20ms and 16ms,
which is standard for housing in Malaysia. We control the firing delay each half period so each 10ms or 8 ms.
To make sure we wont pass those 10ms, I selected the 7400us or 7.4ms as the value. */
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int temp_read_Delay = 500;
int real_temperature = 0;
int setpoint = 100;
bool heating_active = false; // Flag for heating status
bool estop_active = false; // Emergency stop flag
bool timer_started = false; // Flag to track if the countdown has started
unsigned long heating_start_time = 0; // Store heating start time
unsigned long heating_duration = 5400000; // 90 minutes (in milliseconds)
unsigned long time_remaining = 0; // Time remaining for the countdown
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int down_buttonState = 0; // current state of the down button
int down_lastButtonState = 0; // previous state of the down button
bool bPress = false;
//PID variables
float PID_error = 0;
float previous_error = 0;
float elapsedTime, Time, timePrev;
int PID_value = 0;
//PID constants
int kp = 203; int ki = 7.2; int kd = 1.04;
int PID_p = 0; int PID_i = 0; int PID_d = 0;
void setup() {
//Define the pins
pinMode (firing_pin, OUTPUT);
pinMode (zero_cross, INPUT);
pinMode (increase_pin, INPUT_PULLUP);
pinMode (decrease_pin, INPUT_PULLUP);
pinMode (start_pin, INPUT);
pinMode (estop_pin, INPUT);
lcd.init(); //Start the LC communication
lcd.backlight(); //Turn on backlight for LCD
}
void loop()
{
zeroCross();
checkUp();
checkDown();
currentMillis = millis(); //Save the value of time before the loop
lcd.clear();
real_temperature = thermocouple.readCelsius(); //get the real temperature in Celsius degrees
lcd.setCursor(0, 0);
lcd.print("ST: ");
lcd.setCursor(4, 0);
lcd.print(setpoint);
lcd.setCursor(8, 0);
lcd.print("RT: ");
lcd.setCursor(12, 0);
lcd.print(real_temperature);
delay(500);
// Push the start button to activate heating but NOT start the timer yet
if (digitalRead(start_pin) == HIGH && digitalRead(estop_pin) == LOW)
{
heating_active = true; // Activate heating
timer_started = false; // Ensure the timer doesn't start until setpoint is reached
// Heating is active and estop is not active
if (heating_active == true && estop_active == false)
{
if (currentMillis - previousMillis >= temp_read_Delay)
{
previousMillis += temp_read_Delay; //Increase the previous time for next loop
PID_error = setpoint - real_temperature; //Calculate the pid ERROR
PID_p = kp * PID_error; //Calculate the P value
PID_i = PID_i + (ki * PID_error); //Calculate the I value
timePrev = Time; // the previous time is stored before the actual time read
Time = millis(); // actual time read
elapsedTime = (Time - timePrev) / 1000;
PID_d = kd * ((PID_error - previous_error) / elapsedTime); //Calculate the D value
PID_value = PID_p + PID_i + PID_d; //Calculate total PID value
if (PID_value <= 0)
{
PID_value = 0;
}
if (PID_value >= 7400)
{
PID_value = 7400;
}
previous_error = PID_error; //Remember to store the previous error.
}
//If the zero cross interruption was detected we create the 100us firing pulse
if (zero_cross_detected == true)
{
delayMicroseconds(maximum_firing_delay - PID_value); //This delay controls the power
digitalWrite(firing_pin, HIGH);
delayMicroseconds(100);
digitalWrite(firing_pin, LOW);
zero_cross_detected = false;
}
// If the heater reaches the setpoint for the first time, start the 90-minute countdown
if (timer_started == false && real_temperature >= setpoint)
{
heating_start_time = millis(); // Store the time when the setpoint is reached
timer_started = true; // Flag to indicate the timer has started
}
// If the 90-minute countdown has started, update the remaining time
if (timer_started == true)
{
time_remaining = heating_duration - (millis() - heating_start_time);
// Calculate minutes and seconds
int m = time_remaining / 60000;
int s = (time_remaining % 60000) / 1000;
// Display the countdown on the LCD
lcd.setCursor(0, 1);
lcd.print("TL: ");
if (m < 10)
{
lcd.print("0"); // Leading zero for minutes
lcd.print(m);
lcd.print(":");
}
if (s < 10)
{
lcd.print("0"); // Leading zero for seconds
lcd.print(s);
}
// If the countdown has reached 0, stop the heating
if (time_remaining <= 0)
{
heating_active = false; // Stop heating after 90 minutes
digitalWrite(firing_pin, LOW); // Turn off the heater
}
}
}
}
// Push the estop button to immediately stop the heating
else if (digitalRead(estop_pin) == HIGH && digitalRead(start_pin) == LOW)
{
estop_active = true; // Activate emergency stop
heating_active = false; // Turn off heating
timer_started = false; // Stop the timer
digitalWrite(firing_pin, LOW); // Ensure the heater is off
}
}
//End of void loop
void checkUp()
{
up_buttonState = digitalRead(increase_pin);
if (up_buttonState != up_lastButtonState) // compare the buttonState to its previous state
{
if (up_buttonState == LOW) // if the state has changed, increment the counter
{
bPress = true; // if the current state is HIGH then the button went from off to on:
setpoint += 5;
delay(50); // Delay a little bit to avoid bouncing
}
}
up_lastButtonState = up_buttonState; // save the current state as the last state, for next time through the loop
}
void checkDown()
{
down_buttonState = digitalRead(decrease_pin);
if (down_buttonState != down_lastButtonState) // compare the buttonState to its previous state
{
if (down_buttonState == LOW) // if the state has changed, increment the counter
{
bPress = true;
setpoint -= 5;
delay(50); // Delay a little bit to avoid bouncing
}
}
down_lastButtonState = down_buttonState; // save the current state as the last state, for next time through the loop
}
void zeroCross()
{
if (zero_cross == HIGH) { //We make an AND with the state register, We verify if pin D8 is HIGH???
if (last_CH1_state == 0) { //If the last state was 0, then we have a state change...
delay(50);
zero_cross_detected = true; //We have detected a state change! We need both falling and rising edges
}
}
else if (last_CH1_state == 1) { //If pin 8 is LOW and the last state was HIGH then we have a state change
delay(50);
zero_cross_detected = true; //We haev detected a state change! We need both falling and rising edges.
last_CH1_state = 0; //Store the current state into the last state for the next loop
}
}
//End of interruption vector for pins on port 8