Arduino irrigation help

hi guys, I always go ahead with my projects, but now that I have installed the part of the garden I am in trouble with the sketch for irrigation
I would like to be able to manage irrigation times,
type 10 minutes zone1, stop, 10 minutes zone2, stop etc.
I was thinking with the time function, but I’m not an expert,
or with cilci if then else
or with for loops,
help me

@Massimotruglio83 i did not get what this mean. can you provide more details.

example
for [ciclo1=0;CICLO<=100;ciclo1++];

digitalWirte (1,HIGH);]

for then arduino is disconed to remain in loop with the cycle for …
Help me

you want zone1 to remain ON for 10 minutes and then OFF for rest of the period?

Can you share the code you have so far?

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "
char clientID[] = "

#define RELAY_DIGITAL_PIN 3
#define RELAY_DIGITAL_PIN2 4
#define RELAY_DIGITAL_PIN3 5
#define RELAY_DIGITAL_PIN4 7

int luminosita;  //Il valore letto dalla fotoresistenza
int umidita;
int sensorepioggia;
unsigned long lastMillis=0;
int CICLO=1;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
   pinMode(RELAY_DIGITAL_PIN, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN2, OUTPUT); 
   pinMode(RELAY_DIGITAL_PIN3, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN4, OUTPUT); 
   delay(100);
   digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  Cayenne.virtualWrite(3, 0);
  Cayenne.virtualWrite(4, 0);
  Cayenne.virtualWrite(5, 0);
  Cayenne.virtualWrite(6, 0);
  
  
  Cayenne.virtualWrite(12,0);
  Cayenne.virtualWrite(11, 0);
  Cayenne.virtualWrite(10, 0);
  CAYENNE_LOG("Sync data");
  Serial.print("valore del ciclo =");
  Serial.println(CICLO);
  

   pinMode(A0, INPUT);
   pinMode(A1, INPUT);
   pinMode(8, INPUT);
  pinMode(2,INPUT);
   }

void loop() {
  Cayenne.loop();
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    luminosita = digitalRead(2);  //Lettura della luminosità
 
  umidita = analogRead(A1);
  Serial.print("luminosita = ");
  Serial.println(luminosita);
  Serial.print("umidita = ");
  Serial.println(umidita);
  sensorepioggia=digitalRead(8);
   Serial.print("sensore pioggia =");
  Serial.println(sensorepioggia);
   
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    
      Serial.print("Finito invio");
  
  
}}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  
  Cayenne.luxWrite(1, luminosita);
  Cayenne.virtualWrite(2, umidita,TYPE_RELATIVE_HUMIDITY);
   Cayenne.virtualWrite(8, sensorepioggia);
  //sensorepioggia=digitalRead(10);
  //Serial.println(sensorepioggia);
  //Cayenne.virtualWrite(10, sensorepioggia);
  
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
 CAYENNE_IN(3)
 {
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN, LOW);
  }
  }
  

CAYENNE_IN(4)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN2,LOW);
  }
  
}
 CAYENNE_IN(5)
{
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN3, LOW);
  }
  }
  

CAYENNE_IN(6)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN4, LOW);
  }
  
}
CAYENNE_IN(10)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) { Cayenne.virtualWrite(10, 0);
                           Cayenne.virtualWrite(11, 0);
  
    
   } else {Cayenne.virtualWrite(10,1);
           Cayenne.virtualWrite(11,1);
           Cayenne.virtualWrite(21,1);
   delay(100);
    for(int ciclo1=0;ciclo1<=CICLO;ciclo1++){
      digitalWrite(RELAY_DIGITAL_PIN2, LOW);
      Serial.println("sto facendo il ciclo 1");
      Serial.println(ciclo1);
      delay(1000);
       }
        Cayenne.virtualWrite(21,0);
         Cayenne.virtualWrite(22,1);
      digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
       for(int ciclo2=0;ciclo2<=CICLO;ciclo2++){
      digitalWrite(RELAY_DIGITAL_PIN3, LOW);
      Serial.println("sto facendo il ciclo 2");
      Serial.println(ciclo2);
      delay(1000);
       }
        Cayenne.virtualWrite(22,0);
         Cayenne.virtualWrite(23,1);
      digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
      for(int ciclo3=0;ciclo3<=CICLO;ciclo3++){
      digitalWrite(RELAY_DIGITAL_PIN4, LOW);
      Serial.println("sto facendo il ciclo 3");
      Serial.println(ciclo3);
      delay(1000);
       }
        Cayenne.virtualWrite(23,0);
      digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
      
      delay(100);
      CAYENNE_LOG("Sync data");
      Cayenne.virtualWrite(11, 0);
      Cayenne.virtualWrite(10, 0);
      Cayenne.virtualWrite(21,0);
      Cayenne.virtualWrite(22,0);
      Cayenne.virtualWrite(21,0);
      Serial.println("finito ciclo e update valore");
  }
    
    
  }
  
CAYENNE_IN(12)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1
  CICLO=(currentValue*10);
  Serial.println(CICLO);
  
  
}
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

you want to change this code? in which you want RELAY_DIGITAL_PIN2 to remain ON for 10 minutes then OFF. then RELAY_DIGITAL_PIN to remain ON for 10 min and OFF?

yes

what is the value of CICLO? from your code it is 1.
P.S you have used slider to set CICLO value.

@Massimotruglio83 this basic structure to turn ON and OFF relays for x time.

void loop()
{
  Cayenne.loop();
  if (x == 1)
  {
    if (millis() - lastMillis > y && a == 1) 
    {
      digitalWrite(5, HIGH);
      b = 1;
      lastMillis1 = millis();
      digitalWrite(5, LOW);
      a = 0;
    }
    if (b == 1)
    {
      if (millis() - lastMillis1 > y)
      {
        digitalWrite(6, HIGH);
        b = 0;
        x = 0;
      }
    }
  }
}

CAYENNE_IN(10)
{
  x = getValue.asInt(); // 0 to 1
  lastMillis = millis();
  digitalWrite(5, LOW);
  a = 1;
}
CAYENNE_IN(12)
{
  z = getValue.asInt(); // 0 to 1
  y = z * 1000;
}

luminosita = 1
umidita = 235
sensore pioggia =0
Finito invio
valore millis =18718
valore lastmillis =0
valore differenza tempo =18775
inizio ciclo da =10
[19099] ⸮⸮S3⸮
⸮/⸮?⸮O⸮⸮⸮⸮⸮⸮0⸮⸮r⸮⸮/⸮?⸮O⸮_⸮⸮⸮⸮N3<⸮d⸮r⸮⸮⸮⸮⸮⸮e⸮⸮/⸮?⸮O⸮_⸮⸮⸮⸮⸮0⸮⸮⸮⸮ ⸮0⸮⸮⸮⸮⸮0⸮⸮ ⸮m⸮⸮⸮⸮⸮e⸮⸮⸮⸮⸮⸮⸮⸮*⸮7⸮C⸮[⸮⸮⸮⸮⸮2+<⸮3K\⸮⸮0⸮⸮⸮⸮⸮⸮2+<*⸮⸮⸮⸮⸮⸮⸮en⸮⸮⸮⸮⸮e ⸮0⸮@⸮Q⸮⸮⸮⸮J1+<⸮3K⸮,⸮,*⸮⸮⸮⸮⸮⸮⸮e⸮⸮⸮⸮0⸮⸮⸮⸮⸮⸮2 ⸮0⸮@⸮Q⸮⸮J1⸮3⸮⸮p⸮*⸮+⸮⸮⸮⸮⸮⸮⸮⸮,?O^⸮⸮MAC: FE-36-22-58-D9-9F
[514] IP: 192.168.1.210
[515] Connecting to mqtt.mydevices.com:1883
[871] Connected
valore del ciclo =1

why?!

i dint get what you are saying. can you provide more detail.

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.

#define RELAY_DIGITAL_PIN 3
#define RELAY_DIGITAL_PIN2 4
#define RELAY_DIGITAL_PIN3 5
#define RELAY_DIGITAL_PIN4 7

int luminosita;  //Il valore letto dalla fotoresistenza
int umidita;
int sensorepioggia;

int CICLO=1;
int x=0;
int a=0;
int b=0;
int c=0;

unsigned long lastMillis=0;
unsigned long lastMillis1=0;
unsigned long lastMillis2=0;
unsigned long lastMillis3=0;


int y=0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
   pinMode(RELAY_DIGITAL_PIN, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN2, OUTPUT); 
   pinMode(RELAY_DIGITAL_PIN3, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN4, OUTPUT); 
   
   digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  Cayenne.virtualWrite(3, 0);
  Cayenne.virtualWrite(4, 0);
  Cayenne.virtualWrite(5, 0);
  Cayenne.virtualWrite(6, 0);
  
  
  Cayenne.virtualWrite(12,0);
  Cayenne.virtualWrite(11, 0);
  Cayenne.virtualWrite(10, 0);
  CAYENNE_LOG("Sync data");
  Serial.print("valore del ciclo =");
  Serial.println(CICLO);
  

   pinMode(A0, INPUT);
   pinMode(A1, INPUT);
   pinMode(8, INPUT);
  pinMode(2,INPUT);
   }

void loop() {
  Cayenne.loop();
  if (x == 1)
  {
    if (millis() - lastMillis > y && a == 1) 
    { 
      digitalWrite(RELAY_DIGITAL_PIN, HIGH);
      b = 1;
      lastMillis1 = millis();
      digitalWrite(RELAY_DIGITAL_PIN2, LOW);
      a = 0;
      Serial.println("iniziato Ciclo If");
      Cayenne.virtualWrite(21,0);
       Cayenne.virtualWrite(22,1);
    }
    if (b == 1)
    {
      if (millis() - lastMillis2 > y && b == 1)
      { 
        digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
        b = 0;
        lastMillis2 = millis();
        c =1;
        digitalWrite(RELAY_DIGITAL_PIN3, LOW);
        Serial.println("secondo step");
        Cayenne.virtualWrite(22,0);
        Cayenne.virtualWrite(23,1);
      }
    }
    if (c == 1)
    {
      if (millis() - lastMillis3 > y && c == 1)
      { 
        digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
        c = 0;
        lastMillis3 = millis();
        
       
        Serial.println("terzo step");
     
        Cayenne.virtualWrite(23,0);
         Cayenne.virtualWrite(10,0);
          Cayenne.virtualWrite(11,0);
           Serial.println("fine ciclo irrigazione");
      }
  }
  
  
}

    
  
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  
  Cayenne.virtualWrite(1, luminosita);
  Cayenne.virtualWrite(2, umidita,TYPE_RELATIVE_HUMIDITY,UNIT_PERCENT);
   Cayenne.virtualWrite(8, sensorepioggia);
   luminosita = !digitalRead(2);  //Lettura della luminosità
 
  umidita = analogRead(A1);
  Serial.print("luminosita = ");
  Serial.println(luminosita);
  Serial.print("umidita = ");
  Serial.println(umidita);
  sensorepioggia=digitalRead(8);
   Serial.print("sensore pioggia =");
  Serial.println(sensorepioggia);
   
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    
      Serial.println("Finito invio");
  //sensorepioggia=digitalRead(10);
  //Serial.println(sensorepioggia);
  //Cayenne.virtualWrite(10, sensorepioggia);
  
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
 CAYENNE_IN(3)
 {
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN, LOW);
  }
  }
  

CAYENNE_IN(4)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN2,LOW);
  }
  
}
 CAYENNE_IN(5)
{
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN3, LOW);
  }
  }
  

CAYENNE_IN(6)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN4, LOW);
  }
  
}
CAYENNE_IN(10)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
   x = getValue.asInt(); // 0 to 1
  lastMillis = millis();
  digitalWrite(RELAY_DIGITAL_PIN , LOW);
 
  a = 1;
  Serial.println(" inizio ciclo da  =");
  Serial.println(y);
  Cayenne.virtualWrite(11,1);
  Cayenne.virtualWrite(21,1);
  }
  
CAYENNE_IN(12)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
 
  CICLO = getValue.asInt(); // 0 to 1
  y = CICLO * 1000;
  Serial.print(" Valore variabile ciclo =");
  Serial.println(y);
}
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

the code works for example on esp8266 to perfection. with arduino and the w5100 freezes, while with the w5500 no. because?!!

#define CAYENNE_DEBUG’ add this line and see what is the output in serial monitor.

Simple.
It freezes… it doesn’t do anything anymore

@Massimotruglio83 i tested your code and it worked without any problem on my arduino uno and w5100 for 10 minutes.

i hope you are not adding this code. pin 10 11 12 13, they are reserved for communication.

forgive me, where did you see the pins 10,11,12 engaged?

It’s commented out in your code, he added a snip in his post

1 Like

@Massimotruglio83 did you made the changes and tried the code?