Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save izzuddin91/8ea8bf97657d694955cfc7f0c9017b56 to your computer and use it in GitHub Desktop.

Select an option

Save izzuddin91/8ea8bf97657d694955cfc7f0c9017b56 to your computer and use it in GitHub Desktop.
7 segment led + RTC
//ADDED 24 JULY 2017
int segA = 11; // top
int segB = 7; // right-top
int segF = 10; // left-top
int segD = 2; // bottom
int segE = 1; // left-bottom
int segC = 4; // right-bottom
int segG = 5; // middle
int digit1 = 14; // common cathode for digit1 (i.e. A0)
int digit2 = 15; // common cathode for digit2 (i.e. A1)
int digit3 = 16; // common cathode for digit3 (i.e. A2)
int digit4 = 17; // common cathode for digit4 (i.e. A3)
// Number to display
static int number = 0;
static int ms = 0;
//this part for clock
#define DS3231_I2C_ADDRESS 0x68
#include "Wire.h"
// 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) );
}
//this part for clock
void setup() {
// put your setup code here, to run once:
// All pins in digital mode
pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);
pinMode(digit3, OUTPUT);
pinMode(digit4, OUTPUT);
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
//clock part
Wire.begin();
// Serial.begin(9600); tak tau kenapa, tapi kalau biarkan gak nnti akan kacau led display
}
void loop() {
// put your main code here, to run repeatedly:
int hour = returnHour();
// kalau number of hours more than 12, kene tolak 12. hanya boleh cater 1 digit
if (hour >= 12){
hour = hour - 12;
}
int totalminute = returnMinute();
int firstminute = (totalminute ) % 10 ;
int secondminute = (totalminute / 10) % 10 ;
digitalWrite(digit1, LOW);
// light on segments for 'thousands'
drawDigitFast( hour );
// wait 2 ms
delay(2);
// turn off all segments
drawDigitFast( -1 );
// turn off cathode for digit1
digitalWrite(digit1, HIGH);
digitalWrite(digit2, LOW);
drawDigitFast( (number/100) % 10 );
delay(2);
drawDigitFast( -1 );
digitalWrite(digit2, HIGH);
digitalWrite(digit3, LOW);
drawDigitFast( secondminute );
delay(2);
drawDigitFast( -1 );
digitalWrite(digit3, HIGH);
digitalWrite(digit4, LOW);
drawDigitFast( firstminute );
delay(2);
drawDigitFast( -1 );
digitalWrite(digit4, HIGH);
}
void drawDigitFast(int n)
{
const byte aPins[8] = {
segA, segB, segC, segD, segE, segF, segG
};
const byte aSegments[11][8] = {
// A B C D E F G
{ HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW }, // 0
{ LOW, HIGH, HIGH, LOW, LOW, LOW, LOW }, // 1
{ HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH }, // 2
{ HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH }, // 3
{ LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH }, // 4
{ HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH }, // 5
{ HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH }, // 6
{ HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW }, // 7
{ HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH }, // 8
{ HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH }, // 9
{ LOW, LOW, LOW, LOW, LOW, LOW, LOW } // all off
};
if( n < 0 || n > 10 )
{
n = 10;
}
for( int i = 0; i < 7; i++ )
{
digitalWrite( aPins[i], aSegments[n][i] );
}
}
// void function for clock
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());
}
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);
// send it to the serial monitor
// Serial.print(hour, DEC);
return hour;
}
int returnMinute(){
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);
return minute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment