Skip to content

Instantly share code, notes, and snippets.

@woodif
Created December 14, 2021 18:17
Show Gist options
  • Save woodif/becef34629ad2d4b3b9c45b64839bfe6 to your computer and use it in GitHub Desktop.
Save woodif/becef34629ad2d4b3b9c45b64839bfe6 to your computer and use it in GitHub Desktop.
/**
* www.arduinona.com
* ตัวอย่างการเซ็ตค่าเวลา ให้ DS3231
* ใช้คู่กับ Library DS3231 จาก https://github.com/PilleStat/PilleStat/tree/master/arduino%20code/libraries/DS3231
* วิธีการต่อ
* Arduino UNO -> DS3231
* 5V -> VCC
* GND -> GND
* SDA -> A4
* SCL -> A5
*/
#include <Wire.h>
#include "DS3231.h"
RTClib RTC;
void setup() {
/**
* เริ่มการสื่อสารกับ Serial monitor
*/
Serial.begin(115200);
/**
* เริ่มการสื่อสารแบบ I2C
*/
Wire.begin();
}
void loop () {
/**
* หน่วงเวลาดึงทุกๆ 1 วินาที
*/
delay(1000);
/**
* ดึงเวลาจากโมดูล RTC ด้วยคำสั่ง RTC.now() เก็บไว้ใน object ชือว่า now
* ซึ่งสามารถเรียกดึง วันเดือนปีชั่วโมงนาทีวินาที ได้ผ่านฟังก์ชันตัวอย่างด้านล่าง
*/
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment