sensor driven alarm RTC

Hello
I am pretty much a noob when it comes to ardruinos.
What i am trying to achieve is that a sensor sets a alarm and then at a certain time switches a solenoid.
I am using a Arduino mini pro with a 1307rtc and moisture sensor.

basically, sensor reads a certain moisture - if under - set alarm - alarm goes off and switches solenoid.

I have been playing around with the time and timealarm.h but with no success any help would be highly appreciated.

thanks

If the sensor detects low moisture and you want the solenoid to turn on, why do you need a RTC.

Weedpharma

It's a garden watering system so when the soil moisture drops the water turns on in the evening after the bright sun is gone so the plants don't burn.

Rather than setting an alarm, simply look at the sensor at the alarm time and turn the solenoid on if dry.

Once the soil is dry, it makes no difference if it happens early, if it still has to wait for the alarm time.

Start your project by learning how to read the RTC with hard coded times then work out how to change the time. You will need an LCD display.

There is a lot of info on driving RTC and LCD in the forum.

Weedpharma

It would be much, much easier to use a light sensor (e.g. LDR) to detect evening, than an RTC.

actually i just found the watering has to happen in the morning due to watering restrictions.

The code i have so far looks like that.
Any suggestions would be really appreciated.

I would like to get rid of the delays.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

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

RTC_DS1307 RTC;
const byte valve = 4;
const byte sensor = 5;

void setup () {
   Serial.begin(57600);
   Wire.begin();
   RTC.begin();

 if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");
   // following line sets the RTC to the date & time this sketch was compiled
   RTC.adjust(DateTime(__DATE__, __TIME__));
 
    
 }
Alarm.alarmRepeat(05,00,0,measure);
}

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();
Alarm.delay(1000); // wait one second between clock display

}
void measure (){
  digitalWrite(sensor,HIGH);
  delay(5000);
  int sensorReading= analogRead(A0);
  digitalWrite(sensor,LOW);
  Serial.println (sensorReading);
  
  if (sensorReading > 800){//if reading is above 800
             digitalWrite(valve,HIGH); //turns the valve on
             delay(25*60000); //valve on  for 25 min
             digitalWrite(valve,LOW); //turns valve off
}}

slightly revised code.
for whatever reason it wont trigger the alarm.
what am i doing wrong?

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS1307 RTC;
const byte valve = 4;
const byte sensor = 5;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  
     
  }
  

 Alarm.alarmRepeat(11,33,0, MorningAlarm);
}
 
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();

 Alarm.delay(1000); // wait one second between clock display

}
void MorningAlarm(){
   Serial.println("measuring");
   digitalWrite(sensor,HIGH);
   delay(5000);
   int sensorReading= analogRead(A0);
   digitalWrite(sensor,LOW);
   Serial.println (sensorReading);
   
   if (sensorReading > 800){//if reading is above 800
              digitalWrite(valve,HIGH); //turns the valve on
              delay(25*60000); //water 25min
              digitalWrite(valve,LOW); //turns the valve off
}
}

for whatever reason it wont trigger the alarm.
what am i doing wrong?

The time library and time alarms are working off of the internal millis() clock and the internal clock has not been set(synchronized) with the RTC time.

Once the two clocks are synchronized, use the time library and time alarms syntax. and not the now.xxx() syntax of RTCLib. In the background, without you doing anything, the arduino internal clock will be synchronized with the RTC every 5 minutes

See this example of synchronizing the time library with an RTC using RTCLib.h.

#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
#include "RTClib.h"
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  
RTC_DS1307 RTC;

uint32_t syncProvider()
{
  return RTC.now().unixtime();
}

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  setSyncProvider(syncProvider);   // the function to get the time from the RTC
  if(timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");      
}

void loop(void)
{
  timeDateDisplay();  
  delay(1000);
}

void timeDateDisplay(void)
{
 
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(month());
  Serial.print("/");
  Serial.print(day());
  Serial.print("/");
  Serial.print(year()); 
  Serial.println(); 
  Serial.print("Unix Time ");
  Serial.println(now());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}