Pages: [1]   Go Down
Author Topic: DS3234 RTC & Alarms  (Read 1334 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I'm banging my head against a wall trying to figure this out and I'm sure it's a lot easier than what my brain is making of it... Needless to say, I really need some help!

I'm using this RTC based on a Maxim DS3234 RTC chip... http://www.sparkfun.com/products/10160

I'm using this guys library, https://github.com/maniacbug/RTClib

The library works fine, the device is keeping time properly, but I cannot figure out how to setup a simple alarm.... I want to turn pins high/low, etc at certain times. I don't really care about what day it is, just the time in 24hr format.

Like, If time == 1530{pinA, high}

Any advice on how to interface with this library to set specific times for alarms?  Here is the sample code...

Thanks!

Quote
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
RTC_DS3234 RTC( 8 ) ;

void setup () {
    Serial.begin(57600);
    Serial.println("RTClib/examples/ds3234/");
    SPI.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    Serial.print("Setting time to... ");
    Serial.print(__DATE__);
    Serial.print(' ');
    Serial.println(__TIME__);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    const int len = 32;
    static char buf[len];

    DateTime now = RTC.now();

    Serial.println(now.toString(buf,len));
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // 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.println(future.toString(buf,len));
    
    Serial.println();
    delay(3000);
}

« Last Edit: December 11, 2011, 04:17:54 pm by FlyingSteve » Logged

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are initializing the SPI library, but not the Wire library.
I have a timer-based project that uses the ChronoDot (similar to the chip you're using), available on GitHub:
Check out the chargetimer.pde from this project:
https://github.com/jwatte/arduino-chargetimer
Also feel free to use the DateTime library (in DateTime.h and DateTime.cpp) which does both date math (with leap year, but not leap seconds or DST), and read/write the RTC.

Also, in your sketch, you don't actually compare the time to something, and write a pin high.
I would do something like this inside loop():
- use Wire to read the "hour" register
- use Wire to read the "minute" register
- if hour == 0x15 && minute == 0x30 set A high, else set A low
« Last Edit: December 11, 2011, 05:17:51 pm by jwatte » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still confused lol. If I try to type in if(hour == etc...) the compiler comes back and says that "hour" was not declared in this scope.

This code prints the correct information to the serial port but I just don't know how to interface with it to use what it's printing...
Logged

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still confused lol. If I try to type in if(hour == etc...) the compiler comes back and says that "hour" was not declared in this scope.

This code prints the correct information to the serial port but I just don't know how to interface with it to use what it's printing...

"hour" was intended to be whatever variable you're using to hold the "hour" part of the current time. I apologize, because I thought your problem was in understanding the hardware, not in understanding the software.
Also, I made a reference to the I2C part because you're including the Wire library -- but you're actually using the SPI part, and probably shouldn't include the Wire library header at all. (The same hardware chip is available in I2C software version as 3231/3232, and SPI version as 3234).

So, if the problem is just a regular programming question about how to understand that particular RTC API, I'm afraid I can't be very helpful, other than to offer the general suggestion that you need to find the part of that library that lets you read the "hour" and "minute" values of the current time, and compare those to the values you want to use for the alarm. Then you need to actually write the pin that you want to control, based on what those values are.

I took a look at that library's implementation, and the values from the DateTime class are in decimal (not BCD) so the code you need to add inside loop() might look something like:

Code:
 if (now.hour() == 15 && now.minute() == 30) {
    digitalWrite(13, HIGH);
  }
  else {
    digitalWrite(13, LOW);
  }

Also make sure you set pinMode(13, OUTPUT) in the setup() function.
« Last Edit: December 12, 2011, 01:09:00 pm by jwatte » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 94
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

jwatte, thanks for the help. Understanding the software is my problem. I tried your suggestion and it worked perfectly. Thank you very much!

On a funny note, when I first tried your suggestion, set pin 13 to output, etc, it wouldn't work... what the hell... After a few minutes of frustration I look down at my Arduino board and realize that I'm using Pin 13 for SPI CLK function lol. That's how it goes when you're operating on 5hrs of sleep... Time for bed before my brain melts smiley
Logged

Pages: [1]   Go Up
Jump to: