Search
 
SCRIPT & CODE EXAMPLE
 

CPP

DS1302

/*
      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 :: casting to a double in c++ 
Cpp :: async multi thread 
Cpp :: unordered_map c++ 
Cpp :: how can I delete a substring from a string in c++? 
Cpp :: string append at position c++ 
Cpp :: __builtin_popcount 
Cpp :: web dev c++ 
Cpp :: round c++ 
Cpp :: order 2d array in c++ 
Cpp :: options select from array 
Cpp :: merge sort in descending order c++ 
Cpp :: ex: cpp 
Cpp :: A Program to check if strings are rotations of each other or not 
Cpp :: c++ main function parameters 
Cpp :: file streams in c++ 
Cpp :: queue cpp 
Cpp :: c++ shared pointer operator bool 
Cpp :: qt c++ qdockwidget remove title 
Cpp :: c++c 
Cpp :: how to do if command in c++ 
Cpp :: memset array bool 
Cpp :: binary to int c++ bitset 
Cpp :: C++14 (gcc 8.3) sample 
Cpp :: mpi wait 
Cpp :: is obje file binary?? 
Cpp :: overload operator object function call 
Cpp :: the question for me 
Cpp :: last index of array c++ 
Cpp :: c++ hide credentials 
Cpp :: traverse string in cpp 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =