System not working

Hello Folks,
I have a sketch with RTC, I2C, 5v pump on a relay. Want to water my plant every day at a given time, the sketch has no errors, but when it's time to water it does not signal relay to run the pump...

Welcome to the forum

Please post it here, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 RTC;
#define RELAY_PIN 2

//--------------------------------
bool newData;           //new data on serial
bool watering = false;  //status if watering is allowed or not
bool wateredtoday = false;

int wateringhour = 1533;      // water at 9:00 o'clock in the morning (24h time format!)
float wateringlength = 4000;  //10 sec default watering (pumping time)

const int relaypin = 2;

//--------------------------------


void setup() {
  //------------------------------------------------------
  lcd.init();  // initialize the lcd
  lcd.init();
  lcd.backlight();
  //------------------------------------------------------

  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);



  Wire.begin();

  RTC.begin();

  //Refreshing time
  //remove "!", compile, upload to refresh the time in the RTC module
  //the time will be set to the time of the compilation
  if (!RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  Serial.println("Irrigation system.");

  //Fancy startup
  lcd.clear();
  lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
  lcd.print("Watering System");
  lcd.setCursor(0, 1);
  lcd.print("Start in 5...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 4...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 3...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 2...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 1...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
}

void loop() {

  printtime();
  checkSerial();
  checktime();
  executecommand();
}
//----------------------------------------------------------------------------------
void checkSerial() {
  //First we check if there is data on the serial port, i.e. we sent a command
  if (Serial.available() > 0) {

    newData = true;
  }

  //if there is a command, then we step inside and go through the possible commands
  if (newData == true) {


    {
      watering = true;  //change the boolean, so the watering will be ENABLED
    }


    {
      watering = false;  //change the boolean, so the watering will be DISABLED
    }



  } else {
  }
  newData = false;
}
//----------------------------------------------------------------------------------
void executecommand()  //execute the commands, i.e. checking which flag is en/dis-abled
{
  if (watering == true) {
    //start pump
    digitalWrite(relaypin, HIGH);


    //------------------------------------------------------
    //---print info on LCD
    lcd.clear();
    lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
    lcd.print("Watering");
    lcd.setCursor(0, 1);
    lcd.print("Running");  //You can write 16 Characters per line .
    //---Run pump for some time (defined in the beginning of the code as a variable)
    delay(wateringlength);
    //---Shut down pump, set pin to LOW
    digitalWrite(relaypin, LOW);
    //---Clean screen
    lcd.clear();
    //---Disable watering
    watering = false;
    //--Change the value of the bool, so we'll know that the watering had happened
    wateredtoday = true;
  } else {
    //do not do anything
  }
  //---------------------------
}

//----------------------------------------------------------------------------------

void printtime() {
  //it is just a basic RTC and LCD exercise: reading the date and time and printing it with some formatting
  DateTime now = RTC.now();
  //-----------------
  //--First line
  lcd.clear();
  lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
  lcd.print(now.year());
  lcd.setCursor(4, 0);
  lcd.print(".");
  lcd.setCursor(5, 0);
  lcd.print(now.month());
  lcd.setCursor(7, 0);
  lcd.print(".");
  lcd.setCursor(8, 0);
  lcd.print(now.day());
  lcd.setCursor(11, 0);
  lcd.print("water");
  //-----------------
  //--Second line
  lcd.setCursor(0, 1);
  lcd.print(now.hour());  //You can write 16 Characters per line .
  lcd.setCursor(2, 1);
  lcd.print(":");
  lcd.setCursor(3, 1);
  lcd.print(now.minute());
  lcd.setCursor(5, 1);
  lcd.print(":");
  lcd.setCursor(6, 1);
  lcd.print(now.second());
  lcd.setCursor(10, 1);
  //if the watering had happened, then we print it on the screen.
  if (wateredtoday == true) {
    lcd.print("true");
  } else {
    lcd.print("false");
  }

  //----------------------------------------
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(1000);
}
//----------------------------------------------------------------------------------

void checktime() {
  //Here, we check the time, so we can see if the watering should be started
  DateTime now = RTC.now();

  //after the watering time occured, we switch the variable back to false so when the
  //time occurs again (i.e. it is 9:00 in the morning) the watering will be started again
  if (now.hour() == (wateringhour + 1)) {
    //Serial.println("Watered");
    wateredtoday = false;
  }

  //If it is time to water, and we haven't watered, we enable watering
  if (now.hour() == wateringhour && wateredtoday == false) {
    //Serial.println("Not watered");
    watering = true;
    wateredtoday = true;
  }
}


  if (newData == true) 
{
    {
      watering = true;  //change the boolean, so the watering will be ENABLED
    }

    {
      watering = false;  //change the boolean, so the watering will be DISABLED
    }
}

  • Check your braces :woozy_face:

Thank you I will try it...

int wateringhour = 1533;    
. . .

if (now.hour() == (wateringhour + 1)) {
  • Say what ? 1533 water at 9:00 o'clock in the morning
    :thinking:



  • Suggest you slow down and revisit every line of code, ask yourself, does this line of code make sense ?

I change the actual time to water trying to see if it will run pump, instead of waiting 24hrs.

Hello, I don't understand the (wateringhour +1)
Also, I'm new to Arduino...

  • Put some Serial.print statements at strategic locations to check to see if variables are what you think they are.

What's the purpose, if I want to water at 0900 for 10 seconds?

Correct, I didn't write it, so I'm open to cleaning it up

I don't have the unit with me, will have to try the bracket change tonight.

Thank you for your time and knowledge, I'm still learning

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 RTC;
#define RELAY_PIN 2

//--------------------------------
bool newData;           //new data on serial
bool watering = false;  //status if watering is allowed or not
bool wateredtoday = false;

int wateringhour = 1533;      // water at 9:00 o'clock in the morning (24h time format!)
float wateringlength = 4000;  //10 sec default watering (pumping time)

const int relaypin = 2;

//--------------------------------


void setup() {
  //------------------------------------------------------
  lcd.init();  // initialize the lcd
  lcd.init();
  lcd.backlight();
  //------------------------------------------------------

  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);



  Wire.begin();

  RTC.begin();

  //Refreshing time
  //remove "!", compile, upload to refresh the time in the RTC module
  //the time will be set to the time of the compilation
  if (!RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  Serial.println("Irrigation system.");

  //Fancy startup
  lcd.clear();
  lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
  lcd.print("Watering System");
  lcd.setCursor(0, 1);
  lcd.print("Start in 5...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 4...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 3...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 2...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
  lcd.setCursor(0, 1);
  lcd.print("Start in 1...");  //You can write 16 Characters per line .
  delay(1000);                 //wait 3 sec
}

void loop() {
  printtime();
  checkSerial();
  checktime();
  executecommand();
}
//----------------------------------------------------------------------------------
void checkSerial() {
  //First we check if there is data on the serial port, i.e. we sent a command
  if (Serial.available() > 0) {
    newData = true;
  }

  //if there is a command, then we step inside and go through the possible commands
  if (newData == true) {

    {
      watering = true;  //change the boolean, so the watering will be ENABLED
    }


    {
      watering = false;  //change the boolean, so the watering will be DISABLED
    }



  } else {
  }
  newData = false;
}
//----------------------------------------------------------------------------------
void executecommand()  //execute the commands, i.e. checking which flag is en/dis-abled
{
  if (watering == true) {
    //start pump
    digitalWrite(relaypin, HIGH);


    //------------------------------------------------------
    //---print info on LCD
    lcd.clear();
    lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
    lcd.print("Watering");
    lcd.setCursor(0, 1);
    lcd.print("Running");  //You can write 16 Characters per line .
    //---Run pump for some time (defined in the beginning of the code as a variable)
    delay(wateringlength);
    //---Shut down pump, set pin to LOW
    digitalWrite(relaypin, LOW);
    //---Clean screen
    lcd.clear();
    //---Disable watering
    watering = false;
    //--Change the value of the bool, so we'll know that the watering had happened
    wateredtoday = true;
  } else {
    //do not do anything
  }
  //---------------------------
}

//----------------------------------------------------------------------------------

void printtime() {
  //it is just a basic RTC and LCD exercise: reading the date and time and printing it with some formatting
  DateTime now = RTC.now();
  //-----------------
  //--First line
  lcd.clear();
  lcd.setCursor(0, 0);  //Defining positon to write from first row,first column .
  lcd.print(now.year());
  lcd.setCursor(4, 0);
  lcd.print(".");
  lcd.setCursor(5, 0);
  lcd.print(now.month());
  lcd.setCursor(7, 0);
  lcd.print(".");
  lcd.setCursor(8, 0);
  lcd.print(now.day());
  lcd.setCursor(11, 0);
  lcd.print("water");
  //-----------------
  //--Second line
  lcd.setCursor(0, 1);
  lcd.print(now.hour());  //You can write 16 Characters per line .
  lcd.setCursor(2, 1);
  lcd.print(":");
  lcd.setCursor(3, 1);
  lcd.print(now.minute());
  lcd.setCursor(5, 1);
  lcd.print(":");
  lcd.setCursor(6, 1);
  lcd.print(now.second());
  lcd.setCursor(10, 1);
  //if the watering had happened, then we print it on the screen.
  if (wateredtoday == true) {
    lcd.print("true");
  } else {
    lcd.print("false");
  }

  //----------------------------------------
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(1000);
}
//----------------------------------------------------------------------------------

void checktime() {
  //Here, we check the time, so we can see if the watering should be started
  DateTime now = RTC.now();

  //after the watering time occured, we switch the variable back to false so when the
  //time occurs again (i.e. it is 9:00 in the morning) the watering will be started again
  if (now.hour() == (wateringhour + 1)) {
    //Serial.println("Watered");
    wateredtoday = false;
  }

  //If it is time to water, and we haven't watered, we enable watering
  if (now.hour() == wateringhour && wateredtoday == false) {
    //Serial.println("Not watered");
    watering = true;
    wateredtoday = true;
  }
}

Sounds good, Thank you