Hi there! I'm looking to set up a very simple pump controller. The goal is to have the pump (rl1) running for 15 minutes, then turning off for 5 to cool off, and so on. The part that is holding me up is that when the pump fills the tank, it lifts a float switch (pin 7) that should shut off the relay for the pump (rl1). I can't seem to figure out how to allow the Arduino to monitor the float switch while the timer/delay is running as well. I've tried delay() and millis() but I'm really new to this and am having some trouble with it. Could someone help me out? My current code is listed below. (red and grn are just used as status lights for rl1)
#include <ezButton.h>
ezButton toggleSwitch(7);
int red = 5;
int grn = 6;
int rl1 = 11;
int rl2 = 10;
void setup() {
Serial.begin(9600);
toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
toggleSwitch.loop(); // MUST call the loop() function first
int state = toggleSwitch.getState();
if (state == HIGH) {
digitalWrite(red, HIGH);
digitalWrite(grn, LOW);
digitalWrite(rl1, HIGH);
} else {
digitalWrite(red, LOW);
digitalWrite(grn, HIGH);
digitalWrite(rl1, LOW);
}
}
You need to use millis() for the timing so that you can do other things in loop() other than just the timing using delay(). Start by modifying the BlinkWithoutDelay example to use asymmetrical periods (15 mins and 5 mins)
Then add code to loop() to read the float switch input. When it is triggered turn off the relay and set a boolean to false. Use that boolean to control whether the relay turns on in the timing code
After fiddling around a bit, I ended up with this. Thank you so much for your help!
int red = 5; // Red LED
int green = 6; // Green LED
int PinPump = 11; // Water Pump Relay
int PinPump2 = 10; // Accessory Pump Relay
int PinFloat = 7; // Float Switch
const unsigned long MsecOn = 900000; // Time of which the pump runs for (15m)
const unsigned long MsecOff = 300000; // Time of which the pump cools down for (5m)
unsigned long msecPeriod;
unsigned long msecLst;
enum { Off = HIGH, On = LOW }; // Allows for substitution of HIGH for Off and LOW for On.
void loop ()
{
unsigned long msec = millis ();
if (msec - msecLst >= msecPeriod) {
msecLst = msec;
if (On == digitalRead (PinPump)) {
digitalWrite (PinPump, Off);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
msecPeriod = MsecOff;
}
else {
digitalWrite (PinPump, On);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
msecPeriod = MsecOn;
}
}
if (HIGH == digitalRead (PinFloat)) {
msecLst = msec;
digitalWrite (PinPump, Off);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
msecPeriod = MsecOff;
}
}
void setup () {
Serial.begin (9600);
pinMode (PinFloat, INPUT_PULLUP);
pinMode (PinPump, OUTPUT);
digitalWrite (PinPump, Off);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
msecPeriod = MsecOff;
}