1

I'm working on an agriculture system and one of my partners (who was taking care of the automation side of things) has gone MIA and we are left with arduino code running our systems that we have a basic understanding of. We're in a bit of a pickle until we can get another person on board who has this kind of expertise and would greatly appreciate any help from anyone who can assist us in modifying a few parameters of the program.

The arduino is controlling one of the generic 8 relay boards hooked up to mains voltage and 8 outlets. The outlets control lights, cO2, water pumps, and fans (which are programmed to activate when temp/humidity reaches certain levels).

We presently understand how to alter how long the relays are activated for, but what we are trying to understand is how to modify the interval at which these relays are activated (cO2/water is every 150 minutes, we would like to alter this). So for example, how would I alter the code to release cO2 every 90 minutes and activate the water pump every 5 minutes?

Any help on this matter would be greatly appreciated! Thank you so much!

#include <dht.h>
#include <dht.h>

dht DHT;
#define dataPin 7
//#define DHT11_PIN 7

#define CH1 1   // Connect Digital Pin 8 on Arduino to CH1 on Relay Module
#define CH2 2   // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
#define CH3 3   // Connect Digital Pin 8 on Arduino to CH1 on Relay Module
#define CH4 4   // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
#define CH5 5   // Connect Digital Pin 8 on Arduino to CH1 on Relay Module
#define CH6 6   // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
#define CH7 9   // Connect Digital Pin 8 on Arduino to CH1 on Relay Module
#define CH8 8   // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
int seconds = 0;
int minutes = 1;
int hours = 0;
int days = 0;
int temp = 28; // not used
int tempset = 25; // turn on fan temp
int minmil = 1000; //clock 60000
int watertime = 8000; //water for 8 seconds =8000
int co2time = 8000; //water for 8 seconds =8000


void setup() {
  //Setup all the Arduino Pins
  pinMode(CH1, OUTPUT);//Lights
  pinMode(CH2, OUTPUT);//Water
  pinMode(CH3, OUTPUT);// CO2
  pinMode(CH4, OUTPUT);//FAN
  pinMode(CH5, OUTPUT);//FAN
  pinMode(CH6, OUTPUT);//FAN
  pinMode(CH7, OUTPUT);//FAN
  pinMode(CH8, OUTPUT);
}

void loop() {
  int readData = DHT.read11(dataPin); // Reads the data from the sensor
  float t = DHT.temperature; // Gets the values of the temperature
  float h = DHT.humidity; // Gets the values of the humidity
   Serial.print(minutes,2);
 Serial.print(" minutes  ");
 Serial.print(hours,2);
 Serial.print(" hours ");
 Serial.print(days,2);
 Serial.print(" days ");
 Serial.println();



//int chk = DHT.read11(DHT11_PIN);
 seconds=seconds+1;
   if (seconds >= 59) {
 minutes = minutes + 1;//adds to count
    seconds = 0;
  }
  if (minutes >= 59) {
    hours = hours + 1;
    minutes = 0;
  }
  if (hours >= 23) {
    days = days + 1;
    hours = 1;
  }
  if (days >= 31) {
    days = 1;
  }
  if ((days <= 3) && (hours < 19)) {

    digitalWrite(CH1, LOW);//Lights on for first 3 days 12 hours per Day

  }
  if ((days <= 3) && (hours >= 19)) {

    digitalWrite(CH1, HIGH);//Lights on for first 3 days 12 hours per Day

  }

  if ((days > 3) && (hours < 16)) {

    digitalWrite(CH1, LOW);//Lights on for first 3 days 12 hours per Day

  }
  if ((days > 3) && (hours >= 16)) {

    digitalWrite(CH1, HIGH);//Lights on for first 3 days 12 hours per Day

  }

  if (tempset < t) { // TURN ON FANS IF ABOVE TEMP SET POINT
    digitalWrite(CH4, LOW);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH5, LOW);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH6, LOW);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH7, LOW);//Lights on for first 3 days 12 hours per Day
  }
  if (tempset >= t) { // TURN ON FANS IF ABOVE TEMP SET POINT
    digitalWrite(CH4, HIGH);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH5, HIGH);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH6, HIGH);//Lights on for first 3 days 12 hours per Day
    digitalWrite(CH7, HIGH);//Lights on for first 3 days 12 hours per Day
  }

 // if (( (hours % 2) == 0) && (minutes == 30)) { //Water every 2 hours @ 30 minute Mark
if (minutes == 3) { //Water every 2 hours @ 30 minute Mark
    digitalWrite(CH2, LOW);//
    delay(watertime); //how long water pump is engaged
    minutes = minutes + 1;
  }
  else {
    digitalWrite(CH2, HIGH);//
  }
 // if (( (hours % 2) == 0) && (minutes == 45)) { //CO2 Released every 2 hours @ 45 minute Mark
 if  (minutes == 5) { //CO2 Released every 2 hours @ 45 minute Mark
    digitalWrite(CH3, LOW);//CO2
    delay(co2time); //how long CO2 is released
    minutes = minutes + 1;
  }
  else {
    digitalWrite(CH3, HIGH);// no CO2
  }
  delay(minmil);  // (60000) - 1 minute Delay
}

Question: how could we alter the code in order to release co2 every 90 minutes?

  • Do a counter for minutes and every 90 minutes you release co2 and reset the counter. – MatsK Oct 20 '17 at 13:56
2

Straight answer to the question as asked:

To make it do this every 90 minutes would be

if ((hours / 60 + minutes) % 90) == 0) {

If you want something to happen every 5 minutes you would write it like this:

if ((minute % 5) == 0) {

I hope you can find someone who can make some improvements on this. There are much better ways to do it, beginning with a DS3231 Real-Time Clock module.

Good luck with this.

3

Just about all the comments are lying, mostly from copy paste-code-edit where the comment wasn't edited. It's a great example of why comments should not be used to explain code.

The timing code is full of off-by-one errors. The seconds go from 0 to 58, minutes also goes from 0 to 58, hours goes from 1 to 22, and days goes from 1 to 30.

The comments say that it runs the loop() once every minute but instead it's once a second.

There is no hysteresis on the fan control. Which means that in the worst case the fans can cycle on and off every time it runs through loop().

The CH1, CH2, etc. can be named much better like RELAY_LIGHT, RELAY_WATER, RELAY_CO2, etc. After you do that you won't need the comment on each digitalWrite.

Turning off the water and CO2 after they have been activated can be done by setting a timestamp of when to turn it off in the globals and then checking that:

if (((hours % 3 == 0 && minutes == 0)  || 
    (hours % 3 == 1 && minutes == 30)) && seconds == 0) { 
    digitalWrite(RELAY_CO2, LOW);
    CO2RelayOffDays = days;
    CO2RelayOffHours = hours;
    CO2RelayOffMinutes = minutes
    CO2RelayOffSeconds= seconds + CO2_ON_TIME;
    if(CO2RelayOffSeconds >= 60){
        CO2RelayOffSeconds -= 60;
        CO2RelayOffMinutes++;
    }
    if(CO2RelayOffMinutes >= 60){
        CO2RelayOffMinutes -= 60;
        CO2RelayOffHours++;
    }
    if(CO2RelayOffHours >= 24){
        CO2RelayOffHours -= 24;
        CO2RelayOffDays++;
    }
    if(CO2RelayOffDays >=30){
        CO2RelayOffDays -= 30;
    }
}

if(days == CO2RelayOffDays && hours == CO2RelayOffHours && minutes == CO2RelayOffMinutes && seconds == CO2RelayOffSeconds){
    digitalWrite(RELAY_CO2, HIGH);
}

Every 90 minutes means when hours:minutes (modulo 3 hours) is equal to 0:00 or 1:30. But another option is keeping a timestamp of when to turn it on next by adding 1 hour and 30 seconds each time to it you turn it on.

Every 5 minutes is when minutes%5 == 0.

You can collect the various timestamps I suggest into a struct each and then provide a function that will wrap everything appropriately.

void wrapTimestamp(TimeStamp *timestamp){
    if(timestamp->seconds >= 60){
        timestamp->seconds = timestamp->seconds%60;
        timestamp->minutes += timestamp->seconds/60;
    }
    if(timestamp->minutes >= 60){
        timestamp->minutes = timestamp->minutes%60;
        timestamp->hours +=timestamp->minutes/60;
    }
    if(timestamp->hours >= 24){
        timestamp->hours = timestamp->hours%24;
        timestamp->days +=timestamp->hours/24;
    }
    if(timestamp->days >=30){
        timestamp->days = timestamp->days%30;
    }
}

You should consider adding a real-time-clock module that will have much less drift than delay.

  • @SDsolar they are nearly all wrong. I wanted strong language to emphasize that they should be taken with a large grain of salt. – ratchet freak Oct 27 '17 at 9:52
  • I agree with nearly everything you said. If it were me I would indeed do what you said: add a DS3231 clock module and start over. But the OP says this is inherited code and that a rewrite is beyond their capability, alas. – SDsolar Oct 27 '17 at 17:29
2

There is nothing that happens every 150 minutes!

The (hours % 2) == 0 would be true every 2 hours.

NOTE this is terrible code! I hope nothing is to dependent on things happening at a definite time, because the internal clock will drift and no allowance is made for the time other code takes to execute.

  • 1
    There was something that happened every 2 hours at the 30 minutes mark (which is not 150 minutes, but easily confused for it), however that was commented out. – Chris Stratton Oct 19 '17 at 2:24
2

The sketch has so many issues that it's better to start afresh. Dump it. It's worthless. Anyone can rewrite it in 30 minutes while under the influence.

See: you are controlling a grow room with what seems to be 1 KW or more in HID. You can't simply switch ON an HID; you have to be sure that the HID is cold (a waiting period of 5-15 minutes).

The sketch doesn't get time from anywhere. Every time you reset the board, time goes back to zero. Kiss your light schedule goodbye.

You need to control room temperature. If the grow room goes over 30°C, you are in trouble. The minimum you can do is switch OFF the HID to cool things. And if the temp is lower than 20°, better switch OFF the extractor for a while to heat up the room. Girls don't like cold.

The sketch has no user interface, no way to set the light schedule, temp limits (among other things).

Replace it with a wall timer:

Timer

  • Hi Look Alterno! Thank you for responding to my question! I acknowledge the large flaws with the code but it has functioned reasonably well for us without problem. We are using LEDs so the lighting/temp issues you raised is not an issue for us! And we are not growing cannabis hahah! – J Dough Oct 19 '17 at 14:24
  • A timer like the one you have posted would simplify things but we are planning on expanding capabilities when we find the right individual so I think we will stick with the arduino. Thanks again!! – J Dough Oct 19 '17 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.