Created
September 29, 2017 03:33
-
-
Save izzuddin91/85f5f3985d651b85d5d2eccfe53354da to your computer and use it in GitHub Desktop.
full complete for arduino and relay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ADDED ON 24 JULY 2017 | |
| //connection: | |
| //GND to GND | |
| //VCC TO V in | |
| //SDA to A4 | |
| //SCL to A5 | |
| // Written by John Boxall from http://tronixstuff.com | |
| #include "Wire.h" | |
| #define DS3231_I2C_ADDRESS 0x68 | |
| // Convert normal decimal numbers to binary coded decimal | |
| byte decToBcd(byte val){ | |
| return( (val/10*16) + (val%10) ); | |
| } | |
| // Convert binary coded decimal to normal decimal numbers | |
| byte bcdToDec(byte val){ | |
| return( (val/16*10) + (val%16) ); | |
| } | |
| void setup(){ | |
| Wire.begin(); | |
| Serial.begin(9600); | |
| pinMode(13, OUTPUT); | |
| // set the initial time here: | |
| // DS3231 seconds, minutes, hours, day, date, month, year | |
| //uncomment line kat bawah ni kalau nak reset time balik | |
| //setDS3231time(00,28,11,6 ,22, 9,17); | |
| delay(3000); | |
| } | |
| void loop(){ | |
| //// displayTime(); // display the real-time clock data on the Serial Monitor, | |
| int hour = returnHour(); | |
| //// Serial.println(hour); | |
| displayTime(); | |
| if( (hour > 7) && (hour < 18 ) ){ | |
| // Serial.println("its daylight. turn off the led"); | |
| digitalWrite(13, LOW); | |
| }else{ | |
| // Serial.println("its nighttime. turn on led"); | |
| digitalWrite(13, HIGH); | |
| }; | |
| // | |
| // | |
| ////pause for every 30 minutes. hopefully this will reduce power consumption. | |
| // | |
| //delay(1800000); // every second | |
| }; | |
| void displayTime(){ | |
| byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; | |
| // retrieve data from DS3231 | |
| readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, | |
| &year); | |
| // send it to the serial monitor | |
| Serial.print(hour, DEC); | |
| // convert the byte variable to a decimal number when displayed | |
| Serial.print(":"); | |
| if (minute<10){ | |
| Serial.print("0"); | |
| } | |
| Serial.print(minute, DEC); | |
| Serial.print(":"); | |
| if (second<10){ | |
| Serial.print("0"); | |
| } | |
| Serial.print(second, DEC); | |
| Serial.print(" "); | |
| Serial.print(dayOfMonth, DEC); | |
| Serial.print("/"); | |
| Serial.print(month, DEC); | |
| Serial.print("/"); | |
| Serial.print(year, DEC); | |
| Serial.print(" Day of week: "); | |
| switch(dayOfWeek){ | |
| case 1: | |
| Serial.println("Sunday"); | |
| break; | |
| case 2: | |
| Serial.println("Monday"); | |
| break; | |
| case 3: | |
| Serial.println("Tuesday"); | |
| break; | |
| case 4: | |
| Serial.println("Wednesday"); | |
| break; | |
| case 5: | |
| Serial.println("Thursday"); | |
| break; | |
| case 6: | |
| Serial.println("Friday"); | |
| break; | |
| case 7: | |
| Serial.println("Saturday"); | |
| break; | |
| } | |
| } | |
| // | |
| int returnHour(){ | |
| byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; | |
| // retrieve data from DS3231 | |
| readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, | |
| &year); | |
| delay(1000); | |
| return &hour; | |
| } | |
| void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte | |
| dayOfMonth, byte month, byte year){ | |
| // sets time and date data to DS3231 | |
| Wire.beginTransmission(DS3231_I2C_ADDRESS); | |
| Wire.write(0); // set next input to start at the seconds register | |
| Wire.write(decToBcd(second)); // set seconds | |
| Wire.write(decToBcd(minute)); // set minutes | |
| Wire.write(decToBcd(hour)); // set hours | |
| Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) | |
| Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) | |
| Wire.write(decToBcd(month)); // set month | |
| Wire.write(decToBcd(year)); // set year (0 to 99) | |
| Wire.endTransmission(); | |
| } | |
| void readDS3231time(byte *second, | |
| byte *minute, | |
| byte *hour, | |
| byte *dayOfWeek, | |
| byte *dayOfMonth, | |
| byte *month, | |
| byte *year){ | |
| Wire.beginTransmission(DS3231_I2C_ADDRESS); | |
| Wire.write(0); // set DS3231 register pointer to 00h | |
| Wire.endTransmission(); | |
| Wire.requestFrom(DS3231_I2C_ADDRESS, 7); | |
| // request seven bytes of data from DS3231 starting from register 00h | |
| *second = bcdToDec(Wire.read() & 0x7f); | |
| *minute = bcdToDec(Wire.read()); | |
| *hour = bcdToDec(Wire.read() & 0x3f); | |
| *dayOfWeek = bcdToDec(Wire.read()); | |
| *dayOfMonth = bcdToDec(Wire.read()); | |
| *month = bcdToDec(Wire.read()); | |
| *year = bcdToDec(Wire.read()); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment