Relay Questions

So im fairly new to Arduino, i did try to make a project a few years ago but ended up giving up.

i have some very simple skills, (the ability to turn on off relays and set delays)

i am basically trying to use a relay as a timer, however when using the delay function everything seems to pause until the delay has finished.

the problem i have is im trying to use 2x relays
relay 1 to turn a pump on and off once an hour for 10 mins
relay 2 to turn a led bulb on and off, on for 8 hours off for 16

could someone point me in the direction of where to start or an example. i have done a fair bit of googling but ive not had any luck

int WATERPUMP = 2; // relaypin for waterpump
int LIGHTS = 3; // relaypin for lights

int PumpOn = 1000; // set time for pump to be on msecs
int PumpOff = 1000; // set time for pump to be off msecs

int LightsOn = 1000; // set time for lights to be on msecs
int LightsOff= 1000; // set time for lights to be off msecs

void setup() {
  // put your setup code here, to run once:
  pinMode(WATERPUMP, OUTPUT);
  pinMode(LIGHTS, OUTPUT);

  digitalWrite(WATERPUMP, LOW);
  digitalWrite(LIGHTS, LOW);

  Serial.begin(9600);
}

void loop() {
  
  // put your main code here, to run repeatedly:
  digitalWrite(WATERPUMP, HIGH);
  Serial.println("WATERPUMP ON");

  delay(PumpOn);
 
  digitalWrite(WATERPUMP, LOW);
  Serial.println("WATERPUMP OFF");
  delay(PumpOff);

//  delay(delayValue);

  digitalWrite(LIGHTS, LOW);
  Serial.println("LIGHTS ON");
  delay(LightsOn);
  
  digitalWrite(LIGHTS, HIGH);
  Serial.println("LIGHTS OFF");
  delay(LightsOff);
}

Show your code.
Don't use delay(), use millis().

This is what you need to learn. Give it a try. If you get stumped, post your best try, describe what the code actually does and how that differs from what you want.

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

look this over

struct Valve {
    const byte    Pin;
    unsigned long MsecOn;
    unsigned long MsecOff;
    const char   *desc;

    unsigned long msecLst;
    unsigned long msecPeriod;
};

Valve valves [] = {
    { 10, 500, 500, "V1" },
    { 11, 100, 300, "V2" },
    { 12, 250, 100, "V3" },
    { 13, 450, 200, "V4" },
};
#define Nvalve  (sizeof(valves)/sizeof(Valve))

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    Valve *v = valves;
    for (unsigned n = 0; n < Nvalve; n++, v++)  {
        if (msec - v->msecLst > v->msecPeriod)  {
            v->msecLst = msec;

            Serial.println (v->desc);
            if (On == digitalRead (v->Pin))  {
                v->msecPeriod = v->MsecOff;
                digitalWrite(v->Pin, Off);
            }
            else  {
                v->msecPeriod = v->MsecOn;
                digitalWrite(v->Pin, On);
            }
        }
    }
}

void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < Nvalve; n++)  {
        pinMode (valves [n].Pin, OUTPUT);
    }
}

A simpler demo along the several things at a time line.

// independant on and off times for 2 realys plus toggle a third with a 
// momentary push switch.  Switch wired to ground (active LOW) and relays active HIGH. 


const byte buttonPin = 5;
const byte relay1Pin = 6;
const byte relay2Pin = 7;
const byte relay3Pin = 8;

unsigned long relay1OnTime = 30000;
unsigned long relay1OffTime = 2000;
unsigned long relay2OnTime = 50000;
unsigned long relay2OffTime = 10000;

unsigned long currentMillis = 0;

void setup()
{
   Serial.begin(115200);
   pinMode(buttonPin, INPUT_PULLUP);
   pinMode (relay1Pin, OUTPUT);
   pinMode (relay2Pin, OUTPUT);
   pinMode (relay3Pin, OUTPUT);
}

void loop()
{
   currentMillis = millis();
   checkButton(); // relay3 toggle control
   relay1();
   relay2();
}

void relay1()
{
   static bool thisMode = false;
   static unsigned long timer = 0;
   static unsigned long interval = relay1OnTime;
   if (currentMillis - timer >= interval)
   {
      timer = currentMillis;
      if (thisMode)
      {
         //Serial.println("ON");
         digitalWrite(relay1Pin, HIGH);
         interval = relay1OnTime;
      }
      else
      {
         //Serial.println("OFF");
         digitalWrite(relay1Pin, LOW);
         interval = relay1OffTime;
      }
      thisMode = !thisMode;
   }
}

void relay2()
{
   static bool thisMode = true;
   static unsigned long timer = 0;
   static unsigned long interval = relay2OnTime;
   if (currentMillis - timer >= interval)
   {
      timer = currentMillis;
      if (thisMode)
      {
         //Serial.println("ON");
         digitalWrite(relay2Pin, HIGH);
         interval = relay2OnTime;
      }
      else
      {
         //Serial.println("OFF");
         digitalWrite(relay2Pin, LOW);
         interval = relay2OffTime;
      }
      thisMode = !thisMode;
   }
}

void checkButton()  // toggle relay3
{
   static unsigned long timer = 0;
   unsigned long interval = 20;
   if (currentMillis - timer >= interval)
   {
      timer = currentMillis;
     
      static bool lastButtonState = HIGH;
      bool buttonState = digitalRead(buttonPin);
      if (buttonState != lastButtonState)
      {
         if(buttonState == LOW)
         {
            digitalWrite(relay3Pin, !digitalRead(relay3Pin));            
         }
         lastButtonState = buttonState;
      }
   }
}

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