resetting RTC on Data logger shield

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
poledust
 
Posts: 26
Joined: Thu Jan 03, 2013 8:25 pm

Re: resetting RTC on Data logger shield

Post by poledust »

Last edited by poledust on Sat Jan 05, 2013 6:53 pm, edited 1 time in total.

poledust
 
Posts: 26
Joined: Thu Jan 03, 2013 8:25 pm

Re: resetting RTC on Data logger shield

Post by poledust »

I posted a video on youtube in case it helps explain better!
and the code I posted I removed everything to see if something in it was creating the issue and added the seconds back in but that just showed me the seconds were doing it as well!



Real time clock!

https://www.youtube.com/watch?v=s__Ise-x7to

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: resetting RTC on Data logger shield

Post by adafruit_support_bill »

This is not an RTC problem. It is a bug in your display code. You are not clearing the "9".

When you transition from a 2-digit field like "59" to a one digit field like "0" you need to either clear the second digit, or add a leading zero.

poledust
 
Posts: 26
Joined: Thu Jan 03, 2013 8:25 pm

Re: resetting RTC on Data logger shield

Post by poledust »

Thanks for the help I think I know what you mean. would you be able to give me some pointers on how to do that or point me in the right direction, by the way I also got the display from you guys and I am using the code and or libraries the LCD is Standard LCD 20x4.
thank you !

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: resetting RTC on Data logger shield

Post by adafruit_support_bill »

Starting with the code you have, the easiest thing is to blank the fields before you print them. You can do this by simply adding two spaces after the ":"s in each print statement.

Code: Select all

    lcd.setCursor(14,0);
    lcd.print(":  ");
    lcd.setCursor(15,0);
    lcd.print(now.minute(), DEC);
    lcd.setCursor(17,0);
    lcd.print(":  ");
    lcd.setCursor(18,0);
    lcd.print(now.second(), DEC);
You will have the same problem with days and dates. You will want to add two spaces after the "/"s as well.

poledust
 
Posts: 26
Joined: Thu Jan 03, 2013 8:25 pm

Re: resetting RTC on Data logger shield

Post by poledust »

Thank you! That seems to do it!
I am having another issue when the clock is show a one digit number say 9:06 it will show like this 9 :6 when it is a two digit time like 10:10 it show up as 10:10 so that part looks fine the numbers move to the first print location and it looks like it is being caused by this am/pm code it left zeros in those print locations before here is the code the one I found is a little different then the one you posted I will try that one and see if it works bet DateTime now = RTC.now();

int hr_24, hr_12;// liudr
hr_24=now.hour();

if (hr_24==0) hr_12=12;
else hr_12=hr_24%12;

lcd.setCursor(12,0);
lcd.print(hr_12, DEC);
//... everything in between now last line:
lcd.setCursor(18,0);
if (hr_24<12) lcd.print("AM");
else lcd.print("PM");




lcd.setCursor(1,0);
lcd.print(now.month(), DEC);
lcd.setCursor(2,0);
lcd.print("/");
lcd.setCursor(4,0);
lcd.print(now.day(), DEC);
lcd.setCursor(5,0);
lcd.print("/");
lcd.setCursor(6,0);
lcd.print(now.year(), DEC);


lcd.setCursor(14,0);
lcd.print(": ");
lcd.setCursor(15,0);
lcd.print(now.minute(), DEC);

delay(3000);

}

ter.

poledust
 
Posts: 26
Joined: Thu Jan 03, 2013 8:25 pm

Re: resetting RTC on Data logger shield

Post by poledust »

I got the time working and I am posting it in case anyone runs into this problem there is only one problem left for me to figure out when the time reaches 12:00 noon it shows 0:00 instead so if anyone can help with that part it would be great thanks!


Code: Select all

//this code controls relays to control temperature
//and displays it on a lcd

#include <dht11.h> //this is for the temp and humidity sensor
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
#include <DateTime.h>


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int hiTemp  = 30;//this  will activate relay 1 witch will turn on fan 1
int hiHumidity = 32;//this  will activate relay 2 witch will turn on fan 2
int lowTemp = 34;//this  will activate relay 3 witch will turn on heat lamp
int lowHumidity = 36;//this  will activate relay 4 witch will turn on mister
int testRelay = 38;
int ledPin = 13;

//PHOTOCELL VALUES
int sensorMin = 1023; // minimum photo sensor value 
int sensorMax = 0; // maximum photo sensor value
const int sensorPin = A0;// select the input pin for the photocell 
int sensorValue = 0;// variable to store the value coming from the photocell sensor

int ack;
int chk;
int count = 0;
char cmd = 0;
byte temppin = 0;

byte startHour = 5;    // Set our start and end times
byte startMinute =00;  // Don't add leading zero to hour or minute - 7, not 07
byte endHour = 22;      // Use 24h format for the hour, without leading zero
byte endMinute = 00;

byte validStart = 1;    // Declare and set to 0 our start flag
byte poweredOn = 1;     // Declare and set to 0 our current power flag
byte validEnd = 0;      // Declare and set to 0 our end flag

dht11 DHT11;  //this is for the temp and humidity sensor

RTC_DS1307 RTC;


void setup(){
  
   Serial.begin(9600);
   Wire.begin();
   RTC.begin();
   lcd.begin(20, 4);
   DHT11.attach(2);
   
   if (! RTC.isrunning())
{
   lcd.setCursor(0,0);
   lcd.println("RTC is NOT running!");
   
   // following line sets the RTC to the date & time this sketch was compiled
   // RTC.adjust(DateTime(__DATE__, __TIME__));   
   delay(4000);
   lcd.clear();
     
}
   //PIN MODES   
   pinMode(hiTemp, OUTPUT);  //this  will activate relay 1 witch will turn on fan 1
   pinMode(hiHumidity, OUTPUT);//this  will activate relay 2 witch will turn on fan 2
   pinMode(lowTemp, OUTPUT);//this  will activate relay 3 witch will turn on heat lamp
   pinMode(lowHumidity, OUTPUT); //this  will activate relay 4 witch will turn on mister
   pinMode(testRelay, OUTPUT);
   pinMode(ledPin, OUTPUT);// Set our LED as an output pin
   digitalWrite(ledPin, LOW); // Set the LED to LOW (off)
   digitalWrite(hiTemp, HIGH);
   digitalWrite(hiHumidity, HIGH); 
   digitalWrite(lowTemp, HIGH);
   digitalWrite(lowHumidity, HIGH);
   digitalWrite(testRelay, HIGH);
    
    
    
void welcome();
{
   //WELCOME SCREEN
   lcd.setCursor(0,0);
   lcd.print("STARTING UP ZONE 5");
   lcd.setCursor(0,1);
   lcd.print(" WELCOME! ");
   lcd.setCursor(0,3);
   lcd.print("INITIALIZING");
   delay(1000);
   lcd.clear();
   
   //  int chk = DHT11.read();  //this is for the temp and humidity sensor
   // lcd.print("Read sensor: ");  //this is for the temp and humidity sensor
   // switch (chk)
   // {
   //  case 0: Serial.println("OK"); break;  //this is for the temp and humidity sensor
   //   case -1: Serial.println("Checksum error"); break;  //this is for the temp and humidity sensor
   //   case -2: Serial.println("Time out error"); break;  //this is for the temp and humidity sensor
   //   default: Serial.println("Unknown error"); break;  //this is for the temp and humidity sensor
  
} 
}
void loop(){  

//START HIGH TEMPERATURE LOGIC
{
   chk = (DHT11.temperature);
   if (chk > 30)//86 degrees F
{ 
   
   digitalWrite(testRelay, HIGH); 
   delay(500);
   lcd.setCursor(0,1); 
   lcd.print ("HIGH TEMP  ");
   digitalWrite(hiTemp, LOW);
   
   
   
}
   if (chk < 26)//78.8 degrees F
{
   digitalWrite(hiTemp, HIGH);

}  //END HIGH TEMPERATURE LOGIC



}  //START HIGH HUMIDITY LOGIC   
   /*
   You want to keep the humidity in your vegetive/grow room around 40%-60%.
   Keeping the humidity below 40% will cause your plants to dry out & the water could evaporate too fast.
   Having a humidity above 60% will cause your plants to be more susceptible to rot root & other root bound diseases.
   Having the humidity too high might also cause your plants to get moldy.
   */

   chk = (DHT11.humidity);
   if (chk > 62)
{
   lcd.setCursor(0,2); 
   lcd.print ("HIGH HUMIDITY  ");
   delay(500);
   digitalWrite(hiHumidity, LOW);

}
   if (chk < 55)
{
   digitalWrite(hiHumidity, HIGH);
 
 
}  //END HIGH HUMIDITY LOGIC



{  // START LOW TEMPERATURE LOGIC

   chk = (DHT11.temperature);
   // Serial.println ((byte)chk);
   if (chk < 22)//71.6 degrees F
{
 
   lcd.setCursor(0,1); 
   lcd.print ("LOW TEMP   ");
   delay(500);
   digitalWrite(lowTemp, LOW);
 
}
   chk = (DHT11.temperature);
   if (chk > 24)//75.2 degrees F
{
   digitalWrite(lowTemp, HIGH);

   delay(500);
}  //END LOW TEMPERATURE LOGIC




}  //START LOW HUMIDITY LOGIC
   /*
   You want to keep the humidity in your vegetive/grow room around 40%-60%.
   Keeping the humidity below 40% will cause your plants to dry out & the 
   water could evaporate too fast. Having a humidity above 60% will cause
   your plants to be more susceptible to rot root & other root bound diseases.
   Having the humidity too high might also cause your plants to get moldy.

   //I AM NOT SURE IF THIS IS THE BEST WAY TO HANDLE THIS PART OF THE CODE  BELOW WHAT MY IN TENT 
   //IS IS TO TURN ON A MISTER TO BRING THE HUMIDITY UP BUT IT MY TAKE SOME TIME FOR 
   //THE SENSOR TO RESPOND SO IT MAY NEED SOME TYPE OF DELY OR SOMETHING THAT SAYS 
   //TURN ON FOR ON SECOND WAIT FOR READING THEN TURN ON FOR ONE MORE SECOND 
   //SOMETHING LIKE THAT
   */
{
   chk = (DHT11.humidity);
   if (chk < 41)
{ 
   lcd.setCursor(0,2); 
   lcd.print ("LOW HUMIDITY  "); 
   delay(500);   
   digitalWrite(lowHumidity, LOW);
   // delay(500);
}
   if (chk > 50)
{
   digitalWrite(lowHumidity, HIGH);
   
}  //END LOW HUMIDITY LOGIC
 
  
}  //LIGHTS ON/OFF LOGIC
  
   sensorValue = analogRead(sensorPin);    // read the analog input pin
  
{
   if (sensorValue < 700)
{
    // Serial.println("LIGHTS OFF  ");
   //  Serial.println(sensorValue);    // print to screen
   lcd.setCursor(0,3);
   lcd.print ("Lights     OFF ");
   lcd.print (sensorValue); 
}
   else
{
   Serial.println ("Lights  ON");
   // Serial.println(sensorValue);    // print to screen
   lcd.setCursor(0,3); 
   lcd.print ("Lights     ON  ");
   lcd.print (sensorValue);
    
}
}  //END SETUP 
  

{  //START DISPLAY OF TEMPERATURE READINGS   

   int chk = DHT11.read();  //this is for the temp and humidity sensor

   lcd.setCursor(0,2);
   lcd.print("Humidity    %  ");
   lcd.print((float)DHT11.humidity);
   delay(50);
   lcd.setCursor(0,1);
   lcd.print("Temperature F  ");
   lcd.print(DHT11.fahrenheit());

   delay(50);  
}
   DateTime now = RTC.now(); 

   if (now.month()<10)
{
   lcd.setCursor(0,0);
   lcd.print(" ");
   lcd.setCursor(1,0);      
   lcd.print(now.month(), DEC);
   lcd.setCursor(2,0);
   lcd.print("/  ");
}
   else
{
   lcd.setCursor(0,0);
   lcd.print(now.month(), DEC);
   lcd.setCursor(2,0);
   lcd.print("/  ");
}
   if (now.day()<10)
{
   lcd.setCursor(3,0);
   lcd.print(" ");
   lcd.setCursor(4,0);
   lcd.print(now.day(), DEC);
   lcd.setCursor(5,0);
   lcd.print("/    ");
   lcd.setCursor(6,0);
   lcd.print(now.year(), DEC);
}
   else
{
 
   lcd.setCursor(3,0);
   lcd.print(now.day(), DEC);
   lcd.setCursor(5,0);
   lcd.print("/    ");
   lcd.setCursor(6,0);
   lcd.print(now.year(), DEC);     
}  
       
 
   int hr_24, hr_12;// 	liudr
   hr_24=now.hour();

   if (hr_24==0) hr_12=12;
   else hr_12=hr_24%12;

   if (hr_12<10)
{
   lcd.setCursor(12,0);
   lcd.print("  ");
   lcd.setCursor(13,0);
   lcd.print(hr_12, DEC);
   lcd.setCursor(18,0);
   if (hr_24<12) lcd.print("AM");
   else lcd.print("PM");
}
   else
{
 
   lcd.setCursor(12,0);
   lcd.print("  ");
   lcd.setCursor(12,0); 
   lcd.print(hr_12, DEC);

   //... everything in between now last line:
   lcd.setCursor(18,0);
   if (hr_24<12) lcd.print("AM");
   else lcd.print("PM");      
       

} 

   lcd.setCursor(14,0);
   lcd.print(":  ");

   if (now.minute()<10)
{

   lcd.setCursor(15,0); 
   lcd.print("0");

   lcd.setCursor(16,0);
   lcd.print(now.minute(), DEC);
}
   else
{
   lcd.setCursor(15,0);
   lcd.print(now.minute(), DEC);
   delay(500);
}


   if (now.second() == 0) // Only process if we have ticked over to new minute
{ 
  
   if (poweredOn == 0) // Check if lights currently powered on
{   
    
   checkStartTime(); // If they are not, check if it's time to turn them on
} 
   else
{
   checkEndTime();      // Otherwise, check if it's time to turn them off
}
  
   if (validStart == 1) // If our timer is flagged to start, turn the lights on
{    
   turnLightsOn();
}
   if (validEnd == 1) // If our time is flagged to end, turn the lights off
{    
   turnLightsOff();
}
}

   delay(1000);  

}
   byte checkStartTime() 
{
   DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
  
   if (now.hour() == startHour && now.minute() >= startMinute) 
{
   validStart = 1;  // If our start hour and minute match the current time, set 'on' flags
   poweredOn = 1;
}
   else
{
   validStart = 0;  // Otherwise we don't need to power up the lights yet
}
  
   return validStart; // Return the status for powering up
}

   byte checkEndTime() 
{
   DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
  
   if (now.hour() == endHour && now.minute() >= endMinute) 
{
   validEnd = 1;    // If our end hour and minute match the current time, set the 'off' flags
   poweredOn = 0;
}
   else 
{
   validEnd = 0;    // Otherwise we don't need to power off the lights yet
}
  
   return validEnd;   // Return the status for powering off
} 

   void turnLightsOn() 
{
   digitalWrite(ledPin, HIGH);  // Turn on the LED
   digitalWrite(testRelay, LOW);
  
}

   void turnLightsOff() 
{
   digitalWrite(ledPin, LOW);   // Turn off the LED
   digitalWrite(testRelay, HIGH);
}
   


    
 
   

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: resetting RTC on Data logger shield

Post by adafruit_support_bill »

You want a similar adjustment as for the 24:00 case:

Code: Select all

   if (hr_24==0) hr_12=12;
   else hr_12=hr_24%12;
   if (hr_12==0) hr_12 = 12;

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”