Go Down

Topic: turn lights on and off using real time clock ds1307 (Read 2296 times) previous topic - next topic

poledust

Jan 13, 2013, 10:05 pm Last Edit: Jan 14, 2013, 01:52 am by poledust Reason: 1
Code: [Select]
I am wondering if converting the time from 24hr to 12hr is going to interfere with the part of code that will turn on the ledpin and off at set times
Code: [Select]

#include <dht11.h> //this is for the temp and humidity sensor
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
#include <DateTime.h>
//#include <RTClib.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 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 tempPin = 2;//Temp and humidty pin
int ack;
int chk;
int count = 0;
char cmd = 0;
byte temppin = 0;

[[color=red]color=red][font=Verdana]byte startHour = 13;    // Set our start and end times
byte startMinute = 33;  // Don't add leading zero to hour or minute - 7, not 07
byte endHour = 13;      // Use 24h format for the hour, without leading zero
byte endMinute = 34;

byte validStart = 0;    // Declare and set to 0 our start flag
byte poweredOn = 0;     // Declare and set to 0 our current power flag
byte validEnd = 0;      // Declare and set to 0 our end flag
[/color]
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
[color=red]  pinMode(ledPin, OUTPUT);// Set our LED as an output pin
   digitalWrite(ledPin, LOW); // Set the LED to LOW (off)[/color]
  //WELCOME SCREEN
  lcd.setCursor(0,0);
  lcd.print("Starting up");
  lcd.setCursor(0,1);
  lcd.print("*** WELCOME! ***");
  lcd.setCursor(0,3);
  lcd.print("Initializing");
    delay(4000);
  lcd.clear();
 

 
}
void loop()
{

 
//LIGHTS ON/OFF LOGIC
sensorValue = analogRead(sensorPin);    // read the analog input pin
 

 if (sensorValue < 300)
 {
  // Serial.println("LIGHTS OFF  ");
 //  Serial.println(sensorValue);    // print to screen
   lcd.setCursor(0,3);
   lcd.print ("Lights     OFF ");
   lcd.print (sensorValue);    
     delay(50);
}
   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  

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


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

      delay(500);
 
}


   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);    
     

}  
     

[color=red]    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)[/color]
{
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(11,0);
//   lcd.print("   ");
// lcd.setCursor(12,0);
//lcd.print(now.hour(), DEC);
   lcd.setCursor(14,0);
   lcd.print(":  ");

   if (now.minute()<10)
{

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

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

// lcd.setCursor(15,0);
   //lcd.print(now.minute(), DEC);
  delay(3000);
}




 



[color=red]  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
}

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

}[/color]
 



   

 
When you cant get to the Club to see them Dance! Mingle with Exotic Dancers at Poledust.com!

UKHeliBob

Please edit your post and put code tags around your code.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Riva

As your startHour is 13 then switching the RTC to 12 hour mode will effect the on/off time in that the hour will never get to 13 and the lights wont turn on. If you have a lower value for startHour (say 7) then the lights will come on at 7 in the morning and 7 in the evening, maybe not what you wanted. Best to keep the RTC in 24 hour mode and if you want a 12 hour display with AM/PM indicator then convert the hours value on the fly to 12 hour format.

Go Up