//useing pin a4 and a5 as the 2 pins for clock and lcd a4 is SDA and A5 is SCL
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
int watered = 0;
int onMin = 20;
int waterTime = (onMin + 25)%60;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
lcd.init();
lcd.backlight();
lcd.print("DS1307RTC Read Test");
lcd.setCursor(0, 1);// first number across next number up and down 0-3
lcd.print("--------------------");
delay(2000);
lcd.clear();
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
if (hour() == 6 && minute() == onMin && (weekday() == 1 || weekday() == 4 || weekday() == 6)) {
waterOn();
}
lcd.print("Time = ");
print2digitsHour(tm.Hour);
lcd.print(':');
print2digits(tm.Minute);
lcd.print(':');
print2digits(tm.Second);
lcd.setCursor(0, 1);// move down to next line
lcd.print(tm.Month);
lcd.print('/');
lcd.print(tm.Day);
lcd.print('/');
lcd.print(tmYearToCalendar(tm.Year));
lcd.print(' ');
lcd.print(tm.Wday);
lcd.setCursor ( 0, 2 );
lcd.print("# times watered ");
lcd.print(watered);
delay(1000);
lcd.clear();
} else {
if (RTC.chipPresent()) {
lcd.println("The DS1307 is stopped. Please run the SetTime");
lcd.println("example to initialize the time and begin running.");
lcd.clear();
} else {
lcd.println("DS1307 read error! Please check the circuitry.");
lcd.println();
}
delay(9000);
}
//delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.write('0');
}
lcd.print(number);
}
void print2digitsHour(int number) {
if (number > 12 ) {
lcd.print(number-12);
}
else
{
lcd.print(number);
}
}
void waterOn() {
watered = watered+1;
while (minute() != waterTime)
{
digitalWrite(8, LOW);
lcd.print("Time = ");
print2digitsHour(hour());
print2digits(minute());
print2digits(second());
lcd.setCursor ( 0, 1 );
lcd.print("watering till");
lcd.print(waterTime);
delay(1000);
lcd.clear();
delay(500);
}
waterOff();
}
void waterOff() {
digitalWrite(8, HIGH);
lcd.clear();
lcd.print("water off");
delay(1000);
lcd.clear();
}
code seams ok except for the Wday part i would like this to turn on a relay on sunday wed and fri but when even i print out tm.Wday or if i try and use the weekday() i get 0 or a 5?? any idea why they are stuck and not changing with the weekdays?
Did you set the sync provider to the RTC? You should either do that, and get all your time directly from the time library using time library functions, or get your time from the RTC library. You have an unhealthy mixture of both.
yeah rest of the date and time are all there hour min sec and day month year all work fine the water time INT is for the display while watering it should roll over the the next hour's min that will end but yeah i know the remaninder
and AARG im not quite sure what you mean i have tried useing the tm.Wday and also weekday() fun both put out something wrong the weekday() puts out 5???
jasonbrown23:
yeah rest of the date and time are all there hour min sec and day month year all work fine the water time INT is for the display while watering it should roll over the the next hour's min that will end but yeah i know the remaninder
and AARG im not quite sure what you mean i have tried useing the tm.Wday and also weekday() fun both put out something wrong the weekday() puts out 5???
In the arduino ide theres an example under the rtc that has a set time the uses my computers time but isint wday and weekday a function that uses the current time.
ok so double doubled check and weekday() out put changed from 5 to 6 in 24 hours. powercycled the arduino and it went back to 5 no the tm.wday is showing 1 for today i will check tomorrow and see if its shows 2 or still 1 and if 2 will it keep it after power cycle
//useing pin a4 and a5 as the 2 pins for clock and lcd a4 is SDA and A5 is SCL
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
int watered = 0;
int onMin = 48;
int waterTime = (onMin + 1)%60;
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait until Arduino Serial Monitor opens
pinMode(8, OUTPUT);// sets relay pin to output
digitalWrite(8, HIGH);// sets relay pin to high aka off
setSyncProvider(RTC.get); // the function to get the time from the RTC
lcd.init();//turn on lcd
lcd.backlight();//turn on lcdbacklight
if(timeStatus()!= timeSet)
{
lcd.print("Unable to sync with the RTC");
}
else
{
lcd.print("RTC Read Test Good");
delay(2000);
lcd.clear();
}
}
void loop()
{
if (timeStatus() == timeSet)
{
WaterYardiDsplay();
CheckToWater();
}
else
{
lcd.print("The time has not been set. Please run the Time");
lcd.setCursor(0, 1);// first number across next number up and down 0-3
lcd.print("TimeRTCSet example, or DS1307RTC SetTime example.");
delay(4000);
}
delay(1000);
lcd.clear();
}
void WaterYardiDsplay(){
// digital clock display of the time
lcd.print("Time ");
print2digitsHour(hour());
printDigits(minute());
printDigits(second());
lcd.setCursor(0, 1);
lcd.print(month());
lcd.print("/");
lcd.print(day());
lcd.print("/");
lcd.print(year());
lcd.print(" Day #");
lcd.print(weekday());
lcd.setCursor(0, 2);
lcd.print("Times Watered ");
lcd.print(watered);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
void CheckToWater(){
if (hour() == 13 && minute() == onMin && (weekday() == 1 || weekday() == 4 || weekday() == 6)) {
waterOn();
}
}
void waterOn() {
watered = watered+1;
while (minute() != waterTime)
{
digitalWrite(8, LOW);
lcd.print("Time = ");
print2digitsHour(hour());
printDigits(minute());
printDigits(second());
lcd.setCursor ( 0, 1 );
lcd.print("watering till");
lcd.print(waterTime);
delay(1000);
lcd.clear();
delay(500);
}
waterOff();
}
void waterOff() {
digitalWrite(8, HIGH);
lcd.clear();
lcd.print("water off");
delay(1000);
lcd.clear();
}
void print2digitsHour(int number) {
if (number > 12 ) {
lcd.print(number-12);
}
else
{
lcd.print(number);
}
}
i fixed it also that water till will round to the next hour if it moves over