Using Millis to turn relay on and off

You missed a colon out of the address

This works https://create.arduino.cc/projecthub/394412/dosing-peristaltic-pump-pwm-ssr-hx711-scale-and-an-fsm-da9b06

Try this.

#include "blinker.h"

// So everyone wants newbies to write blink without delay. This is good
// because delay() is bad. Well, here's the simple way to do it..

#define pumpPin   13 // Change this to suit your needs.
#define pumpTime  5.0 * 60.0 * 1000.0   //  5 minutes * 60 sec * 1000 ms -> time in Ms
#define soakTime  2.0 * 60.0 * 60.0 * 1000.0   // 2 hours * 60 min * 60 sec * 1000 ms -> time in Ms

    
// Allocate a global blinker object.
blinker aBLinker(pumpPin,pumpTime,soakTime + pumpTime);        

void setup() {

   // Fire it up.
   aBLinker.setOnOff(true);   
}


void loop() {

   // blinker is an idler. Calling idle() once in loop lets ALL idlers run.
   idle();                    
}

You'll need LC_baseTools from the IDE library manager to make this compile.

-jim lee

Frustration level high. I have poured over the code to no end. I scrapped everything and started a new. The relay comes on but nothing else. Again, I need this to run, without blocking. Intended to have pump run for 4 minutes and off for 4 hours. This code is just set for 1 second. I have written in Serial print to see how far the code runs. I only get to "PUMP STARTING". Can anyone help me?

const int relayPin = 8;
const int relayInterval = 1000;
const int waterDuration = 1000;
byte relayState = LOW;
unsigned long currentMillis = 0;
unsigned long millis();
unsigned long previousRelayMillis = 0;
void setup() {
Serial.begin(9600);
Serial.println("PUMP STARTING");

pinMode(relayPin, OUTPUT);
}

void loop() {

currentMillis = millis();
}

void relay() {

if (relay == HIGH) {

if (currentMillis - previousRelayMillis >= waterDuration) {
relayState = LOW;
Serial.print("WATERING");
previousRelayMillis += relayInterval;

}
}

else {
if (currentMillis - previousRelayMillis >= waterDuration) {
relayState = HIGH;
Serial.print("PUMP OFF");
previousRelayMillis += waterDuration;
}
}
}

The FSM in this post seems made to order.

Here is the complete code example of the unequal blink without delay referred to in #1 and #4. The interval switches between the on and off times. Revise as you need.

const byte ledPin =  LED_BUILTIN;// the number of the LED pin

byte ledState = LOW;             
unsigned long previousMillis = 0;     

unsigned long intervalOn = 4*60*1000;  //Four minutes on
unsigned long intervalOff= 4*60*60*1000; //Four hours off
unsigned long interval = intervalOff; //start with pump off

void setup() {
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
      interval = intervalOn;
    } else {
      ledState = LOW;
      interval = intervalOff;
    }
    digitalWrite(ledPin, ledState);
  }
}
2 Likes

Thank you everyone for your input and advice. What a great community. My hats off to the folks that offered up the blink method. Wow, it looks so simple I didn't think it would work. But it works flawlessly and is very adaptable. Cheers

I am glad that you finally took note and that it works for you

Good luck with your project

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.