Please Check my RTC code.

Greetings.

I just got my hands on an RTC module.

the goal is the light turns on from 5am till 8pm(16hours) while the pump on 10mins, off 4hours.

Kindly check if this is correct.

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

#define ONESEC 1000L
#define ONEMIN (60*ONESEC)
#define ONEHOUR (60*ONEMIN)


//light and pump 
const int pump = 7;
const int light = 8;



//Hours that the light is switched on and off
int LightOnHour = 16;
int LightOffHour = 8;



void setup() {
  // Start the serial port to communicate to the PC at 9600 baud
  Serial.begin(9600);

  //Get the time from the RTC
  setSyncProvider(RTC.get);

  //Arduino pin initialization
  pinMode(pump, OUTPUT);
  pinMode(light, OUTPUT);

  //Wire initialization
  Wire.begin();

  //if the light is supposed to be on turn it on
  if (hour() < LightOffHour || hour() >= LightOnHour)
  {
    digitalWrite(light, HIGH);
  }

  delay(150);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(pump,HIGH);
  delay(10*ONEMIN);
  digitalWrite(pump,LOW);
  delay(4*ONEHOUR);
 
}

thanks in advance

Does it compile? If not, it isn't correct.
If it does compile, does it do what you want it to do? If not, it isn't correct.

Pete

el_supremo:
Does it compile? If not, it isn't correct.
If it does compile, does it do what you want it to do? If not, it isn't correct.

Pete

well sir it doesn't do what I want it to do. how should I remedy it?

What does it do?

Pete

check 1st post, i edited it already.

okay i think i know where it's wrong.

the light runs for 16hours then only pump starts.

Using delay() to do 4-hour timing is not really a good idea. Four hours is 14400000 ms, which is a 24-bit number.

Go look at the "blink without delay" example.

Using delay like that is a bad idea. You loop should have logic like this.

Step 1. get the current time.

Is the light on, and it is time to turn it off ? >> turn it off

Is the light off, and it is time to turn it on ? >> turn it on.

Is the pump on, and it is time to turn it off ? >> turn it off

Is the pump off, and it is time to turn it on ? >> turn it on

end of loop( ), which will re-commence at step 1.

//if the light is supposed to be on turn it on
if (hour() < LightOffHour || hour() >= LightOnHour)
{
digitalWrite(light, HIGH);
}

This code is in setup( ), so it will only run once.

You obviously don't know what setup( ) and loop( ) are for, in the Arduino programming paradigm. You need to look at some of the elementary Arduino tutorials, so you understand that concept.