Setting up relay to activate Intermittenly

Hello everyone, recently i had the luxury to play around with my sister mega2560 and i totally loved it, however without guidance i’m kinda lost atm.

What I have already done is setting up the mega with 8ch relay board and ds3231 rtc clock … however when i tried to attempt to code i learnt the hard truth that I am really noob at it.

What I’m trying to do is to turn on AC pump for 1 minute for every 2 minutes off …

/* Relay Control - Handles "Relay is active-low" to assure no relay activation from reset until application is ready.

/*-----( Import needed libraries )-----*/
#include <DS3231.h>

/*-----( Declare Constants )-----*/

#define RELAY_ON 0      //relay setup 
#define RELAY_OFF 1

DS3231  rtc(SDA, SCL);  //ds3231 setup
Time t;
const int OnHour = 12;  
const int OnMin = 24;
const int OffHour = 12;
const int OffMin = 25;

/*-----( Declare objects )-----*/

/*-----( Declare Variables )-----*/

// Arduino Digital I/O pin number to 8ch relay board
#define RELAY1  52
#define RELAY2  50                    
#define RELAY3  48                        
#define RELAY4  46                       
#define RELAY5  44
#define RELAY6  42                     
#define RELAY7  40                        
#define RELAY8  38   

void setup()    /****** SETUP: RUNS ONCE ******/
{
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(RELAY1, RELAY_OFF);
  digitalWrite(RELAY2, RELAY_OFF);
  digitalWrite(RELAY3, RELAY_OFF);
  digitalWrite(RELAY4, RELAY_OFF);
  digitalWrite(RELAY5, RELAY_OFF);
  digitalWrite(RELAY6, RELAY_OFF);
  digitalWrite(RELAY7, RELAY_OFF);
  digitalWrite(RELAY8, RELAY_OFF);
  
  
  //---( THEN set pins as outputs )---- 
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  delay(4000); //Check that all relays are inactive at Reset
  

  //We set the serial port for 19200 baud and wait for connection:
  while (!Serial);
  Serial.begin(115200);
  rtc.begin();

}//--(end setup )---

void loop()
{

  t = rtc.getTime();            //ds3231 codes
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  
  startrelay1:delay(120000);
  
  //RELAY1 FOR PUMP TESTING TIMER 1 MINUTE ON 2 MINUTE OFF
  digitalWrite(RELAY1, RELAY_ON); //set the pump on
  Serial.println("PUMP ON");
  Serial.print(rtc.getTimeStr());
  delay(60000); // wait for a 60s
  digitalWrite(RELAY1, RELAY_OFF); //set the pump off
  Serial.println("PUMP OFF");
  Serial.print(rtc.getTimeStr());
  goto startrelay1;
}

this is what i have atm, tried to use millis and i lost it somewhere along the way … any kind souls to straighten me up ? …

thank you

Wow!

You’re going to get jumped all over. Get rid of the goto!!!

And use code blocks, not quotes for posting code.

/* Relay Control - Handles "Relay is active-low" to assure no relay activation from reset until application is ready.

/*-----( Import needed libraries )-----*/
#include <DS3231.h>

/*-----( Declare Constants )-----*/

#define RELAY_ON 0      //relay setup
#define RELAY_OFF 1

DS3231  rtc(SDA, SCL);  //ds3231 setup
Time t;
const int OnHour = 12; 
const int OnMin = 24;
const int OffHour = 12;
const int OffMin = 25;

/*-----( Declare objects )-----*/

/*-----( Declare Variables )-----*/

// Arduino Digital I/O pin number to 8ch relay board
#define RELAY1  52
#define RELAY2  50                   
#define RELAY3  48                       
#define RELAY4  46                       
#define RELAY5  44
#define RELAY6  42                     
#define RELAY7  40                       
#define RELAY8  38   

void setup()    /****** SETUP: RUNS ONCE ******/
{
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(RELAY1, RELAY_OFF);
  digitalWrite(RELAY2, RELAY_OFF);
  digitalWrite(RELAY3, RELAY_OFF);
  digitalWrite(RELAY4, RELAY_OFF);
  digitalWrite(RELAY5, RELAY_OFF);
  digitalWrite(RELAY6, RELAY_OFF);
  digitalWrite(RELAY7, RELAY_OFF);
  digitalWrite(RELAY8, RELAY_OFF);
 
 
  //---( THEN set pins as outputs )----
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  pinMode(RELAY5, OUTPUT);
  pinMode(RELAY6, OUTPUT);
  pinMode(RELAY7, OUTPUT);
  pinMode(RELAY8, OUTPUT);
  delay(4000); //Check that all relays are inactive at Reset
 

  //We set the serial port for 19200 baud and wait for connection:
  while (!Serial);
  Serial.begin(115200);
  rtc.begin();

}//--(end setup )---

void loop()
{

  t = rtc.getTime();            //ds3231 codes
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
 
  startrelay1:delay(120000);
 
  //RELAY1 FOR PUMP TESTING TIMER 1 MINUTE ON 2 MINUTE OFF
  digitalWrite(RELAY1, RELAY_ON); //set the pump on
  Serial.println("PUMP ON");
  Serial.print(rtc.getTimeStr());
  delay(60000); // wait for a 60s
  digitalWrite(RELAY1, RELAY_OFF); //set the pump off
  Serial.println("PUMP OFF");
  Serial.print(rtc.getTimeStr());
  goto startrelay1;
}

First of all, your goto startrelay1 loops back indefinitely so remove the earlier bit in the loop() to setup() and just let loop() do its thing. (and get rid of goto)

Moving along, in setup you have while(!Serial); BEFORE you've instantiated Serial, so while is going to wait a long (emphasis on long) time before it never (sic) leaves that loop. The wait should come AFTER Serial.begin, but in any event, that wait is old, old, old and was meant for a few processors of which the newer ones don't apply, so get rid of all together.

When your AC service person discovers what you have been doing with the AC, you will have to get another service person.

Paul

And why use delay() that locks up the processor for the time period, when you have an RTC at your fingertips? :slight_smile:

DKWatson:
First of all, your goto startrelay1 loops back indefinitely so remove the earlier bit in the loop() to setup() and just let loop() do its thing. (and get rid of goto)

this much i understand, i've get rid of it

outsider:
And why use delay() that locks up the processor for the time period, when you have an RTC at your fingertips? :slight_smile:

can't decide which libraries to get and how to code it

Without the RTC, and you're not using it anyway, try a library called TimedAction. I've used it in the past. Quite handy.

The other, which works jointly with an RTC and the Time library, is TimeAlarms.