Sprinkler relay control with I o T


#define PIN_RELAY_1  2 // the Arduino pin, which connects to the IN1 pin of relay module
#define PIN_RELAY_2  3 // the Arduino pin, which connects to the IN2 pin of relay module
#define PIN_RELAY_3  4 // the Arduino pin, which connects to the IN3 pin of relay module
#define PIN_RELAY_4  5 // the Arduino pin, which connects to the IN4 pin of relay module
#define PIN_SWITCH   6 //to start watering process

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  
  // initialize digital pin as an output.
  pinMode(PIN_RELAY_1, OUTPUT);
  pinMode(PIN_RELAY_2, OUTPUT);
  pinMode(PIN_RELAY_3, OUTPUT);
  pinMode(PIN_RELAY_4, OUTPUT);
  pinMode(PIN_SWITCH, INPUT);
}
// the loop function runs over and over again forever
void loop() {
  
  if(digitalRead(PIN_SWITCH));
  delay (1000);  
  digitalWrite(PIN_RELAY_1, LOW);
  delay (20000);
  digitalWrite (PIN_RELAY_1,HIGH);
  delay (1000);
  digitalWrite(PIN_RELAY_2, LOW);
  delay (20000);
  digitalWrite(PIN_RELAY_2, HIGH);
  delay (1000);
  digitalWrite(PIN_RELAY_3, LOW);
  delay (20000);
  digitalWrite (PIN_RELAY_3, HIGH);
  delay (1000);
  digitalWrite(PIN_RELAY_4, LOW);
  delay (20000);
  digitalWrite (PIN_RELAY_4, HIGH);

  }
}

MODERATER EDIT : moved the question outside of the code tags to make it easier to read

I have used this code using the arduino IDE both 1 and 2. When I convert it to the cloud I o T, I upload the code to the arduino RP2040 connect board. It uploads as it should and when I use the startbutton to start project, it turns on the first relay and runs as it should. my problem starts when the first relay stops with the delay and it resets my board to start over, and continues this process over and over. I have been trying to learn and implement the millis() function but haven't had much luck with it because of wanting to turn each relay on one and then the other.

Thanks for any help or thoughts

Put a Serial.println("setup"); statement in void setup() to see if the board is resetting completely, or if your sketch is only staying inside void loop()

Also... PIN_SWITCH as INPUT ... do you have that pin pulled low with an external resistor? If the pin floats (as it will), then it will randomly allow your loop to run.

I am not that familiar with that unit but is it possible it has a Watchdog timer? I have seen this embedded in some ESP processors.

thanks for the your input. yes I do have a resistor in circuit with the switch. The sketch works fine as it is written as long as I use the web editor, trouble is when I try and load this code to my arduino rp2040 connect, using the wifi and secret tab, that is when it starts resetting my board. After the first first relay comes on and runs its on cycle, when the board turns the first relay off is when the board resets itself. and will continue to do so. Thanks for any input on that problem. I have been working on trying to understand and use the millis , but am having hard time figuring out not so much how it works but to right the code that will make it work. The delays are long and this code I have posted the delays are shortened so I dont have to wait on them so long.

thanks for looking at my problem and I have seen about the Watchdog timer. I will have to do my research on that thought and I think it has to do with the delays(20000) . when I change all the delays to the (1000), it seems to work well with the both the web editor and when I use it with the I o T sketch. thanks for your thoughts and advice as I am new to coding. But I am having fun learning and it keeps my mind working

That is exactly what I found. From what I understand the main code written and buried in the system was written by Espressif Systems utilizes the watchdog timer. That code maintains the hardware etc and it is done in the background so we have no worries about it most of the time. All of our code is basically a function to that code. Code such as delay(), never ending loops for example waiting on an external function that does not happen etc are called blocking code as such they do not release control of the processor to other code and block the system until there thing is satisfied. I hope this helps.

Thanks and that is kind of what i was thinking my problem was. I have been looking at using the millis function but cant get it in my head on the coding I need to write for it. I am also assuming that I can use millis with a time that is needed for each timer to cycle, which i am looking at about 15 minutes each. Thanks for your thoughts and again any help will be appreciated.

Yes, with the IOT Cloud and the RP2040 there seems to be a "reset" routine that executes when the cloud connection is lost (which is often for me).

To address how to use the millis I included some code and as xpfs said use the serial prints to "document" where your sketch "is". I do that all the time in my code to see what my sketch is doing and then remove them during the final compile (as I don't normally monitor the serial output).

//how to use the millis as a delay using a IF statement (non-blocking)
long int ticks=0;

void setup() {
  Serial.begin(9600);
  ticks=millis();
}

void loop() {
  if(millis()>ticks+5000){ //experiment with this number to chnange the delay
    Serial.print("Ticks: ");
    Serial.println(millis());
    ticks=millis();
  }

}

Thanks for the input gives me something to try

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.