Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MIT License
- Copyright (c) 2023
- Author: R McCleery [email protected]
- Date: May 3rd 2023
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- /*
- * PLEASE READ ME Before you begin
- *
- * #1 This sketch will work with any controller that supports hardware "Serial.begin(BAUD)"
- *
- * #2 When you set the time it is created by syncing your controller millis() to UTC EPOCH
- * including any timezone offsets defined by #8 below
- *
- * #3 The beauty of this sketch is you don't need a network connection or RTC module to make time
- *
- * #4 On ESP Dev boards the FLASH/BOOT button is used to enter "Update Time Mode" UDT button
- *
- * #5 On any other controllers you need to add a button wired between your UDT pin and GND
- *
- * #6 The "LED" has been defined as "LED_BUILTIN" which is supported on Uno, Nano and ESP8266
- * If you either don't have an on-board LED or it's a different pin your can change it and/or
- * wire up a LED to the PIN you define
- * The LED is used as in indicator when in "Update Time Mode"
- *
- * #7 You enter your local time as current "UTC" time which means
- * Lets say you are in a timezone that is UTC -5:00 and the local time is 12:00:00 [PM] ie:noon
- * Be mindful of daylight offset so if in summer time it would be UTC -4:00 if your timezone supports it.
- * You would enter 12 + 4 = 16 for the hour component if in daylight savings time
- *
- * #8 For correct local time you must enter your "POSIX" string as noted
- * Link to a database for "POSIX" strings
- * https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
- * This string will take care of your zone offset to UTC
- *
- * #9 You set your clock by pressing the button with the "Serial Monitor" Opened
- * Once you are in "Update Time Mode" you must enter the time in the correct format to continue
- * The correct format is dd/mm/yyyy hh:mm:ss with NO leading zeros
- * EG Local Time is 12:00:00 [PM] and it's the 3rd of May 2023
- * You'll enter 3/5/2023 16:0:0 the hit the return key and the time will start
- *
- * #10 You can download the "ezTime" library via the Arduino IDE library manager
- * see the Github Manual for more options https://github.com/ropg/ezTime
- *
- * #11 ******* VERY IMPORTANT TASK TO DISABLE NETWORK IN "exTime" ********
- * ******* Because we won't be using and external time sources ********
- * ***** We have to comment out one line in the "exTime.h" file ********
- * ***** After you install the "ezTime" library navigate to your ********
- * ***** "libraries" folder and then open "\ezTime\src\ezTime.h" ********
- * ***** just a few lines down on line 11 add "//" to *****************
- * *** "//#define EZTIME_NETWORK_ENABLE" to disable it *****************
- * ******* Save and close the file then you can compile *****************
- * ******* and upload your new clock sketch *****************
- *
- * #12 Look for "//************* XXXXX ************" for user configuration options
- *
- */
- //************ If you're using an ESP8266 uncomment this define *************
- //#define MY_ESP
- //************ Enter the POSIX string for your Timezone location *************
- //************ See READ ME #8 above *************
- #define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3"
- //Library to use our controllers clock for "Human Readable Local Time"
- #include <ezTime.h>
- Timezone myTZ;
- //************ LED and Button PIN here ****************************************
- //* If you areusing an ESP you can set "UDT 0" to use the FLASH/BOOT button ***
- #define LED LED_BUILTIN
- #define UDT 3
- //Non-blocking timer for button de-bounce and LED Blink functions
- #define DUTY_CYCLE 50
- //Variables
- uint32_t previousTime;
- uint8_t count = 0, blinkCount = 0, udtState,
- lastUDTstate = HIGH, ledState, lastSec;
- int Sc = 0, Mn = 0, Hr = 0, Dy = 1, Mh = 1, Yr = 2023;
- bool updateDateTime = false, ledUpdate = false,
- UDTpressed = false, blink = false, sPtime = true;
- //To test that you entered the time correctly
- bool inRange(int val, int minimum, int maximum){
- return ((minimum <= val) && (val <= maximum));
- }
- void myClock(void){
- Serial.println(myTZ.dateTime("l F jS Y g~:i~:s [A] [~U~T~C P]"));
- //formating options found at https://github.com/ropg/ezTime
- lastSec = myTZ.second();
- return;
- }
- void setup() {
- Serial.begin(115200);
- pinMode(LED, OUTPUT);
- #if defined MY_ESP
- digitalWrite(LED, HIGH);
- #else
- digitalWrite(LED, LOW);
- #endif
- pinMode(UDT, INPUT_PULLUP);
- myTZ.setPosix(TZ_INFO);
- UTC.setTime(Sc, Mn, Hr, Dy, Mh, Yr);
- lastSec = myTZ.second();
- }
- void loop() {
- //Watching for a button press
- udtState = digitalRead(UDT);
- if(udtState != lastUDTstate){
- UDTpressed = true;
- previousTime = millis();
- }
- //Once your clock has been set this runs
- if(sPtime){
- if(myTZ.second() != lastSec){
- myClock();
- }
- }
- //Non-Blocking button de-bounce and led blink timer function
- if(millis() - previousTime > DUTY_CYCLE){
- previousTime = millis();
- count++;
- blinkCount++;
- if(UDTpressed){
- if(udtState == LOW){
- Serial.println("********** Update Time and Date **********");
- Serial.println("********** Fast Blinking LED = True **********");
- Serial.println("********** No Leading ZERO's **********\n");
- Serial.println("\n*Enter Current myTZ Time in following Format*");
- Serial.println("\n** System will Calculate Your Local Time ***\n");
- Serial.println("\n* Type in: dd/mm/yyyy hh:mm:ss to update RTC *\n");
- updateDateTime = true;
- blink = true;
- }
- UDTpressed = false;
- }
- if(blink){
- if(blinkCount > 5){
- blinkCount = 0;
- ledState = !ledState;
- ledUpdate = true;
- }
- }
- if(ledUpdate){
- digitalWrite(LED, ledState);
- ledUpdate = false;
- }
- } // END millis() statements
- if(updateDateTime){
- sPtime = false;
- if(Serial.available() > 0) {
- Dy = Serial.parseInt();
- Mh = Serial.parseInt();
- Yr = Serial.parseInt();
- Hr = Serial.parseInt();
- Mn = Serial.parseInt();
- Sc = Serial.parseInt();
- char buf[40];
- sprintf(buf,"I have understood %u/%u/%u %u:%u:%u\n\n", Dy, Mh, Yr, Hr, Mn, Sc);
- Serial.println(buf);
- // Values being set ( Day(%d), Month(%m), Year(%Y), Hr(%H), Min(%M), Sec(%S) )
- boolean validDate = (inRange(Dy, 1, 31) && inRange(Mh, 1, 12) && inRange(Yr, 2021, 2031));
- boolean validTime = (inRange(Hr, 0, 23) && inRange(Mn, 0, 59) && inRange(Sc, 0, 59));
- if(validTime && validDate){
- UTC.setTime(Hr, Mn, Sc, Dy, Mh, Yr);
- Serial.flush();
- sPtime = true;
- blink = false;
- #if defined MY_ESP
- ledState = HIGH;
- #else
- ledState = LOW;
- #endif
- ledUpdate = true;
- updateDateTime = false;
- }
- }
- }
- lastUDTstate = udtState; // Update Last button State
- }
Advertisement
Add Comment
Please, Sign In to add comment