Hello fellow friends,
Been awhile since I last posted since I was working on my diesel glow plug controller project. I want to say I finally got the engine installed and running and have been able to test out my glow plug controller in a live environment and it works as designed!
I did have some difficulty with a portion of my code and my thread asking for advice went a little sideways and I got into a little trouble. I ultimately was able to fix the offending code and create a compromise. It in fact works but I would like to optimize the code to work as I originally had desired.
Alright lets get into it....In this portion of code I am triggering a relay based on engine coolant temperature and a 12v signal from the vehicle alternator. The 12v signals that the engine has started.
The desired behavior would be to read the coolant temp sensor and 'while' it is greater then 1.23v (less than 140F) it will trigger the relay (pin HIGH). I would like to place 2 conditions on this trigger, a) Temperature less then 140F (>1.23v) OR b) Timer of 2min (120000ms) has elapsed.
I previously tried the 'millis' function and have already banged my head against the wall with it and was never successful at making it work, so a simple timer function would be superb, start a timer, poll the time for elapsed time, that's it.
I am using a samd21 chipset and I believe that has a built in timer function but calling on silicone for timing sounds overly complex.
Now I have the code broke into 3 sections, preglow, start glow and afterglow. Everything works as designed including a fail safe mode I coded in the event the coolant sensor became disconnected or failed out of range. The only code I am concerned with right now is the afterglow portion.
My apologies if this is very verbose, but I feel that if you have all the background then it will be easier to assist in my request for help.
The following code works as written, if volts higher than 1.23v it will hold the relay on for 5 seconds and go low, and if less then 1.23v it will not trigger the relay.
Existing 'afterglow' code:
// Alternator signal detected, engine has started
if ((!afterglowComplete) && (digitalRead(alternatorPin)) == HIGH) {
float voltage = calculateVoltage(analogRead(temperaturePin)); // Read temp sensor
if (voltage > 1.23) { // Check if temperature less the 140*F
delay(250);
digitalWrite(relayControlPin, HIGH); // If temp less then 140*F, turn relay on for xx-sec
delay(5000);
digitalWrite(relayControlPin, LOW);
afterglowComplete = true; // Exit after-glow mode
}
}
//-------------------------------------------------------------------------------
So what I would like to do is use a very simple timer function to just start a simple countdown and just be able to poll that count down. So perhaps something like this.....
// Alternator signal detected, engine has started
if ((!afterglowComplete) && (digitalRead(alternatorPin)) == HIGH) {
// Start a timer ( I am guessing at this since I don't know)
int timer = elapsedtime(start(timer));
float voltage = calculateVoltage(analogRead(temperaturePin)); // Read temp sensor
// Check if temperature less the 140*F OR elapsed time less then 2 minutes
while (voltage > 1.23 || elapsedtime < 120000) {
// This should only iterate once while condition is true, should not be iterating over and over cycling relay on and off each time through the loop
delay(250);
digitalWrite(relayControlPin, HIGH);
}
// Once either condition is met, relay goes low and turns off
if (voltage <= 1.23 || elapsedtime >= 120000) {
digitalWrite(relayControlPin, LOW);
afterglowComplete = true; // Exit after-glow mode
}
}
//-------------------------------------------------------------------------------
Hopefully that is clear enough, I really appreciate the feedback. Really would like to figure out a simple timer function. I have seen multiple timer functions written for Arduino code, as well as the built in C++ chrono function. I am just unsure what is going to be the simplest to implement and trouble free.
Thank you all for your assistance.