Code help for Advance Arduino Clock

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
fruitkid101
 
Posts: 17
Joined: Mon Mar 08, 2010 2:49 am

Code help for Advance Arduino Clock

Post by fruitkid101 »

Hey, so I built this clock with the DS 1307 and 16x2 Lcd and my fabulous arduino. My original plan was to put it on my backpacks, so people could see the time as I was walking by. Now, I want to try to do something where it will display what Class Period it is at the time. I'm thinking it's probably an "if (so and so time)" then "lcd.print (7th period) or something like that. I don't really know how though.
It shows the time, and date on the first line. And "Cruz's Backpack" on the second line. I was thinking of having it switch back and forth between my name and the class period, like every 5 seconds. Any code ideas? I'd appreciate it. I'm also posting a picture of my setup, and my full code :)

Code: Select all

// Date and time functions using just software, based on millis() & timer

#include "LiquidCrystal.h"
#include <Wire.h>
#include "RTClib.h"

#define LCD_I2C_ADDR 0
#define LCD_COLUMNS 16
#define LCD_ROWS 2

#define DATE_ROW 0
#define DATE_COL 0

#define TIME_ROW 0
#define TIME_COL 8

#define BANNER_ROW 1
#define BANNER_COL 1

RTC_Millis RTC;
LiquidCrystal lcd(LCD_I2C_ADDR);
RTC_DS1307 rtc;

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

  // initialize the LCD
  lcd.begin(LCD_COLUMNS, LCD_ROWS);
  lcd.setBacklight(HIGH);
  lcd.clear();
    // following line sets the RTC to the date & time this sketch was compiled
   
   // RTC.begin(DateTime(__DATE__,__TIME__));
    
     lcd.setCursor(BANNER_COL, BANNER_ROW);
  lcd.print("Cruz's BackPack");
}

void print_padded_digit(int value) {
  if (value < 10) {
    lcd.print('0');
  }
  lcd.print(value, DEC);
}

//! Print the month, day, and time to the LCD.
/*!
Prints the date in mm/dd format and the time in HH:MM:SS to the LCD.
\param now the current date and time
*/
void print_date_time(DateTime *now) {
  lcd.setCursor(DATE_COL, DATE_ROW);
  print_padded_digit(now->month());
  lcd.print('/');
  print_padded_digit(now->day());

  lcd.setCursor(TIME_COL, TIME_ROW);
  print_padded_digit(now->hour());
  lcd.print(':');
  print_padded_digit(now->minute());
  lcd.print(':');
  print_padded_digit(now->second());
}

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();
    
    Serial.print(" seconds since 1970: ");
    Serial.println(now.unixtime());
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(1000);
    
   

  print_date_time(&now);
  

}

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

Re: Code help for Advance Arduino Clock

Post by adafruit_support_bill »

Look at the "datecalc" example in the DS1307 library. If you use the ".unixtime()" member of the DateTime object, it converts the date and time into a long integer so you can do a simple mathematical comparison.

First build a schedule using the DateTime objects (Just fill in the hours and minutes fields and leave all the others as zero).
Then read the current time from the RTC and zero out the year, month and day fields. Now you can compare the current time with the schedule:

Code: Select all

if ((now.unixtime() > period1.unixtime()) && (now.unixtime() < period2.unixtime()))
{
}

etc...

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

Return to “Arduino”