For back ground reference you can check out the main project thread here:
Ok so lets get right into it, the project is a automotive glow plug relay controller for a diesel engine. I have built the hardware and I have used a dumb AI bot to help build out the boiler plate code. Now I have been able to clean up quite abit of it and it works for the most part, but I am not a programmer and I am at my limit of knowledge. I need help, but not entirely clueless.
Inputs:
Coolant Temp Sensor (Bosch 026 NTC)
Starter Input, 12v from ignition key switch. Signals engine cranking
Alternator Input, 12v from L terminal. Once engine starts, this goes live 12v.
Output:
External Relay Coil, Negative Ground Signaling from IRL B8721P N Channel Mosfet
Microcontroller:
Adafruit Qt Py with samd21 chipset
For those that actually would like to know how this system works I have written a 2 page overview of operations. I have also included the factory Mitsubishi Glow Plug Operations manual that the overview was based on. This controller is in fact an attempt at replicating the Mitsubishi Glow Plug Controller.
Controller_Overview.pdf (29.8 KB)
Mitsu_4M40_GlowPlug_Ops.pdf (157.4 KB)
My problem with the code is at the afterglowMode, it will turn on the relay, but then instead of checking the conditions, it bypasses that and returns to the beginning of the loop.
This is how it should be working:
Once it enters afterglowMode it should start the millis() timer and query the temp sensor and constantly check for either temperature to go above 140*F OR reach 3 minutes. Once either one of these conditions is met, it will turn off the relay and end operations.
Like I said it is working in the fact that it is turning the relay on and turning it off based on other conditions in the loop. When I first started the relay was not coming on or it would come on and simply just stay on forever so I have made some improvements to the code.
Thank YOU so much if you have read this far and are able to assist me!
#include <Arduino.h>
const int temperaturePin = A0;
const int alternatorPin = A1;
const int starterPin = A2;
const int relayControlPin = 6;
int relayOnTime = 0; // Variable to hold the relay "on" time
bool controlSequenceComplete = false; // Flag to indicate if the control sequence is complete
bool afterglowMode = false; // Flag to indicate if the control sequence is in afterglow mode
unsigned long afterglowStartTime = 0; // Start time of afterglow mode
const unsigned long afterglowDuration = 180000; // Duration of afterglow mode in milliseconds (3 Min)
void setup() {
pinMode(relayControlPin, OUTPUT);
pinMode(starterPin, INPUT_PULLUP);
pinMode(alternatorPin, INPUT_PULLUP);
// Additional setup code if needed
}
void loop() {
if (!controlSequenceComplete) {
float voltage = calculateVoltage(analogRead(temperaturePin));
// Check if voltage is within valid range for temperature sensor
if (voltage >= 0.0 && voltage <= 3.2) {
// Switch case statement based on voltage ranges
switch (int(voltage * 100)) {
case 282 ... 320:
relayOnTime = 24;
break;
case 208 ... 281:
relayOnTime = 16;
break;
case 123 ... 207:
relayOnTime = 10;
break;
default:
relayOnTime = 0;
break;
}
// Activate the relay circuit with the calculated relayOnTime
digitalWrite(relayControlPin, HIGH);
delay(relayOnTime * 1000);
digitalWrite(relayControlPin, LOW);
if (digitalRead(starterPin) == HIGH) {
// Starter signal detected during the initial operation
// Override and cancel the operation
controlSequenceComplete = true;
} else {
// Control sequence completed successfully
controlSequenceComplete = true;
}
} else {
// Failsafe warning signal, if we lose temp sensor R2 then voltage
// should go to 3.3, anything above 3.2v(-30C(-22F) will trigger.
for (int i = 0; i < 5; i++) {
digitalWrite(relayControlPin, HIGH);
delay(1000);
digitalWrite(relayControlPin, LOW);
delay(2000);
}
digitalWrite(relayControlPin, HIGH);
delay(10000);
digitalWrite(relayControlPin, LOW);
controlSequenceComplete = true;
}
} else {
if (digitalRead(starterPin) == HIGH) {
// Starter signal detected, activate the relay control pin
digitalWrite(relayControlPin, HIGH);
} else {
digitalWrite(relayControlPin, LOW); // Deactivate the relay circuit
// Starter signal went low, stop the control sequence
controlSequenceComplete = true; // Set control sequence complete flag
}
}
if ((!afterglowMode) && (digitalRead(alternatorPin)) == HIGH) {
// Alternator signal detected, engine has started
// Enter afterglow mode
afterglowMode = true;
digitalWrite(relayControlPin, HIGH); // Activate the relay control pin
unsigned long afterglowCurrentTime = millis(); // Start afterglow mode timer
float voltage = calculateVoltage(analogRead(temperaturePin));
// Check if afterglow mode should continue
if ((voltage <= 1.23) || (afterglowCurrentTime - afterglowStartTime >= afterglowDuration)) {
// Coolant temperature reached 60°C or three minutes have elapsed
digitalWrite(relayControlPin, LOW); // De-activate the relay circuit
afterglowMode = false; // Exit afterglow mode
controlSequenceComplete = true; // Set control sequence complete flag
} else {
controlSequenceComplete = false; // Set control sequence not complete flag
}
}
}
float calculateVoltage(int sensorValue) {
float voltage = sensorValue * (3.3 / 1024.0); // Convert the analog value to voltage
return voltage;
}