/*
Arduino Forum
Topics: Diesel Glow Plug Controller Project
Sub-Category: Programming Questions
Category: Using Arduino
Link: https://forum.arduino.cc/t/diesel-glow-plug-controller-project/1129589/46
*/
#define temperaturePin A0 // Analog input
#define alternatorPin A1 // Discrete input
#define starterPin A2 // Discrete input
#define relayControlPin 6 // Discrete output
#define sensorFaultPin 5 // Discrete output
#define underRangePin 2 // Under Range LED Pin
#define overRangePin 3 // Over Range LED Pin
#define TX_RAW_MIN 192 // RAW Min
#define TX_RAW_MAX 961 // RAW Max
#define TX_UNDER_RANGE 173 // Under Range
#define TX_OVER_RANGE 1009 // Over Range
#define TIME_DEALY_MIN 1000 // Time Delay Min
#define TIME_DELAY_MAX 6000 // Time Delay Max
#define TIME_DEBOUNCE 20UL // Debounce Time Delay
#define TIME_BLINK 500UL // Blink Time Delay
typedef struct {
unsigned long delayTime; // Delay Time
unsigned long startTime; // Start Time
bool input; // Input State
bool state; // Timer On State
bool timerOn(bool enable); // Timer On Function
} timer_t;
timer_t starterTimer; // Starter Timer
timer_t alternatorTimer; // Alternator Timer
timer_t relayTimer; // Relay Timer
bool blinkState; // Blink State
timer_t blinkTimer; // Blink Timer
bool start; // Start State
void setup() {
pinMode(starterPin, INPUT_PULLUP); // Input Pullup Pin Mode
pinMode(alternatorPin, INPUT_PULLUP); // Input Pullup Pin Mode
pinMode(relayControlPin, OUTPUT); // Output Pin Mode
pinMode(LED_BUILTIN, OUTPUT); // Output Pin Mode
pinMode(underRangePin, OUTPUT); // Output Pin Mode
pinMode(overRangePin, OUTPUT); // Output Pin Mode
pinMode(sensorFaultPin, OUTPUT); // Output Pin Mode
starterTimer.delayTime = TIME_DEBOUNCE; // Debounce Time Delay
alternatorTimer.delayTime = TIME_DEBOUNCE; // Debounce Time Delay
blinkTimer.delayTime = TIME_BLINK; // Blink Time Delay
start = false; // Clear Start state
}
void loop() {
Blink(); // Blink Function
int rawValue = analogRead(temperaturePin); // Get RAW Value
bool fault = SensorFault(rawValue); // Get Fault State
relayTimer.delayTime = TimeDelay(rawValue); // Get Relay Time Delay
starterTimer.timerOn(!digitalRead(starterPin)); // Starter State with Debounce
alternatorTimer.timerOn(!digitalRead(alternatorPin)); // Alternator State with Debounce
start = (starterTimer.state | start) & !relayTimer.state & !fault; // Start stat (Reset-Set, Reset dominant)
relayTimer.timerOn(start); // Relay Timer On
digitalWrite(relayControlPin, start); // Relay Output
digitalWrite(LED_BUILTIN, blinkState); // Controller Running Status
}
bool SensorFault(int rawValue) {
bool underRange = rawValue <= TX_UNDER_RANGE; // Under Range Condition
bool overRange = rawValue >= TX_OVER_RANGE; // Over Range Condition
bool fault = underRange | overRange; // Under Range or Over Range
digitalWrite(underRangePin, underRange); // Under Range Status
digitalWrite(overRangePin, overRange); // Over Range Status
digitalWrite(sensorFaultPin, fault & blinkState); // Fault Status
return fault; // Return Fault State
}
unsigned long TimeDelay(int rawValue) {
if (rawValue < TX_RAW_MIN) return TIME_DEALY_MIN; // Low Limit Time Delay
if (rawValue > TX_RAW_MAX) return TIME_DELAY_MAX; // High Limit Time Delay
return map(rawValue, TX_RAW_MIN, TX_RAW_MAX, TIME_DEALY_MIN, TIME_DELAY_MAX); // Scale Time Delay
}
void Blink() {
if (!blinkTimer.timerOn(!blinkTimer.state)) return; // Return if the timer-on is not activated.
blinkState = !blinkState; // Toggle Blink State
}
bool timer_t::timerOn(bool enable) {
if (enable) {
if (!state) {
unsigned long currTime = millis();
if (!input) startTime = currTime;
unsigned long elapsedTime = currTime - startTime;
if (elapsedTime >= delayTime) {
state = true;
}
}
} else {
state = false;
}
input = enable;
return state;
}
100.0%
Over Range
|
192
0.0%
|
173
Under Range
|
1009
|
0
|
961
Under Range
Over Range
Starter
Relay
Fault
Alternator