Tuesday, 31 October 2017

A fortuitous ebay mistake

Today, my ultrasonic sensor arrived with the 4 digit display.  BUT - instead of the bare 4 digit display, they sent me a display module with a 4 pin interface, which is good, but it is a clock module with a colon in the middle, rather than normal digits - each with a decimal point.
After a little digging around, I have found a library to drive the module and figured out how to use it.  As it happens - since I wouldn't need a decimal point, I could use this module in the water tank gauge project, but I have already pointed out the mistake to the seller, in the hope he will send me a new display of the type shown in the ebay listing.

But as I now have an LED display geared up for a clock type display, I thought - why not pair it up with my new RTC module and have a little clock.  Ok - so it only shows hours and minutes, but it will help me to test the longevity of the 2032 battery in the DS3231.

Here is my code for the clock

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <DS3231.h>
#include <TM1637Display.h>


// Module connection pins (Digital Pins)
#define CLK 9
#define DIO 8

// Initialise LED
TM1637Display display(CLK, DIO);

// Initialise RTC & RTC variables
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

// other variables
long counter;
uint8_t mask = 0b00010000;


void setup() {

// turn off annoying LED on 13
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);

// set brightness of LED
  display.setBrightness(0x0f);

// set all segments off for all digits
  uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 };
  display.setSegments(data);

// store current millis()
  counter = millis();
}

void loop() {
// get Time from RTC
  int hrs = Clock.getHour(h12,PM);
  int mins = Clock.getMinute();

  if (hrs > 12) hrs -= 12;
  
// display the hours with colon enabled/disabled as each second passes
  // mask for colon on digit 2
  // 0x80>>3 = 0b00010000 (I have no idea WHY this works!)

  if (millis() - counter > 1000) {
    if (mask == 0b00010000) {
      mask = 0b00000000;
    } else {
      mask = 0b00010000;
    }
    counter = millis();
  }
  display.showNumberDecEx(hrs,mask,false,2,0);

// display the minutes
  display.showNumberDec(mins,true,2,2);

}

I have tried forcing the hours to am/pm mode, as the RTC always seems to show it in 24 hour format (I expect there is a way to do it using the library functions, but I didn't find it yet), and then apply leading zero suppression to the hours, so that it shows as 9:00 for example, rather than 09:00.  However, I'm not sure what'll happen between midnight and 1am - perhaps the hour will not show at all...?  I won't be awake to see it though - at least not tonight!

I have no clue why the mask that controls the colon being displayed works... it is a bitwise shift right by three places, but by my reckoning, as the left most bit in the mask is a shift of 0, then the mask should be using a shift of either 1 or 2 places (depending if the colon is linked to the right side of digit 2 or the left side of digit 3.  But - I got it working by trial and error, and have even managed to make it flash every second.

Tomorrow, hopefully, I may start playing with the ultrasonic sensor unit.  Exciting!!

Update - 2 days later:
The RTC is still holding the time well.  I did originally have some issues with it randomly resetting itself, but I think was down to my dodgy prototyping with modules just hanging off the Uno on the end of DuPont cables - things were possibly touching other things and shorting out, etc.  After I blu-tacked all the modules down on a base board - no more problems.
By the way - even with leading zero suppression, times between midnight and 1 o'clock DO show with a single zero for the hour, as I hoped.

Update - 3 weeks later:
It is still holding the time, but given that my display is only hours and minutes, I haven't confirmed just how accurate the time is.  But the fact it still has a valid time that is even roughly correct is a big win.  

No comments:

Post a Comment