help regarding calling a loop to run based on a time.alarm

Hello arduino.cc forum users I have almost finished building a growroom controller, the code is almost there but I have two issues regarding the arduino sketch, ideally what i would like is to have two loops (void LightOn() and void LightOff().
I have a time.alarm which runs for 16 hrs, I have coded a start time for the alarm and a stop time for the alarm, what I would like is to call and run the void LightOn() loop when the alarm is on and LightOff() loop when the alarm is off so it just runs that part of the sketch.
The reason for this is I can alter the growing conditions when light is on compared to when they are off via the sensors and relays I have attached to the arduino.
The sketch is a little rough, but i'm hoping not to many changes are needed, but as always if you can see any improvements that could be made please say :slight_smile:
The sensors attached to the arduino is a DHT-22 and two ds18b20 sensors, the next issue I have is regarding making a if statement based on the output of both ds18b20 i'm unsure of how to code this, I have tried a couple of ways but without success, and I would like to ask for help as i'm sure most of you users are more experienced than I am, this is my first project and any help would be greatly appreciated.

Here is the code :

// Growroom controller

// 18 hr growing cycle (18hrs on 6hrs off)
// lights on at 13.00 lights off at 5.00
// Conditions wanting to achieve:
// 18 hrs on:
// Temperature = 21 - 27 C (Ideal 26 C)      setPointonTemp = 26
// Humidity = 40 %                           setPointonHumid = 40
// Nutrient heat = 19 - 21 C (Ideal 20 C)    setPointonHeat = 20
// 6 hrs off:
// Temperature = 18 - 22 C (Ideal 20 C)      setPointoffTemp = 20
// Humidity = 40 %                           setPointoffHumid = 40
// Nutrient heat = 19 - 21 C (Ideal 20 C)    setPointoffHeat = 20
// Grow light = digital pin 6   
// Grow room heater = digital pin 7
// Grow room humidifier = digital pin 8
// Grow room dehumidifier = digital pin 9
// Grow room nutrient tank heater = digital pin 10
// Grow room intake and exhaust fans = digital pin 11
// Sensors used and pin :
// DHT-22 to read temp and humidity of grow room = digital pin 5
// 1xDS18b20 to read the the temp of nutrients = digital pin 3 (one wire bus) addresss { 0x28, 0xA0, 0xCF, 0xF8, 0x04, 0x00, 0x00, 0xFC }
// 1xds18b20 to read the outside temp of growroom = digital pin 3 (one wire bus) { 0x28, 0xE8, 0x74, 0xB3, 0x05, 0x00, 0x00, 0xC8 }


#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>
 
RTC_DS1307 RTC;
 
// Start Time
int sHour = 13; // start time of growing cycle
int sMinute = 00;
int sSecond = 0;
 
// End Time
int eHour = 05; // end time of growing cycle
int eMinute = 00;
int eSecond = 0;
 
const int lightRelay = 6;
const int heaterRelay = 7;
const int humidifierRelay = 8;
const int dehumidifierRelay = 9;
const int nutrientheaterRelay = 10;
const int fanRelay = 11;

#define DHTPIN 5     // PIN data of DHT22

#define DHTTYPE DHT22   // DHT 22  (AM2302)

#define ONE_WIRE_BUS 3
DHT dht(DHTPIN, DHTTYPE);

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);


// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
 
// The address of the DS18B20
DeviceAddress TANK_TEMP = { 0x28, 0xA0, 0xCF, 0xF8, 0x04, 0x00, 0x00, 0xFC };
DeviceAddress OUTSIDE_TEMP = { 0x28, 0xE8, 0x74, 0xB3, 0x05, 0x00, 0x00, 0xC8 };

// Set some setpoints via perfect Conditions to allow slight deviation when turning on relays
float setPointonTemp = 26;
float setPointonHumid = 40;
float setPointonHeat = 20;
float setPointoffTemp = 20;
float setPointoffHumid = 40;
float setPointoffHeat = 20;



void setup(void)
{
  // Set the relay to off immediately
  digitalWrite(lightRelay, HIGH);
  
  // Start serial
  Serial.begin(9600);
  
  // Instantiate the RTC
  Wire.begin();
  RTC.begin();
  
  // Set the pinmode
  pinMode(lightRelay, OUTPUT);
  pinMode(heaterRelay, OUTPUT);
  pinMode(heaterRelay,OUTPUT);
  pinMode(humidifierRelay,OUTPUT);
  pinMode(dehumidifierRelay,OUTPUT);
  pinMode(nutrientheaterRelay,OUTPUT);
  pinMode(fanRelay,OUTPUT);
  
  // Check if the RTC is running.
  if (! RTC.isrunning()) {
  Serial.println("RTC is NOT running");
  
  }
  // Get time from RTC
  DateTime current = RTC.now();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (current.unixtime() < compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));    
  
  }
  // Use RTC time to set the start time
  setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
  time_t s = now();
 
  // Use RTC time to set the end time 
  setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());   
  time_t e = now();
   
  // Use RTC time to set the current time
  setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());  
  time_t n = now();
 
  // Test if grow light should be on
 if (s <= n && n <= e) {
    digitalWrite(lightRelay, LOW);  // Sets the grow light "on"
  }
     
  Alarm.alarmRepeat(sHour, sMinute, sSecond, LightOn);
  Alarm.alarmRepeat(eHour, eMinute, eSecond, LightOff);
  
  // Start up the library
  sensors.begin();
   
  // Set the resolution to 10 bit (good enough?)
  sensors.setResolution(TANK_TEMP, 10);
  sensors.setResolution(OUTSIDE_TEMP, 10);
  
  dht.begin(); 
 
  Serial.println("Setup Complete.");  
}
 
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}
 
void loop(void)
{ 
 // Get the current time
  DateTime now = RTC.now();  
  setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());  
   
  // Display the current time
  Serial.print("Current time: ");
  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();
  
  float h = dht.readHumidity(); // Value of Temp
  float t = dht.readTemperature(); // Value of Humidity
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  delay(2000);  
  Serial.print("Getting temperature...\n\r");
  sensors.requestTemperatures();
   
  Serial.print("TEMP OF TANK is: ");
  printTemperature(TANK_TEMP);
  Serial.print("\n\r");
  delay(2000);
  Serial.print("TEMP OUTSIDE is: ");
  printTemperature(OUTSIDE_TEMP);
  Serial.print("\n\r");
  Alarm.delay(2000);
  
}
void LightOn() {
  Serial.println("Turning Light On");
  digitalWrite (lightRelay, HIGH);
  float h = dht.readHumidity(); // Value of Temp
  float t = dht.readTemperature(); // Value of Humidity
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  if (h < setPointonHumid)
  {
  digitalWrite(humidifierRelay,HIGH);
  digitalWrite(fanRelay,LOW);
  } else if (h == setPointonHumid +3) {
  digitalWrite(humidifierRelay,LOW);
  digitalWrite(fanRelay,HIGH);
  if (t < setPointonTemp)
  {
  digitalWrite(heaterRelay,HIGH);
  digitalWrite(fanRelay,LOW);
  } else if (t == setPointonTemp +3) {
  digitalWrite(heaterRelay,LOW);
  digitalWrite(fanRelay,HIGH);
  } 
  Serial.print("Getting temperature...\n\r");
  sensors.requestTemperatures();
  Serial.print("TEMP OF TANK is: ");
  printTemperature(TANK_TEMP);
  Serial.print("\n");
  delay(2000);
  Serial.print("TEMP OUTSIDE is: ");
  printTemperature(OUTSIDE_TEMP);
  Serial.print("\n");
  digitalWrite(lightRelay, LOW);
  Alarm.delay(2000);
  }
  
}

void LightOff() {
  Serial.println("Turning Light Off");
  digitalWrite (lightRelay, LOW);
  float h = dht.readHumidity(); // Value of Temp
  float t = dht.readTemperature(); // Value of Humidity
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  if (h < setPointoffHumid)
  {
  digitalWrite(humidifierRelay,HIGH);
  digitalWrite(fanRelay,LOW);
  } else if (h == setPointoffHumid +3) {
  digitalWrite(humidifierRelay,LOW);
  digitalWrite(fanRelay,HIGH);
  if (t < setPointoffTemp)
  {
  digitalWrite(heaterRelay,HIGH);
  digitalWrite(fanRelay,LOW);
  } else if (t == setPointoffTemp +3) {
  digitalWrite(heaterRelay,LOW);
  digitalWrite(fanRelay,HIGH);
  } 
  Serial.print("Getting temperature...\n\r");
  sensors.requestTemperatures();
   
  Serial.print("TEMP OF TANK is: ");
  printTemperature(TANK_TEMP);
  Serial.print("\n");
  delay(2000);
  Serial.print("TEMP OUTSIDE is: ");
  printTemperature(OUTSIDE_TEMP);
  Serial.print("\n");
  digitalWrite(lightRelay, LOW);
  Alarm.delay(2000);
  } 
  Serial.print("Getting temperature...\n\r");
  sensors.requestTemperatures();
 
   
  Serial.print("TEMP OF TANK is: ");
  printTemperature(TANK_TEMP);
  Serial.print("\n\r");
  delay(2000);
  Serial.print("TEMP OUTSIDE is: ");
  printTemperature(OUTSIDE_TEMP);
  Serial.print("\n\r");
  Alarm.delay(2000);
  digitalWrite(lightRelay, HIGH);
  
}

ideally what i would like is to have two loops (void LightOn() and void LightOff().

Are you using "loop" to mean function?

I have a time.alarm which runs for 16 hrs

That doesn't make sense. Alarm.alarmRepeat() (what I think you are talking about) triggers a function at a specific time. That function shouldn't run for 16 hours.

If you have an RTC, I don't see the need for the Alarm class at all.

I don't see the relationship between the alarm triggering events at a specific time and the desire to make things happen when it gets light or dark.

the next issue I have is regarding making a if statement based on the output of both ds18b20 i'm unsure of how to code this

An if statement is used to determine if a block of code is to be executed. The conditional part of the expression is evaluated, and results in either true (so the block is executed) or false (so the block is not). Without knowing how you want to use the two temperatures, we can hardly be expected to tell you how to arrange the conditional part of the expression.

Anyone?

iandsm1:
Anyone?

What? You don't like my comments and questions?

Sorry I wasn't being rude I didn't see the comment when I posted maybe we were doing it at the same time!

I was thinking along these lines for the ds182b0:

if (TANK_TEMP < setPointoffHeat)
{
digitalWrite(nutrientheaterRelay,HIGH);

} else if (TANK_TEMP < setPointoffHeat +3) {
digitalWrite(nutrientheaterRelay,HIGH,);

The only thing im unsure what unit to put in where TANK_TEMP is in the if statement so the arduino turns the relay on and off based on the output

Also I thought you may be right concerning the time alarm, would it be possible to wright a if command based on time that will call this at 13.00 hrs

void LightOn() {
Serial.println(“Turning Light On”);
digitalWrite (lightRelay, HIGH);
float h = dht.readHumidity(); // Value of Temp
float t = dht.readTemperature(); // Value of Humidity
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.println(” *C”);
if (h < setPointonHumid)
{
digitalWrite(humidifierRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (h == setPointonHumid +3) {
digitalWrite(humidifierRelay,LOW);
digitalWrite(fanRelay,HIGH);
if (t < setPointonTemp)
{
digitalWrite(heaterRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (t == setPointonTemp +3) {
digitalWrite(heaterRelay,LOW);
digitalWrite(fanRelay,HIGH);
}
Serial.print(“Getting temperature…\n\r”);
sensors.requestTemperatures();
Serial.print(“TEMP OF TANK is: “);
printTemperature(TANK_TEMP);
Serial.print(”\n”);
delay(2000);
Serial.print(“TEMP OUTSIDE is: “);
printTemperature(OUTSIDE_TEMP);
Serial.print(”\n”);
digitalWrite(lightRelay, LOW);
Alarm.delay(2000);
}

and run it until the time is 05.00 hrs then run this

void LightOff() {
Serial.println(“Turning Light Off”);
digitalWrite (lightRelay, LOW);
float h = dht.readHumidity(); // Value of Temp
float t = dht.readTemperature(); // Value of Humidity
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.println(” *C”);
if (h < setPointoffHumid)
{
digitalWrite(humidifierRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (h == setPointoffHumid +3) {
digitalWrite(humidifierRelay,LOW);
digitalWrite(fanRelay,HIGH);
if (t < setPointoffTemp)
{
digitalWrite(heaterRelay,HIGH);
digitalWrite(fanRelay,LOW);
} else if (t == setPointoffTemp +3) {
digitalWrite(heaterRelay,LOW);
digitalWrite(fanRelay,HIGH);
}
Serial.print(“Getting temperature…\n\r”);
sensors.requestTemperatures();

Serial.print(“TEMP OF TANK is: “);
printTemperature(TANK_TEMP);
Serial.print(”\n”);
delay(2000);
Serial.print(“TEMP OUTSIDE is: “);
printTemperature(OUTSIDE_TEMP);
Serial.print(”\n”);
digitalWrite(lightRelay, LOW);
Alarm.delay(2000);
}
Serial.print(“Getting temperature…\n\r”);
sensors.requestTemperatures();

Serial.print(“TEMP OF TANK is: “);
printTemperature(TANK_TEMP);
Serial.print(”\n\r”);
delay(2000);
Serial.print(“TEMP OUTSIDE is: “);
printTemperature(OUTSIDE_TEMP);
Serial.print(”\n\r”);
Alarm.delay(2000);
digitalWrite(lightRelay, HIGH);

}

until 13.00 hrs ?

im not sure on how to write the code for this

I was thinking along these lines for the ds182b0:

if (TANK_TEMP < setPointoffHeat)
{
digitalWrite(nutrientheaterRelay,HIGH);

} else if (TANK_TEMP < setPointoffHeat +3) {
digitalWrite(nutrientheaterRelay,HIGH,);

If the tank temperature is less than the set point, isn’t it also going to be less than the set point + 3?
If the heater is to be turned on when the temperature is less than the set point + 3, why is there a test for/(and same)action for when the temperature is less than the set point?

im unsure what unit to put in where TANK_TEMP is in the if statement so the arduino turns the relay on and off based on the output

First, that isn’t what the code does. Second, all capital letter names are reserved (by convention, not by law) for constants. Since TANK_TEMP is, clearly, not a constant, the name shouldn’t be all capital letters. The value in the (renamed) variable should be what the sensor returns, in degrees F or C (whichever unit your set point is in).