Ds1307

hi,

i am using arduino uno with ds1307, why when i put the code serial.print((RTC.get(DS1307_HR,true))); in a function and i call the function in the loop, at the serial the time is 0. but when i put the code in loop it's ok?

Not good enough. Post your complete code or at least enough code to show how the code looks like in loop and in function.

thanasisloi7:
hi,

i am using arduino uno with ds1307, why when i put the code serial.print((RTC.get(DS1307_HR,true))); in a function and i call the function in the loop, at the serial the time is 0. but when i put the code in loop it’s ok?

my full code is

// This is for compatibility with both arduino 1.0 and previous versions
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif


#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x38,16,2);

int led = 10;
int sensorPin = A0;
int sensorValue = 0;
int LDR_Pin = A1;
int ledvar ;
const int buttonPin = 9;
int buttonState = 0;
const int buttonAlarm = 8;
int AlarmState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode (buttonAlarm, INPUT);
  Wire.begin(2);

  /*
  RTC.stop();
   RTC.set(DS1307_SEC,1);        //set the seconds
   RTC.set(DS1307_MIN,35);     //set the minutes
   RTC.set(DS1307_HR,21);       //set the hours
   RTC.set(DS1307_DOW,4);       //set the day of the week
   RTC.set(DS1307_DATE,31);       //set the date
   RTC.set(DS1307_MTH,1);        //set the month
   RTC.set(DS1307_YR,13);         //set the year
   RTC.start();
   */
  lcd.init();
  lcd.backlight();
  lcd.clear();

}

void lcdPrint()
{  

  lcd.setCursor(0,0);
  lcd.print((RTC.get(DS1307_HR,true)));
  lcd.setCursor(2,0);
  lcd.print(":");
  lcd.setCursor(3,0);
  lcd.print((RTC.get(DS1307_MIN,true)));
  lcd.setCursor(6,0);
  lcd.print((RTC.get(DS1307_DATE,true)));
  lcd.setCursor(8,0);
  lcd.print("/");
  lcd.setCursor(9,0);
  lcd.print((RTC.get(DS1307_MTH,true)));
  lcd.setCursor(10,0);
  lcd.print("/");
  lcd.setCursor(11,0);
  lcd.print((RTC.get(DS1307_YR,true)));
  Serial.println(ledvar);
  if (ledvar == 1) 
  { 
    lcd.setCursor(11,1);
    lcd.print("On");
  }
  else
  {
    lcd.setCursor(11,1);
    lcd.print("Off");
  }
}

void lcdPrint1 ()
{

  lcd.setCursor(0,0);
  lcd.print((RTC.get(DS1307_HR,true)));
  lcd.setCursor(2,0);
  lcd.print(":");
  lcd.setCursor(3,0);
  lcd.print((RTC.get(DS1307_MIN,true)));
  lcd.setCursor(6,0);
  lcd.print((RTC.get(DS1307_DATE,true)));
  lcd.setCursor(8,0);
  lcd.print("/");
  lcd.setCursor(9,0);
  lcd.print((RTC.get(DS1307_MTH,true)));
  lcd.setCursor(10,0);
  lcd.print("/");
  lcd.setCursor(11,0);
  lcd.print((RTC.get(DS1307_YR,true)));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  AlarmState = digitalRead(buttonAlarm);
  if (AlarmState == LOW) {

    if (buttonState == HIGH) {
      int day = (RTC.get(DS1307_DOW,false));
      sensorValue = analogRead(sensorPin); 
      int LDRReading = analogRead(LDR_Pin);

      /*
  Serial.println(sensorValue);
       Serial.println(LDRReading);
       Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
       Serial.print(":");
       Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
       Serial.print(":");
       Serial.print(RTC.get(DS1307_SEC,false));//read seconds
       Serial.print("      ");
       Serial.print(RTC.get(DS1307_DATE,false));//read date
       Serial.print("/");
       Serial.print(RTC.get(DS1307_MTH,false));//read month
       Serial.print("/");
       Serial.print(RTC.get(DS1307_YR,false)); //read year
       Serial.print("     ");
       */

      switch (day){ 
      case 1:
        Serial.println("Monday");
        lcd.setCursor(0,1);
        lcd.print("Monday");
        if (LDRReading < sensorValue)
        {
          if ((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9)
          {
            ledvar = 1;
            lcdPrint();
            digitalWrite(led, HIGH);  
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else
        {

          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }
        break;
      case 2:
        Serial.println("Tuesday");
        lcd.setCursor(0,1);
        lcd.print("Tuesday");
        if (LDRReading < sensorValue)
        {
          if (((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9 ) || ((RTC.get(DS1307_HR,false)) <= 19 && (RTC.get(DS1307_HR,false)) >= 17 ))
          {
            ledvar = 1;
            lcdPrint();  
            digitalWrite(led, HIGH);
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else
        {
          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }

        break;
      case 3:
        Serial.println("Wednesday");
        lcdPrint();
        lcd.setCursor(0,1);
        lcd.print("Wednesday");
        if (LDRReading < sensorValue)
        {
          if ((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9)
          {
            ledvar = 1;
            lcdPrint();
            digitalWrite(led, HIGH);
            // Serial.println(RTC.get(DS1307_DOW,false));    
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else
        {
          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }
        break;
      case 4:
        Serial.println("Thursday");
        lcdPrint();
        lcd.setCursor(0,1);
        lcd.print("Thursday");
        if (LDRReading < sensorValue)
        {
          if (((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9 ) || ((RTC.get(DS1307_HR,false)) <= 19 && (RTC.get(DS1307_HR,false)) >= 17 ))
          {
            ledvar = 1;
            lcdPrint();
            digitalWrite(led, HIGH);
            // Serial.println(RTC.get(DS1307_DOW,false));    
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else
        {
          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }
        break;
      case 5:
        Serial.println("Friday");
        lcdPrint();
        lcd.setCursor(0,1);
        lcd.print("Friday");
        if (LDRReading < sensorValue)
        {
          if (((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9 ) || ((RTC.get(DS1307_HR,false)) <= 19 && (RTC.get(DS1307_HR,false)) >= 17 ))
          {
            ledvar = 1;
            lcdPrint();
            digitalWrite(led, HIGH);
            // Serial.println(RTC.get(DS1307_DOW,false));    
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else 
        {
          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }
        break;
      case 6:
        Serial.println("Saturday");
        lcdPrint();
        lcd.setCursor(0,1);
        lcd.print("Saturday");
        if (LDRReading < sensorValue)
        {
          if ((RTC.get(DS1307_HR,false)) <= 14 && (RTC.get(DS1307_HR,false)) >= 9)
          {
            ledvar = 1;
            lcdPrint();
            digitalWrite(led, HIGH);
            // Serial.println(RTC.get(DS1307_DOW,false));    
          }
          else 
          {
            digitalWrite(led, LOW);
            ledvar = 0;
            lcdPrint();
          }
        }
        else
        {
          digitalWrite(led, LOW);
          ledvar = 0;
          lcdPrint();
        }
        break;
      case 7:
        Serial.println("Sunday");
        lcdPrint();
        lcd.setCursor(0,1);
        lcd.print("Sunday");
        digitalWrite(led, LOW);
        ledvar = 0;
        lcdPrint();
        break;

      default:
        digitalWrite(led, LOW);
        lcdPrint();
        break;

      }
    }
    else {

      digitalWrite(led, LOW);
      lcdPrint1();
      lcd.setCursor(0,1);
      lcd.print("System is OFF");
    }
  }
  else {
    digitalWrite(led, LOW);
    lcdPrint1();
    lcd.setCursor(0,1);
    lcd.print("The alarm is ON");
  }
  delay(5000);
  lcd.clear();
}

Why are you always passing true to RTC.get()? That causes it to get the time from the chip again, every time.

So i need to change it to false?

You need to set it to true for the first call to RTC.get(), and false the rest of the time, in lcdPrint() and other functions that call RTC.get() more than once to get all the time data.

thanasisloi7:

thanasisloi7:
hi,

i am using arduino uno with ds1307, why when i put the code

serial.print((RTC.get(DS1307_HR,true)));

in a function and i call the function in the loop, at the serial the time is 0. but when i put the code in loop it’s ok?

my full code is

// This is for compatibility with both arduino 1.0 and previous versions

#if defined(ARDUINO) && ARDUINO >= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif

#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x38,16,2);

i

I recognise that you have alarms and LEDs added but the code looks wierd and somewhat verbose to a newbie like me. I believe the secret lies in the first line, and you would be better off using the latest IDE and libraries. It’s all free, and if you had them your problem might not exist.