clock project!

I made time today.

image

After many MANY stops and starts and ups and downs, I was able to get the Arduino to resemble a clock the way I (kind of) wanted it to.

I didn’t receive the RTC shield until last night, do things came down to the wire on this.

I’m pretty happy with it. I learned how to:

  • switch the time from 24-hour to 12-hour
  • display the day of the week in words
  • move characters on the LCD display
  • attempted to display month as words (ergo learned about DateTime & RTC libraries)
  • how the RTC shield works

In general, this is a cool test of the things we’ve learned so far in class. Of course, it’s a lot of information to retain, but it helped with practice. One of the awesome aspects of the project was using all the open source info that people have put out there to be accessed.

One of the goals of the project that was working up until today was the photocell. The idea sprang from the desire to dim the display when there was less light detected. Unfortunately, it doesn’t seem to want to work on this final iteration. Additionally, I had really wanted to put the clock inside a casing, but alas…

This was super fun.

Here’s the code:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include “RTClib.h”
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int led = 9;
int brightness = 0;
#if defined(ARDUINO_ARCH_SAMD) // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

RTC_DS1307 rtc;
DateTime now;
char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
char months[12][4] = {“JAN”, “FEB”, “MAR”, “APR”, “MAY”, “JUN”, “JUL”, “AUG”, “SEP”, “OCT”, “NOV”, “DEC”};

void setup () {

#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif

Serial.begin(57600);
if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}

if (! rtc.isrunning()) {
Serial.println(“RTC is NOT running!”);
}
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.setCursor(8, 3);
lcd.print(“:”);
lcd.setCursor(0, 2); //column then row, counting 0
lcd.print(“yay!!!”);

}

void updateDisplay() {
int h = now.hour();
int m = now.minute();
int s = now.second();
lcd.setCursor(7,3);
if (h > 13) {
lcd.print(h – 12);
lcd.setCursor(12, 3);
lcd.print(“pm”);
}
else {
lcd.print(“am”);
}
//if (h>10)
//{
//lcd.setCursor(8, 2);
//}
//else {
// lcd.setCursor(9,2);
//}

lcd.setCursor(9, 3);
if (m < 10) {
lcd.print(0);
}
lcd.print(m);

int y = now.year();
int n = now.month();
int d = now.day();
//lcd.setCursor(10, 1);
//lcd.print(d);
lcd.setCursor(16, 1);
lcd.print(y);
lcd.setCursor(15, 1);
lcd.print(“-“);
lcd.setCursor(10,1);
lcd.print(n);
lcd.setCursor(12, 1);
lcd.print(“-“);
lcd.setCursor(13,1);
if (d < 10) {
lcd.print(0);
}
lcd.print(d);

}

void loop () {

int fotovalue = analogRead(A0); //assigns name to photoresistor and identifies analog pin
Serial.println(fotovalue);
brightness = map(fotovalue, 0, 1023, 0, 255);
analogWrite(led, brightness);

now = rtc.now();
updateDisplay();

Serial.print(now.year(), DEC);
Serial.print(‘/’);
Serial.print(now.month(), DEC);
Serial.print(‘/’);
Serial.print(now.day(), DEC);
Serial.print(” (“);
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(“) “);
Serial.print(now.hour(), DEC);
Serial.print(‘:’);
Serial.print(now.minute(), DEC);
Serial.print(‘:’);
Serial.print(now.second(), DEC);
Serial.println();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
//lcd.setCursor(0,1);
//lcd.print(months[now.month()-1]);
Serial.println();
delay(3000);
}

Also, reference links:

  1. http://www.dummies.com/how-to/content/how-to-display-the-time-for-your-arduino-clock-pro.html
  2. https://www.arduino.cc/en/Reference/Array
  3. https://www.arduino.cc/en/Reference/Char
  4. https://www.arduino.cc/en/Reference/ASCIIchart
  5. http://tronixstuff.com/2014/12/01/tutorial-using-ds1307-and-ds3231-real-time-clock-modules-with-arduino/
  6. serial.print(new.year() dec)
  7. http://forum.arduino.cc/index.php?topic=197991.0
  8. http://forum.arduino.cc/index.php?topic=260526.0
  9. https://www.arduino.cc/en/Reference/Else
  10. http://php.net/manual/en/class.datetime.php
  11. https://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/understanding-the-code
  12. http://datasheets.maximintegrated.com/en/ds/DS1307.pdf
  13. https://learn.adafruit.com/photocells/using-a-photocell
  14. https://www.arduino.cc/en/Tutorial/HelloWorld
  15. http://www.instructables.com/id/RTC-SET-AND-SHOW-THE-TIME-ON-YOUR-LCD/step4/The-set-rtc-sketch-from-member-WWC-take-this/

A lot of this was useless, but just in case, I can come back to it.