0

Output RTC Month to Alphanumeric Display
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Output RTC Month to Alphanumeric Display

by BluesPwr1009 on Fri Nov 10, 2017 12:18 am

I am using a DS1307 RTC breakout with an Arduino to create a clock. I have the time and year working properly, but I want to output the month to a 4-digit alphanumeric display. Eventually, I want to get the 2 digit numeric month to display as a 3 letter month on the display, but I was trying to start by getting the 2 digit month to display n the alphanumeric display, but I am unsure how to do it properly. Any help would be appreciated. Here is the code:

Code: Select all | TOGGLE FULL SIZE

#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "RTClib.h"


// Set to false to display time in 12 hour format, or true to use 24 hour:
#define TIME_24_HOUR      false

// I2C address of the display.  Stick with the default address of 0x70
// unless you've changed the address jumpers on the back of the display.
#define DISPLAY_ADDRESS   0x70
#define DISPLAY_ADDRESS_2   0x71
#define DISPLAY_ADDRESS_3   0x72


// Create display and DS1307 objects.  These are global variables that
// can be accessed from both the setup and loop function below.
Adafruit_7segment clockDisplay = Adafruit_7segment();
Adafruit_7segment yearDisplay = Adafruit_7segment();
Adafruit_AlphaNum4 monthDisplay = Adafruit_AlphaNum4();
RTC_DS1307 rtc = RTC_DS1307();

// Keep track of the hours, minutes, seconds displayed by the clock.
// Start off at 0:00:00 as a signal that the time should be read from
// the DS1307 to initialize it.
int hours = 0;
int minutes = 0;
int seconds = 0;
int years = 0;
int months = 0;
char monthArray[2];

// Remember if the colon was drawn on the display so it can be blinked
// on and off every second.
bool blinkColon = false;


void setup() {
  // Setup function runs once at startup to initialize the display
  // and DS1307 clock.

  // Setup Serial port to print debug output.
  Serial.begin(115200);
  Serial.println("Clock starting!");

  // Setup the clock display.
  clockDisplay.begin(DISPLAY_ADDRESS);

    //Setup the year display.
  yearDisplay.begin(DISPLAY_ADDRESS_2);

      //Setup the year display.
  monthDisplay.begin(DISPLAY_ADDRESS_3);

  // Setup the DS1307 real-time clock.
  rtc.begin();

  // Set the DS1307 clock if it hasn't been set before.
  bool setClockTime = !rtc.isrunning();
  // Alternatively you can force the clock to be set again by
  // uncommenting this line:
  //setClockTime = true;
  if (setClockTime) {
    Serial.println("Setting DS1307 time!");
    // This line sets the DS1307 time to the exact date and time the
    // sketch was compiled:
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // Alternatively you can set the RTC with an explicit date & time,
    // for example to set January 21, 2014 at 3am you would uncomment:
    //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

}

void loop() {
  // Loop function runs over and over again to implement the clock logic.
 
  // Check if it's the top of the hour and get a new time reading
  // from the DS1307.  This helps keep the clock accurate by fixing
  // any drift.
  if (minutes == 0) {
    // Get the time from the DS1307.
    DateTime now = rtc.now();
    // Print out the time for debug purposes:
    Serial.println("Read date & time from DS1307: ");
    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();
    // Now set the hours and minutes.
    hours = now.hour();
    minutes = now.minute();
    years = now.year();
    months = now.month();
  }

  // Show the time on the display by turning it into a numeric
  // value, like 3:30 turns into 330, by multiplying the hour by
  // 100 and then adding the minutes.
  int displayValue = hours*100 + minutes;

  // Do 24 hour to 12 hour format conversion when required.
  if (!TIME_24_HOUR) {
    // Handle when hours are past 12 by subtracting 12 hours (1200 value).
    if (hours > 11) {
      displayValue -= 1200;
    }
    // Handle hour 0 (midnight) being shown as 12.
    else if (hours == 0) {
      displayValue += 1200;
    }
  }

  // Now print the time value to the display.
  clockDisplay.print(displayValue, DEC);

  // Now print the year value to the display.
  yearDisplay.print(years, DEC);
  yearDisplay.writeDisplay();

  //Convert months integer into a character array.
  String m;
  m = String(months);
  m.toCharArray(monthArray,2);
 
    // Now print the month value to the display.
  monthDisplay.writeDigitAscii(2,monthArray[1]);
  monthDisplay.writeDigitAscii(3,monthArray[2]);
  monthDisplay.writeDisplay();

  // Add zero padding when in 24 hour mode and it's midnight.
  // In this case the print function above won't have leading 0's
  // which can look confusing.  Go in and explicitly add these zeros.
  if (TIME_24_HOUR && hours == 0) {
    // Pad hour 0.
    clockDisplay.writeDigitNum(1, 0);
    // Also pad when the 10's minute is 0 and should be padded.
    if (minutes < 10) {
      clockDisplay.writeDigitNum(2, 0);
    }
  }

  // Blink the colon by flipping its value every loop iteration
  // (which happens every second).
  blinkColon = !blinkColon;
  clockDisplay.drawColon(blinkColon);

  // Now push out to the display the new values that were set above.
  clockDisplay.writeDisplay();

  // Pause for a second for time to elapse.  This value is in milliseconds
  // so 1000 milliseconds = 1 second.
  delay(1000);

  // Now increase the seconds by one.
  seconds += 1;
  // If the seconds go above 59 then the minutes should increase and
  // the seconds should wrap back to 0.
  if (seconds > 59) {
    seconds = 0;
    minutes += 1;
    // Again if the minutes go above 59 then the hour should increase and
    // the minutes should wrap back to 0.
    if (minutes > 59) {
      minutes = 0;
      hours += 1;
      // Note that when the minutes are 0 (i.e. it's the top of a new hour)
      // then the start of the loop will read the actual time from the DS1307
      // again.  Just to be safe though we'll also increment the hour and wrap
      // back to 0 if it goes above 23 (i.e. past midnight).
      if (hours > 23) {
        hours = 0;
      }
    }
  }

  // Loop code is finished, it will jump back to the start of the loop
  // function again!
}

BluesPwr1009
 
Posts: 22
Joined: Sat Aug 22, 2015 5:52 pm

Re: Output RTC Month to Alphanumeric Display

by adafruit_support_bill on Fri Nov 10, 2017 8:48 am

What is being displayed now using the code you have posted?

adafruit_support_bill
 
Posts: 62909
Joined: Sat Feb 07, 2009 10:11 am

Re: Output RTC Month to Alphanumeric Display

by BluesPwr1009 on Fri Nov 10, 2017 6:30 pm

A photo is attached.
Attachments
rsz_img_20171110_172205903.jpg
rsz_img_20171110_172205903.jpg (106.24 KiB) Viewed 42 times

BluesPwr1009
 
Posts: 22
Joined: Sat Aug 22, 2015 5:52 pm

Re: Output RTC Month to Alphanumeric Display

by adafruit_support_bill on Sat Nov 11, 2017 12:20 pm

I don't see anything obviously wrong there. Have you tested the display with the example code to make sure it is working OK?

adafruit_support_bill
 
Posts: 62909
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.