// Pin assignments for components
const int ignitorPin = 2; // D2
const int combustionBlowerPin = 3; // D3
const int convectionBlowerPin = 4; // D4
const int augerMotorPin = 5; // D5
const int tempSwitchPin = 6; // D6 (Temperature switch)
const int thermostatPin = 7; // D7
const int indicatorLightPin = 8; // D8 (Indicator light)
//Timers in milliseconds
const unsigned long augerCycleDuration = 5000; // 3 seconds
const unsigned long augerRunDuration = 1000; // 1 second
const unsigned long shutdownDelay = 10000; // 10 seconds
const unsigned long startupTimeout = 15000; // 15 seconds
// Run states
enum RunState {
OFF,
STARTUP,
RUNNING,
SHUTDOWN
};
// Variables to manage the run state and timing
RunState runState = OFF;
unsigned long startTime = 0;
unsigned long shutdownStartTime = 0;
unsigned long startupTimer = 0;
#include <EEPROM.h>
// Variable addresses in EEPROM
int faultAddress = 0;
// Add this variable declaration at the beginning of your code
boolean fault;
void setup() {
// Initialize pins as OUTPUT or INPUT
pinMode(ignitorPin, OUTPUT);
pinMode(combustionBlowerPin, OUTPUT);
pinMode(convectionBlowerPin, OUTPUT);
pinMode(augerMotorPin, OUTPUT);
pinMode(tempSwitchPin, INPUT);
pinMode(thermostatPin, INPUT);
pinMode(indicatorLightPin, OUTPUT); // Indicator light as OUTPUT
// Read fault variable from EEPROM
fault = EEPROM.read(faultAddress);
fault = false; // Initialize fault to false at startup
// Set the initial state to OFF
runState = OFF;
// Initialize serial communication
Serial.begin(9600); // Adjust the baud rate accordingly
// Allow the Arduino to stabilize after a reset
startTime = millis();
}
void loop() {
// Your main loop logic goes here
// Introduce a delay of 100 milliseconds
delay(100);
// Read the current state of the thermostat
boolean thermostatCurrentState = digitalRead(thermostatPin);
boolean tempSwitchState = digitalRead(tempSwitchPin);
switch (runState) {
case OFF:
Serial.print("OFF");
Serial.print(", Fault Status: ");
Serial.println(fault ? "Fault" : "No Fault");
// All outputs are off, indicator light is off
stopIgnitor();
stopCombustionBlower();
stopConvectionBlower();
stopAuger();
digitalWrite(indicatorLightPin, LOW);
// Introduce a delay of 100 milliseconds
delay(100);
// Check if the thermostat is turned on to transition to STARTUP state
if (thermostatCurrentState == HIGH && fault == false) {
startupTimer = millis(); // Initialize startupTime when entering STARTUP state
runState = STARTUP;
}
else{
stopIgnitor();
stopCombustionBlower();
stopConvectionBlower();
stopAuger();
}
break;
case STARTUP:
Serial.print("STARTUP");
Serial.print(", millis: ");
Serial.print(millis());
Serial.print(", startupTimer: ");
Serial.print(startupTimer);
Serial.print(",millis - startupTimer: ");
Serial.println(millis()-startupTimer);
// Initialize startup sequence
startIgnitor();
startCombustionBlower(); // Combustion blower should be running at the start
runAugerCycle(); // Start the auger cycle during startup
// Introduce a delay of 100 milliseconds
delay(100);
// Check if the temperature switch signal is received after the startup timeout
if (thermostatCurrentState == LOW) {
runState = SHUTDOWN;
} if (millis() - startupTimer > startupTimeout && tempSwitchState == HIGH) {
Serial.println("Enter RUNNING temp switch hot(high)");
runState = RUNNING;
} else if (millis() - startupTimer > startupTimeout && tempSwitchState == LOW) {
fault = true;
Serial.println("Enter SHUTDOWN temp switch cold(low)");
runState = SHUTDOWN;
// Save the fault variable to EEPROM
EEPROM.write(faultAddress, fault);
} else {
// Do nothing in other cases
//runState = OFF;
}
// Flash the indicator light fast for startup
digitalWrite(indicatorLightPin, (millis() / 200) % 2);
break;
case RUNNING:
Serial.println("RUNNING");
// Check if the thermostat is turned off during the ignitor cycle
// Turn off the ignitor in the RUNNING state
stopIgnitor();
startCombustionBlower(); // Combustion blower should be running during the run cycle
startConvectionBlower(); // Run the convection blower during the run cycle
runAugerCycle();
// Introduce a delay of 100 milliseconds
delay(100);
if (thermostatCurrentState == LOW) {
shutdownStartTime = millis();// Initialize shutdownStartTime when entering SHUTDOWN state
runState = SHUTDOWN;
Serial.println("Thermostat turned off. Transitioning to SHUTDOWN state.");
// Check if the temperature switch is hot
}else if (tempSwitchState == LOW) {
// Temperature switch is cold, initiate shutdown
fault = true;
shutdownStartTime = millis();// Initialize shutdownStartTime when entering SHUTDOWN state
Serial.println("Temperature switch is cold. Initiating shutdown.");
runState = SHUTDOWN;
} else {
// Thermostat is on, continue running
Serial.println("Running Normal");
}
// Indicator light is on for running
digitalWrite(indicatorLightPin, HIGH);
break;
case SHUTDOWN:
Serial.print("SHUTDOWN");
Serial.print(",millis: ");
Serial.print(millis());
Serial.print(", shutdownStartTime: ");
Serial.print(shutdownStartTime);
Serial.print(",millis - SHUTDOWN: ");
Serial.println(millis() - shutdownStartTime);
// Ensure auger is off
stopAuger();
stopIgnitor();
startCombustionBlower();
// Introduce a delay of 100 milliseconds
delay(100);
// Keep combustion blower on for 20 seconds
if (millis() - shutdownStartTime >= shutdownDelay) {
stopCombustionBlower();
stopConvectionBlower(); // Stop the convection blower after the shutdown delay
Serial.println("SHUTDOWN");
runState = OFF; // Transition to OFF state
}
// Flash the indicator light slowly for shutdown
digitalWrite(indicatorLightPin, (millis() / 1000) % 2);
break;
default:
// Do nothing in other cases
break;
}
}
// Add or modify functions for combustion blower and convection blower as needed
void startCombustionBlower() {
digitalWrite(combustionBlowerPin, HIGH);
}
void stopCombustionBlower() {
digitalWrite(combustionBlowerPin, LOW);
}
void startConvectionBlower() {
digitalWrite(convectionBlowerPin, HIGH);
}
void stopConvectionBlower() {
digitalWrite(convectionBlowerPin, LOW);
}
// Add or modify functions for other components as needed
void startIgnitor() {
digitalWrite(ignitorPin, HIGH);
}
void stopIgnitor() {
digitalWrite(ignitorPin, LOW);
}
void runAugerCycle() {
// Run the auger for 1 second every 3 seconds
unsigned long elapsedTime = millis() - startTime;
if (elapsedTime % augerCycleDuration < augerRunDuration) {
digitalWrite(augerMotorPin, HIGH);
} else {
digitalWrite(augerMotorPin, LOW);
}
}
void stopAuger() {
digitalWrite(augerMotorPin, LOW);
}