/*
Sketch: AuCP_Fan-Control-With-A-Thermostat.ino
Created: 17-Apr-2023
Author: MicroBeaut (μB)
GitHub: https://github.com/MicroBeaut/Finite-State#fan-control-with-a-thermostat
*/
#include "FiniteState.h"
#define thermostatPin A0
#define startStatusPin 5
#define stopStatusPin 6
/*
____________________________________________________________________________________________________________________________________________________
| State-Transition Table |
|___________________________________________________________________________________________________________________________________________________|
| | | | Next-State | Next-State | | | Delay-Time | |
| State | Id | Predicate | Fase | True | Process | Event | (milliseconds) | Timer-Type |
|_____________|_______|___________________|_____________|_____________|_________________|_______________________|_________________|_________________|
| STOP | 0 | HighTempPredicate | 0 | 1 | FanStopProcess | - | - | - |
| START | 1 | LowTempPredicate | 1 | 0 | FanStartProcess | - | - | - |
|_____________|_______|___________________|_____________|_____________|_________________|_______________________|_________________|_________________|
*/
void FanStartProcess(id_t id);
void FanStopProcess(id_t id);
bool HighTempPredicate(id_t state);
bool LowTempPredicate(id_t state);
enum FanState : id_t {
STOP,
START
};
Transition transitions[] = {
{HighTempPredicate, STOP, START, FanStopProcess}, // State-0 - NextF = 0, NextT = 1
{LowTempPredicate, START, STOP, FanStartProcess} // State-1 - NextF = 1, NextT = 0
};
const uint8_t numberOfTransitions = sizeof(transitions) / sizeof(Transition); // Calculate the number of transitions.
FiniteState finiteStateMachine(transitions, numberOfTransitions); // Finite-State Object
const long ThermostatRead();
void setup() {
pinMode(startStatusPin, OUTPUT); // Set the start status pin mode
pinMode(stopStatusPin, OUTPUT); // Set the sotp status pin mode
finiteStateMachine.begin(STOP); // FSM begins with Initial Transition Id 0
}
void loop() {
finiteStateMachine.execute(); // Execute the FSM
}
bool HighTempPredicate(id_t state) {
return ThermostatRead() >= 40; // Determine Fan Start Action
}
bool LowTempPredicate(id_t state) {
return ThermostatRead() <= 30; // Determine Fan Stop Action
}
void FanStartProcess(id_t id) {
digitalWrite(stopStatusPin, false); // Update fan stop status
digitalWrite(startStatusPin, true); // Update fan start status
}
void FanStopProcess(id_t id) {
digitalWrite(startStatusPin, false); // Update fan start status
digitalWrite(stopStatusPin, true); // Update fan stop status
}
const long ThermostatRead() {
long value = analogRead(thermostatPin); // Read Pushbutton Value
return map(value, 0, 1023, 0, 100); // Scaling temperature
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led1:A
led1:C
led2:A
led2:C
r8:1
r8:2
r9:1
r9:2
pot1:VCC
pot1:SIG
pot1:GND