Need help on my project

Hello ,

I’m new here, and i don’t now if i’m posting in the right place but here goes.

i am creating a project for an automated watering system with an arduino nano, an Tiny RTC I2M module , a relay.

I started with only one watering schedule that consisted in once a day at a specific time, but i need to increase the watering for twice a day and i added another routine to the sketch and it does not work .
When i run the sketch only with the routine in green it works fine but when i add the part in red it simply does not do anything .
Can any one help me

//Criado por Nuno Protasio

//Sistema de rega temporizada

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;


void setup() {

  Serial.begin(9600);
  Wire.begin();

  rtc.begin();

  if (! rtc.isrunning())
  {
    Serial.println("RTC nao esta a trabalhar!");
  }
  //Usar o pin que quiser, eu usei o pin 4
  pinMode(4, OUTPUT);


}

void loop() {

  DateTime now = rtc.now();

  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();

  //O horario esta configurado para 24h
  //Pin 4 liga as 18:32 e desliga as 18:33
  if (now.hour() == 18 & now.minute() == 32 & now.second() == 0)
  {
    digitalWrite(4, HIGH);
  }

  else if (now.hour() == 18 & now.minute() == 33 & now.second() == 0)
  {
    digitalWrite(4, LOW);

  
    //Aqui começa o novo programa
    //Pin4 liga as 18:05 e desliga as 18:07 
  if (now.hour() == 18 & now.minute() == 37 & now.second() == 0)
  {
    digitalWrite(4, HIGH);
  }

  else if (now.hour() == 18 & now.minute() == 38 & now.second() == 0)
  {
    digitalWrite(4, LOW);
 
    Serial.println();
  delay(1000);
  }

}
    }

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

(deleted)

else if (now.hour() == 18 & now.minute() == 33 & now.second() == 0)
  {
    digitalWrite(4, LOW);


    //Aqui começa o novo programa
    //Pin4 liga as 18:05 e desliga as 18:07
  if (now.hour() == 18 & now.minute() == 37 & now.second() == 0)

If the first if statement is true, what are the odds of the second one also being true? A snowball in hell on a hot day has a better chance.

What do you mean by that

He means, if the time now is 18:33:00, what are the chances that it is now also 18:37:00 ?

(deleted)

Nunomsp: ```  if (now.hour() == 18 & now.minute() == 32 & now.second() == 0)  {    digitalWrite(4, HIGH);  }

 else if (now.hour() == 18 & now.minute() == 33 & now.second() == 0)  {    digitalWrite(4, LOW);

     //Aqui começa o novo programa    //Pin4 liga as 18:05 e desliga as 18:07  if (now.hour() == 18 & now.minute() == 37 & now.second() == 0)  {    digitalWrite(4, HIGH);  }

 else if (now.hour() == 18 & now.minute() == 38 & now.second() == 0)

I think you meant to use &&(boolean and) instead of &(bitwise AND). For more information see https://www.arduino.cc/en/Reference/BitwiseAnd and https://www.arduino.cc/en/Reference/Boolean

You have incorrectly nested your if clauses. Use apple-T (not sure what it is on widows) to auto-format your code. This helps make the nesting more obvious.

spycatcher2k: I already gave the solution in your OTHER post. >:(

Threads merged.

OP - To make coding on projects like this a lot easier on yourself - learn some fundamentals of coding structure. e.g. struct{} switch() and other tools.

#Define your 'constants' and pins up the top with meaningful names - then refer to them in the code. Break functional bits of code into functions() - which makes isolation and debugging a lot easier - along with future incremental development.

Good luck!

What really needs to happen here is that we need a struct for "watering schedule", and an array of them for the different schedules. You the iterate through them - if any are on, the water goes on.

This struct could include on time, off time, and a bit mask of pins that it controls. Bitwise-or the pin masks of every struct that is on now, and that tells you what pins to turn on.