Close

Baby steps...Clock & Display

A project log for Toddler Door Monitor - Sleep Enforcer - Clock

A door monitor that goes one step further and shows toddlers when it is time to sleep or time to get up.

juniorJunior 03/12/2015 at 03:430 Comments

I put together some code to get the Chronodot and 7Seg display working together. I'm pretty happy with the results so far. Pic and code follow...I've got it doing 12hr or 24hr time...need to add in the AM/PM dot though...I'll try that next.

I've gotta say, I really love modulus (the % operator in the following code)! quite handy in these situations. If you aren't already familiar with it, while / is division and * is multiplication, % returns only the remainder after division has occured, i.e. the outcome of 13 % 2 is 1 ... the outcome of 23 % 10 is 3.

In the code I'm afraid to paint the AM/PM dot I'll have to use matrix.writeraw() and that's why I'm using itoa() to get the digits a characters. Might not be necessary though, a little testing might help.

Here's the code thus far. What do you pros think?

//7seg and RTC together--baby steps 1

//Includes//////////////////////////////////////////////////////////////////////////////////////////
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS3231.h>


//Global Variables//////////////////////////////////////////////////////////////////////////////////
Adafruit_7segment matrix = Adafruit_7segment();
RTC_DS3231 RTC;
volatile long TOGGLE_COUNT = 0;
boolean timein12hr = true;

//Defines////////////////////////////////////////////////////////////////////////////////////////////
#define SQW_FREQ DS3231_SQW_FREQ_1024     //0b00001000   1024Hz
#define PWM_COUNT 1020    //determines how often the LED flips
#define LOOP_DELAY 5000    //ms delay time in loop


void setup(){
    Serial.begin(9600);      
    matrix.begin(0x70);
    Wire.begin();
    RTC.begin();

    if (! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
      // following line sets the RTC to the date & time this sketch was compiled
      RTC.adjust(DateTime(__DATE__, __TIME__));
    }
  
    DateTime now = RTC.now();
    DateTime compiled = DateTime(__DATE__, __TIME__);
    if (now.unixtime() < compiled.unixtime()) {
      Serial.println("RTC is older than compile time!  Updating");
      RTC.adjust(DateTime(__DATE__, __TIME__));
    }
    
    RTC.enable32kHz(true);
    RTC.SQWEnable(true);
    RTC.BBSQWEnable(true);
    RTC.SQWFrequency( SQW_FREQ );
  
    char datastr[100];
    RTC.getControlRegisterData( datastr[0]  );
    Serial.print( datastr );
 
  
  
}

void loop(){

    DateTime now = RTC.now();
    paint7seg(now.hour(), now.minute(), now.second(), true);
    delay(50);
}

void paint7seg(int hours, int minutes, int seconds, boolean AMPM){
    
    //12 or 24 hour
    if (timein12hr){
          hours = hours % 12; //using modulus
        if (hours == 0){ hours = 12; }
    }
    
    //get the string digits
    //char cHours[4];
    //itoa(hours,cHours,10);
    //char cMinutes[4];
    //itoa(minutes,cMinutes,10);
    
    //paint the digits
    boolean drawDots = false;
    if (seconds % 2 == 0){
        drawDots = true;
    }
    if ( hours/10 >= 1 ){matrix.writeDigitNum(0, (hours / 10), drawDots);}
    matrix.writeDigitNum(1, (hours % 10), drawDots);
    matrix.drawColon(drawDots);
    matrix.writeDigitNum(3, (minutes / 10) % 10, drawDots);
    matrix.writeDigitNum(4, minutes % 10, drawDots);
    
    //blink the colon
    
    
    matrix.writeDisplay();
}

Discussions