4.3 Pemrograman
// seven segment common anoda
// Mikrokontroler ATmega328
// RTC DS1307
// Seven Segment Common Anoda
// Beberapa tombol
// Resistor 560 Ohm
// Resistor 1k
// Resistor 10k
// Capacitor 22pF
// Capacitor 100nF
// Crystal 16MHz
// Buzzer 5V
#include <Wire.h>
#include "RTClib.h"
#include <EEPROM.h>
RTC_DS1307 RTC;
int pin=17;
int relay=1;
int ap=16;
int hours;
int hh;
int mm;
int ss;
int timecc;
int buzer = 0;
int segmentA = 2;
int segmentB = 3;
int segmentC = 4;
int segmentD = 5;
int segmentE = 6;
int segmentF = 7;
int segmentG = 8;
//int ampm = 9;
int digitOne = 10;
int digitTwo = 11;
int digitThree = 12;
int digitFour = 13;
int hoursButton = A0;
int minsButton = A1;
int enter = 9;
byte value,value2,value3;
void setup() {
pinMode(buzer,OUTPUT);
pinMode(pin,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(segmentA, OUTPUT);
pinMode(segmentB, OUTPUT);
pinMode(segmentC, OUTPUT);
pinMode(segmentD, OUTPUT);
pinMode(segmentE, OUTPUT);
pinMode(segmentF, OUTPUT);
pinMode(segmentG, OUTPUT);
pinMode(digitOne, OUTPUT);
pinMode(digitTwo, OUTPUT);
pinMode(digitThree, OUTPUT);
pinMode(digitFour, OUTPUT);
pinMode(hoursButton, INPUT_PULLUP);
pinMode(minsButton, INPUT_PULLUP);
pinMode(enter, INPUT_PULLUP);
pinMode(ap,OUTPUT);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Wire.beginTransmission(0X68);
Wire.write(0x07);
Wire.write(0x10);
Wire.endTransmission();
digitalWrite(buzer,HIGH);
delay(1000);
digitalWrite(buzer,LOW);
value = EEPROM.read(0);
value2 = EEPROM.read(1);
if(value>23){
EEPROM.write(0, 0);
}
if(value2>59){
EEPROM.write(1, 0);
}
}
void loop() {
aman:
value = EEPROM.read(0);
value2 = EEPROM.read(1);
value3 = EEPROM.read(2);
DateTime now = RTC.now();
hours = now.hour();
mm = now.minute();
ss = now.second();
if(hours>23){
digitalWrite(pin,HIGH);
}
else digitalWrite(pin,LOW);
hh = hours;
if (hours < 1) {
hh = 00;
}
if ((hours > 24) && (hours < 24)) {
hh = hours - 24;
}
timecc = hh*100+mm;
showNum(timecc);
if (digitalRead(hoursButton) == LOW) {
mati();
delay(200);
hours++;
ss = 00;
if (hours > 23) {
hours = 00;
}
updateRTC();
}
if (digitalRead(minsButton) == LOW) {
mati();
//debounce
delay(200);
mm++;
ss = 00;
if (mm >59) {
mm = 00;
}
updateRTC();
}
if (digitalRead(enter) == LOW){
mati();
delay(200);
if(digitalRead(enter)==LOW){
if(value3==1){
for(int a=0; a<500; a++){
mati();
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,HIGH);
digitalWrite(digitThree, LOW);
digitalWrite(digitFour,LOW);
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,HIGH);
delay(4);
mati();
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree, HIGH);
digitalWrite(digitFour,LOW);
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,HIGH);
digitalWrite(segmentC,HIGH);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
delay(4);
mati();
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree, LOW);
digitalWrite(digitFour,HIGH);
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,HIGH);
digitalWrite(segmentC,HIGH);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
delay(4);
}
EEPROM.write(2, 0);
goto aman;
}
else{
for(int a=0; a<500; a++){
mati();
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree, HIGH);
digitalWrite(digitFour,LOW);
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,HIGH);
delay(4);
mati();
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree, LOW);
digitalWrite(digitFour,HIGH);
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,HIGH);
delay(4);
}
EEPROM.write(2, 1);
goto aman;
}
}
timecc=value*100+value2;
while(digitalRead(enter)==HIGH){
timecc=value*100+value2;
showNum(timecc);
if(digitalRead(hoursButton)==LOW){
mati();
delay(200);
value++;
if(value>23){
value=00;
}
EEPROM.write(0, value);
}
if(digitalRead(minsButton)==LOW){
mati();
delay(200);
value2++;
if(value2>59){
value2=00;
}
EEPROM.write(1, value2);
}
}
mati();
delay(200);
}
if(EEPROM.read(2)==1){
if(value==hours&&value2==mm){
for(int a=0;a<56;a++){
showNum(timecc);
if(a>0&&a<4){
digitalWrite(buzer,HIGH);
}
else if(a>4&&a<8){
digitalWrite(buzer,LOW);
}
else if(a>8&&a<12){
digitalWrite(buzer,HIGH);
}
else if(a>12&&a<16){
digitalWrite(buzer,LOW);
}
else if(a>16&&a<20){
digitalWrite(buzer,HIGH);
}
else if(a>20&&a<24){
digitalWrite(buzer,LOW);
}
else if(a>24&&a<28){
digitalWrite(buzer,HIGH);
}
else if(a>28&&a<56){
digitalWrite(buzer,LOW);
}
}
}
}
}
void showNum(int n) {
mati();
int n1 = (n%10000)/1000;
if (n > 999) {
printNumber(n1,1);
}
delay(4);
mati();
int n2 = (n%1000)/100;
printNumber(n2,2);
delay(4);
mati();
int n3 = (n%100)/10;
printNumber(n3,3);
delay(4);
mati();
int n4 = n%10;
printNumber(n4,4);
delay(3);
}
void printNumber(int n, int d) {
if(d==1) {
digitalWrite(digitOne,HIGH);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree,LOW);
digitalWrite(digitFour,LOW);
if ((hours == 12) || (hours == 22) || (hours == 23)) {
}
else {
}
}
if(d==2) {
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,HIGH);
digitalWrite(digitThree, LOW);
digitalWrite(digitFour,LOW);
if ((hours > 12) && (hours < 22)) {
}
else {
}
}
if(d==3) {
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree,HIGH);
digitalWrite(digitFour,LOW);
}
if(d==4) {
digitalWrite(digitOne,LOW);
digitalWrite(digitTwo,LOW);
digitalWrite(digitThree,LOW);
digitalWrite(digitFour,HIGH);
}
switch(n) {
case 0:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,HIGH);
break;
case 1:
digitalWrite(segmentA,HIGH);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,HIGH);
digitalWrite(segmentG,HIGH);
break;
case 2:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,HIGH);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,HIGH);
digitalWrite(segmentG,LOW);
break;
case 3:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,HIGH);
digitalWrite(segmentG,LOW);
break;
case 4:
digitalWrite(segmentA,HIGH);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
break;
case 5:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,HIGH);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
break;
case 6:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,HIGH);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
break;
case 7:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,HIGH);
digitalWrite(segmentG,HIGH);
break;
case 8:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,LOW);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
break;
case 9:
digitalWrite(segmentA,LOW);
digitalWrite(segmentB,LOW);
digitalWrite(segmentC,LOW);
digitalWrite(segmentD,LOW);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,LOW);
digitalWrite(segmentG,LOW);
break;
}
}
byte decToBcd(byte val) {
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void updateRTC() {
Wire.beginTransmission(104);
Wire.write(0);
Wire.write(decToBcd(ss));
Wire.write(decToBcd(mm));
Wire.write(decToBcd(hours));
Wire.endTransmission();
}
void mati(){
digitalWrite(segmentA,HIGH);
digitalWrite(segmentB,HIGH);
digitalWrite(segmentC,HIGH);
digitalWrite(segmentD,HIGH);
digitalWrite(segmentE,HIGH);
digitalWrite(segmentF,HIGH);
digitalWrite(segmentG,HIGH);
}