Hello! First post, so forgive me if I miss something that I should include.
I'm trying to power a 12v water pump to run every 3 hours for 15 minutes (the exact timing is not a big deal, a few minutes early or late would be fine). Every code I've tried works at first, but ultimately stops after the first loop (unless I turn the delay wayy low).
I've tried the blinkWithoutDelay example, and a few different codes:
const int PumpPin = 3;
constexpr unsigned long OffDuration = 20000;// OFF time for Pump
constexpr unsigned long OnDuration = 3000;// ON time for Pump
constexpr unsigned long PumpTimes[] {OffDuration,OnDuration};
void setup()
{
pinMode(PumpPin, OUTPUT); // define LEDpin as output
digitalWrite(PumpPin, LOW); // set initial state
}
void loop()
{
static unsigned long timeStamp=millis();
if (millis()-timeStamp>=PumpTimes[digitalRead(PumpPin)])
{
timeStamp=millis();
digitalWrite(PumpPin,digitalRead(PumpPin)^1);
}
}
And the very first thing I tried, the most basic code of them all:
const int RELAY_PIN = 3; // the Arduino pin, which connects to the IN pin of relay
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin as an output.
pinMode(RELAY_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(RELAY_PIN, LOW);
delay(14400000 );
digitalWrite(RELAY_PIN, HIGH);
delay(780000);
}```
Both the relay and Arduino Uno board are using a 12v power cable, and with small time delays it works fine.
Any advice is greatly appreciated, thanks!
Correct me if I'm wrong, but the second code I tried used millis did it not?
const int PumpPin = 3;
constexpr unsigned long OffDuration = 14400000 ;// OFF time for Pump
constexpr unsigned long OnDuration = 780000;// ON time for Pump
constexpr unsigned long PumpTimes[] {OffDuration,OnDuration};
void setup()
{
pinMode(PumpPin, OUTPUT); // define LEDpin as output
digitalWrite(PumpPin, LOW); // set initial state
}
void loop()
{
static unsigned long timeStamp=millis();
if (millis()-timeStamp>=PumpTimes[digitalRead(PumpPin)])
{
timeStamp=millis();
digitalWrite(PumpPin,digitalRead(PumpPin)^1);
}
}
Perhaps I did something wrong there (aside from those times I was using for shorter tests), I can give it another shot and report back tomorrow (lil late here in EST).
Edit: I missed your edit, sorry! The only problem I have with that is that shorter times seem to work fine, I figured it might be a number limit but if I'm not mistaken it can handle MS up to 49 days.
For simplicity sake, I followed this guide pretty closely, the only thing I switched from the supply list was the pump and a 12v power cable for the Arduino instead of 9. The relay is using the 5 volt pin (tested with a multimeter), and it has a 5 volt coil so that part should match up.
As for that code sample, I will test it out tomorrow. Like I said, everything above that I tried worked fine for short periods (if I did a 1 min on 5 mins off, etc.), I only noticed issues with the high values so I will leave it set to 3 hours.