Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Arduino Real TIme Clock

/*
      A simple program designed to setup and demonstrate the DS1302 RTC library and 
      DS1302 Real Time Clock Module - BKAA100056
     
      The program uses the DS1302 RTC library to initialise the DS1302 chipset 
      and then read the time from it every second.
      
      modified 03 March 2019
      by Sebastian Karam - Flux Workshop
      
      The DS1302 RTC library created by Matt Sparks
      https://github.com/msparks/arduino-ds1302
    */ 
    #include "DS1302.h" // include the DS1302 RTC library
     
    const int ResetPin = 8;  // reset Pin
    const int DataPin = 7;  // data Pin
    const int ClockPin = 6;  // clock Pin
     
    DS1302 rtc(ResetPin, DataPin, ClockPin); // create a DS1302 object
     
    String dayAsString(const Time::Day day) { // function that converts the day ouptput into a string
      switch (day) {
        case Time::kSunday: return "Sunday";
        case Time::kMonday: return "Monday";
        case Time::kTuesday: return "Tuesday";
        case Time::kWednesday: return "Wednesday";
        case Time::kThursday: return "Thursday";
        case Time::kFriday: return "Friday";
        case Time::kSaturday: return "Saturday";
      }
      return "(unknown day)";
    }
     
    char * readTime() { // function that reads the time from the chip and returns it in a character array
      Time t = rtc.time(); // get the time and date from the chip.
      const String day = dayAsString(t.day); // obtain text for the day of the week
      static char CurrentTime[50]; // initialise a character array to hold the date text
      snprintf(CurrentTime, sizeof(CurrentTime), "%s %04d-%02d-%02d %02d:%02d:%02d", day.c_str(), t.yr, t.mon, t.date, t.hr, t.min, t.sec); // format the time into the character array
      return CurrentTime; // return the current time
    }
     
     
    void setup() {
      Serial.begin(9600); // initialise the serial connection
      rtc.writeProtect(false); // turn off write protection
      rtc.halt(false); // clear the clock halt flag
      Time t(2019, 3, 03, 1, 23, 45, Time::kFriday); // create a new time object with set date
      rtc.time(t); // send the time to the chipset
    }
     
    // Loop and print the time every second.
    void loop() {
      char * timearray = readTime(); // obtain the time from readtime()
      Serial.println(timearray); // print the time to the serial monitor
      delay(1000); // pause before looping
    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: STD::ERASE FUNCTION IN C++ 
Cpp :: swap alternate elements in an array c++ problem 
Cpp :: How to pass a multidimensional array to a function in C and C++ 
Cpp :: ifstream 
Cpp :: evennumbers 1 to 100 
Cpp :: #define in cpp 
Cpp :: compare values within within a vector c++ 
Cpp :: C++ float and double Using setprecision() For Floating-Point Numbers 
Cpp :: find factorial in c++ using class 
Cpp :: how to insert in a queue c++ 
Cpp :: cpp custom exception 
Cpp :: phi function (n log (log(n))) 
Cpp :: assign value to a pointer 
Cpp :: How to see gateway on linux 
Cpp :: sort 2d vector c++ 
Cpp :: binary to decimal online converter 
Cpp :: Check whether the jth object is in the subset 
Cpp :: C++ ss 
Cpp :: gtest assert not equal 
Cpp :: the amount of input is unknown 
Cpp :: print all variables separated by comma c++ 
Cpp :: right rotation of array in c++ by one element 
Cpp :: C++ Detect when user presses arrow key 
Cpp :: C++ using a member function of a class to pass parameters to a thread 
Cpp :: coin change top-down 
Cpp :: sideways triangle c++ xy plane 
Cpp :: c++ program to convert celsius to fahrenheit 
Cpp :: stack algorithm in c++ 
Cpp :: input many numbers to int c++ 
Cpp :: c++ tuple example 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =