top of page

​

                          Included file is the arduino code to set the DS3231 module clock

​

​

​

​

​

​

//   Arduino Clock
//   Daniel Rhodes

#include <cactus_io_AM2302.h>
#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC
#include "pitches.h"                //library for tone generator
#include <pitches.h>

LiquidCrystal_I2C led(0x27, 20, 4); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68


// Time Set Buttons
int button1;
int button2;

// Pins definition for Time Set Buttons
int hh = 5; // pin 5 for Hours Setting
int mm = 6; // pin 6 for Minutes Setting
/********************************/
// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};


void updateRTC()
{

  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"
                          };
  String str = "";
  long sedate[6];

  while (Serial.available()) {
    Serial.read(); 
  }
 
  for (int i = 0; i < 6; i++) {
    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");
    while (!Serial.available()) {
      ; // wait for user input
    }
    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array
    Serial.println(newDate[i]); // show user input
  }
  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{
  /*
     create array to convert digit days to words:
     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
  /*
     create array to convert digit months to words:
     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
                                   };
  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();
  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  lcd.setCursor(0,0);
  lcd.print("Daniel Rhodes");
  lcd.setCursor(0,1);
  lcd.print("Digital Clock");


                                          // move LCD cursor to upper-left position
  lcd.setCursor(0, 3);                   // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");           // add preceding '0' if number is less than 10 (just looks better for user)
  lcd.print(monthInWords[MM]);
  lcd.print(" ");
  lcd.print(dd);
  lcd.print(" ");
  lcd.print(yyyy);
  lcd.print("  ");
  lcd.print(dayInWords[DD]);
  // move LCD cursor to lower-left position
  lcd.setCursor(0, 2);
  // print time in 12H format
 
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');
 
  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" AM");       // print AM/PM indication
  else lcd.print(" PM");                      //not necessary, but I wanted to add this
}

void setup()
{
  Serial.begin(9600);          // initialize serial
  lcd.init();                  // initialize lcd
  lcd.backlight();             // switch-on lcd backlight
  rtc.begin();                 // initialize rtc
  pinMode(hh,INPUT_PULLUP);    // avoid external Pullup resistors for Button 1
  pinMode(mm, INPUT_PULLUP);   // and Button 2

  /********************************/                                //plays tone on startup to confirm speaker is functioning
for (int thisNote = 0; thisNote < 8; thisNote++) {    

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
 
}

void loop()
{
    // Display the temperature                                Temp Sensor does not work with full circuit, but I included
    //  Serial.print("T=");                                   the code commented out
    //  Serial.print(Clock.getTemperature(), 2);
    

    // read Setting Buttons
  button1 = digitalRead(hh);                           // Read Buttons
  button2 = digitalRead(mm);                           // These buttons were supposed to adjust Hour and Minute, but I couldn't get them working
  if (button1 == 0) {
    hh = hh + 1;
  }

  if (button2 == 0) {
    mm = mm + 1;
  }
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }
// Indicate whether an alarm went off
// if (Clock.checkIfAlarm(1)) {
// Serial.print(" A1!");
// }
// New line on display
// Serial.print('\n');
// Display Alarm 1 information
//  Serial.print("Alarm 1: ");
//  Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
//  Serial.print(ADay, DEC);
//  if (ADy) {
//    Serial.print(" DoW");
//  } else {
//    Serial.print(" Date");
//  }
//  Serial.print(' ');
//  Serial.print(AHour, DEC);
//  Serial.print(' ');
//  Serial.print(AMinute, DEC);
//  Serial.print(' ');
//  Serial.print(ASecond, DEC);
//  Serial.print(' ');
//  if (A12h) {
//    if (Apm) {
//      Serial.print('pm ');
//    } else {
//      Serial.print('am ');
//    }
//  }
//  if (Clock.checkAlarmEnabled(1)) {
//    Serial.print("enabled");
//  }
//  Serial.print('\n');
}

bottom of page