I am a noob to this (40+ years as an electronics tech though)nad i thought it would be easy to use an arduino in place of some CMOS dividers and 555 clock etc. to create a special use timer.
The basic purpose of the timer is in an automotive application. It is a timer which permits an extended "ignition on supply" (via a relay) for various devices in the vehicle to remain on for approx 2 hrs after the engine has been turrned off (i.e. 2 way radios to remain on for a period but then turn off to preserve the vehicle battery)
The Input will be to sense when the vehicle ignition is on (this I have no trouble with) and after the ignition is turned off, the timer starts (for approx 2 hrs) and then the device will be idle until the ignition is turned back on.
I imagined various ways to do this but when I wrote the sketch, nothing seemed to work as it was described in the doco's
Without rewriting it all here the basic premise was
Set Constants
Set Variables
Set Ints (unsigned long)
set input
set outputs (I was going to use 2, one for Ignition inout "mirroring" and the other when the timer was running) (these would be diode split to a transistor and then the relay with a cap or 2 for good measure and allow for hysterisis between the switching)
then use the while command to check for the ignition input and after this was turned off start the timer out (using millis etc.) with a check for ignition being turned back on to run back to the beginning of the loop.
Unfortunately......nothing I try seems to get close.
Generally everything does work as described in the documentation so it's likely that you didn't write the correct thing to do what you wanted. If you post your best attempt and tell us what it did do then it will be easier for us to see what's going wrong.
But the general approach to debugging code is to put lots of Serial.write()s around the code and then check in the monitor to see if it's actually getting to where you expected it to and any variables are getting set to the values you're expecting. That often gets you a fair way.
One of the greatest things about programmable devices is that they’re not fixed to a single function like discrete chips, mechanical hardware etc...
But that’s also where the pain sets in.
Being programmable, there is usually three or more perfectly legitimate methods to achieve the same result. Some will be more appropriate under different circumstances.
A good example is blinking a LED. You can simply use delay() to generate the blink cadence you need, or may extend that with variables to adjust the blink at runtime. Then you ma decide to implement a ‘tick’ timer using millis(), which allows you to run the LED and perform other tasks concurrently.
Each of these works fine, and has reason to exist.
It’s only your evolution as a developer that determines which strategy you use and why. Patience unfortunately takes time.
const byte PowerRelayDOPin = 2;
const byte IgnitionSenseDIPin = 3;
unsigned long TimerStartTime = 0;
const unsigned long PowerExtendInterval = 2UL * 60UL * 60UL * 1000UL; // 2 hours
void setup()
{
digitalWrite(PowerRelayDOPin, LOW); // Relay modules are usually Active Low
pinMode(PowerRelayDOPin, OUTPUT); // Turns on power relay
pinMode(IgnitionSenseDIPin, INPUT);
}
void loop()
{
unsignedLong currentMillis = millis()
if (digitalRead(IgnitionSenseDIPin) == HIGH)
TimerStartTime = currentMillis; // Re-start the interval
if (currentTime - TimerStartTime >= PowerExtendInterval)
{
// Ignition has been off for the desired interval
digitalWrite(PowerRelayDOPin, HIGH); // Turn off the relay
// Arduino (and relay) are powered by the relay so this will shut off
// the Arduino until the ignition is turned on again.
}
}
Grot63:
Without rewriting it all here the basic premise was
Set Constants
Set Variables
Set Ints (unsigned long)
set input
set outputs (I was going to use 2, one for Ignition inout "mirroring" and the other when the timer was running) (these would be diode split to a transistor and then the relay with a cap or 2 for good measure and allow for hysterisis between the switching)
then use the while command to check for the ignition input and after this was turned off start the timer out (using millis etc.) with a check for ignition being turned back on to run back to the beginning of the loop.
Arduino int is a variable type, 16 bit signed integer (as opposed to float) while unsigned long is a 32 bit unsigned integer. Just saying that you need to cover your basics much better.
What John Wasser posted is the kind of code you want. It permits segments of code inside of void loop() to act as independently running tasks, virtual parallel processing.
The Basics will give you terms/literacy you need to learn more.
The Microcontrollers section will teach about pins and might keep you from burning any up.
In Programming, get the 1st 3 links down will save you a lot of time messing around. The next 2 links will become important soon enough and I don't know why the FPGA link is there at all.
If you finish the rest, you've likely worn your "new" off and are no longer a beginner.