-
Baby steps...Clock & Display
03/12/2015 at 03:43 • 0 commentsI 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(); }
-
Coding...How do you start?
03/04/2015 at 16:05 • 0 commentsWhile waiting for my Spark Photon to arrive, I decided to code up using the Pro Mini as a stand in. I like to block out my code using comments, that way I can identify parts of code that need to exist and put them in the appropriate places. Not having any Formal schooling on code or code writing, I was just wondering if this is a "Thing"... comment coding, and if it's recommended, discouraged, etc. I know it works for me, but what do you PROs do?
////////////////////////Here there be loops//////////////////////// void loop() { //Check for user interaction //encoder button AdminLoop(); //door reed switch //alert parents //Check for scheduled alarm status e.g. sleep time/wake time //ChronoDot holds 2 alarms and will pull SQW pin low when alarm times are reached //Check the time //update the 7seg display //update the Neopixel ring } void AdminLoop() { //Administrative actions loops //Loop to set time - Blue Encoder boolean blnSetTime=true; while (blnSetTime) { //Check for user interaction //encoder button blnSetTime=false; //encoder spin //update the 7seg display //update the Neopixel ring } //Loop to set go to sleep - Red Encoder //Loop to set wake up - Green Encoder } void Update7seg(String Time){ //put stuff into the 7seg display } void UpdateNeopixel(int Color, int NumLeds){ //paint up the Neopixel Ring }
-
Spark Photon delivery delayed
03/01/2015 at 03:50 • 0 commentsSo I was hoping to have my Spark Photon in hand by March, but they had some setbacks in manufacturing, and delivery has been pushed back til April. Standing in for the Photon then is the ever capable Arduino Pro Mini 328 - 5V/16Mhz.
I've tested each of the individual components I have on the Pro Mini one at a time, and so far they work wonderfully. I was initially worried about the Rotary Encoder, but after watching Kevin Darrah's video, I worked up the courage to wire it up and test it out. His code uses interrupts for the rising and falling of pins 2 and 3 on the Arduino, and after watching the numbers on the Serial Monitor dutifully increment and decrement as I spun that encoder, I was satisfied that this thing might just work.
I'm hoping to use the RGB and encoder as the only user interact able part of the TDM-SE-C. Pushing the button starts the user modifiable options like setting the clock, setting the wake time, setting the sleep time, with the color of the encoder letting you know what setting you are modifying.
Next I'll be piecing the Arduino code together for the Encoder, ChronoDot, and 4Digit-7Segment display and hit the Pro Mini with it, to see what flys.