#include "LedControl.
h" //Memanggil library Led Control
#include <Button.h> //Memanggil library Push Button
//wiring Module Display 8digit 7segment ke Arduino (Uno)
// Arduino Pin D12 to DIN,
// Arduino Pin D11 to CLK,
// Arduino Pin D10 to CS,
// No.of devices is 1 (Display Urutan ke 1)
//Setup Tombol Setting
#define DN_PIN 2 //Decrease Button
#define UP_PIN 3 //Increase Button
#define SET_PIN 4 //Setup Button
#define PULLUP true //Mengaktifkan internal Pull Up
#define INVERT true
#define DEBOUNCE_MS 20
#define REPEAT_FIRST 500
#define REPEAT_INCR 100
//Declare push buttons
Button btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS);
Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS);
Button btnSET(SET_PIN, PULLUP, INVERT, DEBOUNCE_MS);
//////////////////////////////////////////
// Example by Dani Rajacell //
// Mohon tetap mencantumkan sumber //
//apabila menggunakan example kode ini //
//////////////////////////////////////////
LedControl lc=LedControl(12,11,10,1); // DIN,CLK,CS,No.
enum {WAIT, INCR, DECR}; //The possible states for the state machine
uint8_t STATE; //The current state machine state
int count; //The number that is adjusted
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long pre
//Variable penyimpan logika setup
uint8_t setMode = 8;
uint8_t setRun = 0;
//Pengambilan waktu dari compiller
uint32_t targetTime = 0;
uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
//Mengambil waktu jam dari waktu Compiler Arduino IDE
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6);
uint8_t hh1 = hh%10, mm1 = mm%10, ss1 = ss%10; // mengambil digit kedua
uint8_t hh2 = (hh%100)/10, mm2 = (mm%100)/10, ss2 = (ss%100)/10; // mengambil digit pertama
void setup()
{
//Serial.begin (115200);
// Initialize the MAX7219 device
// Kondisi default Max7219 saat power ON adalah Standby jadi harus kita aktifkan dulu...
lc.shutdown(0,false); // Enable display
lc.setIntensity(0,12); // Set brightness level (Level Kecerahan : 0 sampai 15)
writeRJC();
lc.clearDisplay(0); // Clear display register (Mengkosongkan tampilan segment)
}
//////////////////////////////////////////
// Example by Dani Rajacell //
// Mohon tetap mencantumkan sumber //
//apabila menggunakan example kode ini //
//////////////////////////////////////////
void loop(){
if (targetTime < millis()) {
targetTime = millis() + 1000;
ss1++; // Advance second
if (ss1 == 10) {
ss1 = 0;
ss2++;
if (ss2 == 6) {
ss2 = 0;
mm1++; // Advance minute
if (mm1 == 10) {
mm1 = 0;
mm2++;
if (mm2 == 6) {
mm2 = 0;
hh1++; // Advance hour
if (hh1 == 10) {
hh1 = 0;
hh2++;}
if (hh2>=2 && hh1>=4) {
hh2=0;
hh1=0;
}
}
}
}
}
}
lc.setDigit(0,7,hh2,false);
lc.setDigit(0,6,hh1,false);
lc.setChar(0,5,'-',false);
lc.setDigit(0,4,mm2,false);
lc.setDigit(0,3,mm1,false);
lc.setChar(0,2,'-',false);
lc.setDigit(0,1,ss2,false);
lc.setDigit(0,0,ss1,false);
setupClock ();
}
//////////////////////////////////////////
// Example by Dani Rajacell //
// Mohon tetap mencantumkan sumber //
//apabila menggunakan example kode ini //
//////////////////////////////////////////
void setupClock (void) {
btnUP.read(); //read the buttons
btnDN.read();
btnSET.read();
if (hh2==2 && hh1>3){hh1=0;}
if (setMode==2){setMode=3;}
if (setMode==5){setMode=6;}
if (setMode == 9){writeSetup(); setMode = 0; setRun=1;}
if (setMode == 8 && setRun == 1){writeFinish(); setRun=0;}
lc.setLed(0,setMode,0,true);
switch (STATE) {
case WAIT: //wait for a button event
if (btnSET.wasPressed())
{ setMode = setMode+1; if (setMode == 10)setMode=0;}
if (btnUP.wasPressed())
STATE = INCR;
else if (btnDN.wasPressed())
STATE = DECR;
else if (btnUP.wasReleased()) //reset the long press interval
rpt = REPEAT_FIRST;
else if (btnDN.wasReleased())
rpt = REPEAT_FIRST;
else if (btnUP.pressedFor(rpt)) { //check for long press
rpt += REPEAT_INCR; //increment the long press interval
STATE = INCR;
}
else if (btnDN.pressedFor(rpt)) {
rpt += REPEAT_INCR;
STATE = DECR;
}
break;
case INCR: //increment the counter
if (setMode==1 && ss2<5)ss2=ss2+1;
if (setMode==0 && ss1<9)ss1=ss1+1;
if (setMode==4 && mm2<5)mm2=mm2+1;
if (setMode==3 && mm1<9)mm1=mm1+1;
if (setMode==7 && hh2<2)hh2=hh2+1;
if (setMode==6 && hh1<9 && hh2<2)hh1=hh1+1;
if (setMode==6 && hh1<3 && hh2==2)hh1=hh1+1;
STATE = WAIT;
break;
case DECR: //decrement the counter
if (setMode==1 && ss2>0)ss2=ss2-1;
if (setMode==0 && ss1>0)ss1=ss1-1;
if (setMode==4 && mm2>0)mm2=mm2-1;
if (setMode==3 && mm1>0)mm1=mm1-1;
if (setMode==7 && hh2>0)hh2=hh2-1;
if (setMode==6 && hh1>0)hh1=hh1-1;
STATE = WAIT;
break;
}
}
//////////////////////////////////////////
// Example by Dani Rajacell //
// Mohon tetap mencantumkan sumber //
//apabila menggunakan example kode ini //
//////////////////////////////////////////
void writeSetup() {
lc.setDigit(0,7,5,false);
lc.setChar(0,6,'e',false);
lc.setRow(0,5,B00001111);
lc.setRow(0,4,B00111110);
lc.setChar(0,3,'p',false);
lc.setChar(0,2,'-',false);
lc.setChar(0,1,'-',false);
lc.setChar(0,0,'-',false);
delay(1000);
if(ss1<9)ss1=ss1+1;
lc.clearDisplay(0);
}
void writeFinish() {
lc.setRow(0,7,B01000111);
lc.setRow(0,6,B00110000);
lc.setRow(0,5,B00010101);
lc.setRow(0,4,B00110000);
lc.setDigit(0,3,5,false);
lc.setRow(0,2,B00010111);
lc.setChar(0,1,'-',false);
lc.setChar(0,0,'-',false);
delay(1000);
if(ss1<9)ss1=ss1+1;
lc.clearDisplay(0);
}
void writeRJC() {
lc.setRow(0,7,B11000110);
lc.setRow(0,6,B11111101);
lc.setRow(0,5,B10111000);
lc.setRow(0,4,B11111101);
lc.setRow(0,3,B11001110);
lc.setRow(0,2,B11101111);
lc.setRow(0,1,B10001110);
lc.setRow(0,0,B10001110);
delay(2000);
lc.clearDisplay(0);
}
e it's different when the sketch starts)
the repeats for long presses
IME__ + 6);
ngambil digit pertama
aktifkan dulu...