/*---------------------------- ------------------------------ ------------
Project : Jam Digital DotMatrix Display
Author : diy ALC
Version : 3.0
------------------------------ ------------------------------ ----------*/
// Menyisipkan File Library
#include <Wire.h>
#include <RTClib.h>
#include <SPI.h>
#include <TimerOne.h>
#include <DMD1.h>
#include <SystemFont5x7.h>
#include <System6x7.h>
#include <BigNumber.h>
#include <stdio.h>
#include <DS1302.h>
byte SW0 = A4;
byte SW1 = A5;
byte SW2 = 4;
unsigned int tumbal;
unsigned int tumbal2;
int Button;
boolean mboh;
int timer1 = 1;
int delay_now;
int delay_past;
boolean showMode = 0;
char datToString[2];
// Mendifinisakan Fungsi
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
#define Panjang 1
#define Lebar 1
// Deklarasi Fungsi
DMD dmd(Panjang, Lebar); // Panjang x Lebar
namespace {
const int kCePin = A2; // Chip Enable
const int kIoPin = A1; // Input/Output
const int kSclkPin = A0; // Serial Clock
DS1302 rtc(kCePin, kIoPin, kSclkPin);
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Minggu";
case Time::kMonday: return "Senin";
case Time::kTuesday: return "Selasa";
case Time::kWednesday: return "Rabu ";
case Time::kThursday: return "Kamis";
case Time::kFriday: return "Jum'at";
case Time::kSaturday: return "Sabtu";
}
return "(unknown day)";
}
void Show_TwoDigit(int posX, int posY, double value)
{
if (value < 10)
{
dtostrf(value, 1, 0, datToString);
dmd.drawString(posX, posY, "0", 1, GRAPHICS_NORMAL);
dmd.drawString(posX + 7, posY, datToString, 1, GRAPHICS_NORMAL);
}
else
{
dtostrf(value, 2, 0, datToString);
dmd.drawString(posX, posY, datToString, 2, GRAPHICS_NORMAL);
}
}
void ShowTime() //show small time
{
Time t = rtc.time();
int det = t.sec % 2;
if (det != 0)
dmd.drawString(16, 0, ":", 1, GRAPHICS_NORMAL); //blink dot
else
dmd.drawString(16, 0, ":", 1, GRAPHICS_NOR);
Show_TwoDigit (2, 0, t.hr); // hsow hour
Show_TwoDigit (19, 0, t.min); //show minute
}
void drawText( String dispString )
{
char newString[128];
int sLength = dispString.length();
dispString.toCharArray( newString, sLength + 1 );
dmd.drawMarquee( newString , sLength , 31 , 8);
long timer = millis();
boolean ret = false;
dmd.clearScreen(true);
Time t = rtc.time();
while ( !ret ) {
if ( ( timer + 40 ) < millis() )
{
Time t = rtc.time();
ShowTime();
ret = dmd.stepSplitMarquee(8, 15);
timer = millis();
}
}
dispString = "";
showMode = false;
delay_past = millis();
dmd.clearScreen(true);
}
void TwoDigit(int digit, String &hasilDigit)
{
hasilDigit = "";
if (digit < 10)
{
hasilDigit += "0";
hasilDigit += digit;
}
else
{
hasilDigit += digit;
}
}
void scrollDate()
{
String text2, textSmntara ;
Time t = rtc.time();
String twodigit;
textSmntara = "<-MOH.SJAHRI <-KAMLAHWATI <-KURNIAWAN KAMARI <-INDRA WAHYUDI <-AMAR ANSHORI "; ///u can add text here
text2 += textSmntara;
if (dayAsString(t.day) == "Minggu") text2 += "Minggu";
else if (dayAsString(t.day) == "Senin") text2 += "Senin";
else if (dayAsString(t.day) == "Selasa") text2 += "Selasa";
else if (dayAsString(t.day) == "Rabu ") text2 += "Rabu";
else if (dayAsString(t.day) == "Kamis") text2 += "Kamis";
else if (dayAsString(t.day) == "Jum'at") text2 += "Jum'at";
else if (dayAsString(t.day) == "Saptu") text2 += "Saptu";
else text2 += "Kosong";
Serial.print("gulung");
Serial.println(dayAsString(t. day));
text2 += "-";
//Time c = rtc.time();
TwoDigit(t.date, twodigit);
text2 += twodigit;
text2 += "-";
// Time c = rtc.time();
TwoDigit(t.mon, twodigit);
text2 += twodigit;
text2 += "-";
// Time c = rtc.time();
dtostrf(t.yr, 4, 0, datToString);
text2 += datToString;
drawText (text2);
}
void ShowTimeBig()//show big time
{
String bigDigit = "";
dmd.selectFont(BigNumber);
Time t = rtc.time();
int j;
int det = t.sec % 2;
if (det != 0) {
dmd.drawCircle(16, 5, 1, GRAPHICS_NORMAL); //blink clock dot
dmd.drawCircle(16, 10, 1, GRAPHICS_NORMAL);
}
else
{ dmd.drawCircle(16, 5, 1, GRAPHICS_NOR);
dmd.drawCircle(16, 10, 1, GRAPHICS_NOR);
}
j = t.hr;
TwoDigit(j, bigDigit);
bigDigit.toCharArray( datToString, 3);
dmd.drawString(1, 0, datToString, 2, GRAPHICS_NORMAL); //show big hour
j = t.min;
TwoDigit(j, bigDigit);
bigDigit.toCharArray( datToString, 3);
dmd.drawString(19, 0, datToString, 2, GRAPHICS_NORMAL); //show big minute
}
double moreLess24(double value) //make sure a value is between 0 and 24
{
while (value > 24 || value < 0)
{
if (value > 24)
value -= 24;
else if (value < 0)
value += 24;
}
return value;
}
void doubleToHrMin(double number, int &hours, int &minutes)//convert the double number to Hours and Minutes
{
hours = floor(moreLess24(number));
minutes = floor(moreLess24(number - hours) * 60);
}
byte decToBcd(byte val) {
return ((val / 10 * 16) + (val % 10));
}
void ReadButton()
{
if ((digitalRead(SW1) == 0) && (digitalRead(SW2) == 0))
{ Button = 4;
//delay(50);
Serial.println("tombol 4");
}
else if (digitalRead(SW0) == 0)
{ Button = 1; // minus button
//delay(50);
Serial.println("tombol 1");
}
else if (digitalRead(SW1) == 0)
{ Button = 2; // plus button
//delay(50);
Serial.println("tombol 2");
}
else if (digitalRead(SW2) == 0)
{ Button = 3; // menu button
//delay(50);
Serial.println("tombol 3");
}
else Button = 0; Serial.println("tombol 0");
}
void softReset()
{
asm volatile (" jmp 0");
}
void seting()
{
char modesetup[6][9] = {"Jam ", "Mnt ", "Tgl ", "Bln ", "Thn ", "Hri "}; //Set Waktu Dan Tanggal
Time t = rtc.time();
byte sminutes = t.min;
byte shours = t.hr;
byte tgl = t.day;
byte bln = t.mon;
byte th = t.yr - 2000;
byte hr;
Serial.println(sminutes);
Serial.println(shours);
Serial.println(tgl);
Serial.println(bln);
Serial.println(th);
if (dayAsString(t.day) == "Minggu") hr = 1;
else if (dayAsString(t.day) == "Senin") hr = 2;
else if (dayAsString(t.day) == "Selasa") hr = 3;
else if (dayAsString(t.day) == "Rabu ") hr = 4;
else if (dayAsString(t.day) == "Kamis") hr = 5;
else if (dayAsString(t.day) == "Jum'at") hr = 6;
else if (dayAsString(t.day) == "Saptu") hr = 7;
else hr = 15;
Serial.print(hr);
Serial.print("-");
Serial.println(dayAsString(t. day));
dmd.clearScreen( true );
while ((tumbal != 0) || (Button == 2) || (Button == 3)) {
delay(500);
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
ReadButton();
if (tumbal > 6)
{
tumbal = 1;
}
Serial.print("tumbalswitch: ");
Serial.println(tumbal);
Serial.print("tumbal2: ");
Serial.println(tumbal2);
switch (tumbal) {
case 1: { //set jam
if (Button == 2) {
dmd.clearScreen( true );
shours++;
if (shours >= 24) shours = 0;
}
if (Button == 3) {
dmd.clearScreen( true );
if (shours <= 0)shours = 24;
shours--;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
dmd.drawString(0, 8, " ", 5, GRAPHICS_NORMAL);
Show_TwoDigit (0, 8, shours);
Serial.print("jambutton: ");
Serial.println(Button);
}
break;
case 2: { //set menit
if (Button == 2) {
dmd.clearScreen( true );
sminutes++;
if (sminutes >= 60) sminutes = 0;
}
if (Button == 3) {
dmd.clearScreen( true );
sminutes--;
if (sminutes <= 0) sminutes = 59;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
Show_TwoDigit (0, 8, sminutes);
Serial.print("menitbutton: ");
Serial.println(Button);
}
break;
case 3: { //tanggal
if (Button == 2) {
dmd.clearScreen( true );
tgl++;
if (tgl >= 31) tgl = 1;
}
if (Button == 3) {
dmd.clearScreen( true );
tgl--;
if (tgl == 0) tgl = 31;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
Show_TwoDigit (0, 8, tgl);
Serial.print("tanggalbutton: ");
Serial.println(Button);
}
break;
case 4: { //set bulan
if (Button == 2) {
dmd.clearScreen( true );
bln++;
if (bln >= 12) bln = 1;
}
if (Button == 3) {
dmd.clearScreen( true );
bln--;
if (bln == 0)bln = 12;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
Show_TwoDigit (0, 8, bln);
Serial.print("bulanbutton: ");
Serial.println(Button);
}
break;
case 5: { //set tahun
if (Button == 2) {
dmd.clearScreen( true );
th++;
if (th > 99) th = 0;
}
if (Button == 3) {
dmd.clearScreen( true );
th--;
if (th < 0) th = 99;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
Show_TwoDigit (0, 8, th);
Serial.print("tahunbutton: ");
Serial.println(Button);
}
break;
case 6: { //set hari
if (Button == 2) {
dmd.clearScreen( true );
hr++;
if (hr >= 8) hr = 1;
}
if (Button == 3) {
dmd.clearScreen( true );
hr--;
if (hr <= 0) hr = 7;
}
dmd.drawString(0, 0, modesetup[tumbal - 1], 8, GRAPHICS_NORMAL);
if (hr == 1) dmd.drawString(0, 8, "Minggu", 7, GRAPHICS_NORMAL);
else if (hr == 2) dmd.drawString(0, 8, "Senin", 7, GRAPHICS_NORMAL);
else if (hr == 3) dmd.drawString(0, 8, "Selasa", 7, GRAPHICS_NORMAL);
else if (hr == 4) dmd.drawString(0, 8, "Rabu ", 7, GRAPHICS_NORMAL);
else if (hr == 5) dmd.drawString(0, 8, "Kamis", 7, GRAPHICS_NORMAL);
else if (hr == 6) dmd.drawString(0, 8, "Jum'at", 7, GRAPHICS_NORMAL);
else if (hr == 7) dmd.drawString(0, 8, "Saptu", 7, GRAPHICS_NORMAL);
Serial.print("haributton: ");
Serial.println(Button);
}
break;
}
while (Button == 0)
{
ReadButton();
} //wait for key pressed
if (Button == 1)
{
int a;
a++;
if (a > 6)a = 1;
Serial.print("a++; ");
Serial.println(a);
tumbal2 = tumbal = a;
Serial.print("tumbal++; ");
Serial.println(tumbal);
}
delay(100);
if (Button == 4) {
rtc.writeProtect(false);
rtc.halt(false);
th = th + 2000;
Time t(th, bln, tgl, shours, sminutes, 00, hr);
rtc.time(t);
dmd.clearScreen( true );
tumbal = 0;
Serial.print("Button4: ");
Serial.println(tumbal);
delay(500);
softReset();
delay(500);
}
}
}
void setingJam()
{
Serial.println("seting jam");
ReadButton();
delay(250);
while ((Button != 0 ) || (tumbal != 0))
{
Serial.println("readbutton");
ReadButton();
delay(50);
if (Button == 1)
{
tumbal = 1;
}
Serial.println("seting");
seting();
}
}
void printTime() {
dmd.selectFont(System6x7);
if (millis() <= 500)
{
setingJam();
}
delay_now = millis();
if (delay_now - delay_past > 30000) {
delay_past = delay_now;
showMode = true;
}
if (!showMode)ShowTimeBig();
else scrollDate();
}
}//namespace
void ScanDMD() {
dmd.scanDisplayBySPI();
}
void setup() ////////////////////////////// ////////////////////////////// //////////
{
Serial.begin(9600);
pinMode(SW0, INPUT_PULLUP); // Set Time (for this use a slide switch)
pinMode(SW1, INPUT_PULLUP); // Set Date (for this use a slide switch)
pinMode(SW2, INPUT_PULLUP); // N.O. push button switch
Timer1.initialize( 3000 );
Timer1.attachInterrupt( ScanDMD );
dmd.clearScreen( true );
rtc.writeProtect(false);
rtc.halt(false);
}
void loop() ////////////////////////////// ////////////////////////////
{
printTime();
}
////////////////////////////// ////////////////////////////// /////////