Tuesday, July 31, 2018
Programmable Timer 8 Channel dengan Controller Arduino Uno
Programmable Timer 8 Channel dengan Controller Arduino Uno
Pada Tutorial kali ini kita akan membuat sebuah project pengendalian beban/relay dengan methode Timer, yaitu setiap relay dapat kita program jam berapa harus ON dan jam berapa harus OFF.Setiap relay memiliki 1 settingan timer sendiri sehingga mempunyai waktu masing-masing kapan harus ON dan OFF.
Disini kita menggunakan memory EEPROM internal atmega328 (Arduino Uno) untuk menyimpan data settingan timer sehingga kita akan kehilangan settingan saat Power mati (mati listrik) atau saat Arduino Restart/reset.
A.Bahan dan Module yang dibutuhkan
- 1Unit Relay Module 5V 8 Channel
- 1Unit RTC DS3231
- 1Unit Arduino Uno/Nano/Pro mini/Mega
- 1Unit LCD 1602 dengan i2C connection
- 4Unit Tombol Tactile Push Button (momentary switch)
B.Wiring
Perhatikan Gambar pada header artikel ini.
C.Library
- Library EEPROM (sudah include dalam sistem Arduino IDE)
- Library Wire.h (sudah include dalam sistem Arduino IDE)
- Library RTC DS3231 silahkan download disini.
- Library LiquidCrystal_I2C silahkan download disini.
- Library Push Button silahkan download disini.
Setelah anda mendownload semua library yang dibutuhkan dalam project ini,silahkan extract terlebih dulu dari .rar/.zip file nya kemudian copy semua folder master library tersebut (misal : folder button-master) ke dalam folder "libraries" yang ada di folder installer Arduino IDE.
BACA JUGA ARTIKEL MENARIK LAINNYA :
- Cara Menggunakan SMS Untuk Control LED / RELAY VIA MODULE SIM800L
- CAra Membuat System Aplikasi Bank ATM Berupa Source COde Gratis Berbasis Desktop
- Cara Upload Program Arduino Via Android Menggunakan Bluino Loader dari Codepolitan
D.Sourch Code
Berikut Sourch Code Untuk Tutorial kali ini.Silahkan copy paste code berikut ke dalam Arduino IDE anda kemudian Upload ke Arduino Boar.
CODING HERE
// .-~~~~~~~~~-._ _.-~~~~~~~~~-.// __. Create by. ~. .~ www. `.__// .// DANI RJC ./ belajarduino `.// .// 2016 | .com `.// .// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. `.// .//.-" `-. | .- "-.`.// .//______.============-.. | / ..-============.______`.//.______________________________|/______________________________`.#include <DS3231.h> //Memanggil RTC3231 Library#include <Wire.h> // i2C Conection Library#include <LiquidCrystal_I2C.h> //Memanggil i2C LCD Library#include <Button.h> //Memanggil library Push Button#include <EEPROM.h>#define ON 0 //Ganti 0 jadi 1 saat pakai High Active Trigger#define OFF 1 //Ganti 1 jadi 0 saat pakai High Active Trigger//Setup Tombol Setting#define DN_PIN A0 //Decrease Button#define UP_PIN A1 //Increase Button#define SET_PIN A2 //Setup Clock Button#define ALR_PIN A3 //Setup Timer 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 buttonsButton btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS);Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS);Button btnSET(SET_PIN, PULLUP, INVERT, DEBOUNCE_MS);Button btnALR(ALR_PIN, PULLUP, INVERT, DEBOUNCE_MS);enum {WAIT, INCR, DECR};uint8_t STATE;int count;int lastCount = -1;unsigned long rpt = REPEAT_FIRST;LiquidCrystal_I2C lcd(0x3F, 16, 2);//custom degrees charuint8_t dgrsChar[8] = {0x4,0xa,0x4,0x0,0x0,0x0,0x0,0x0};// Init DS3231DS3231 rtc(SDA, SCL);// Init a Time-data structureTime t; //pencacah string time()uint8_t hh = 0, mm = 0, ss = 0, dd = 0, bb = 0;int yy = 0;String Day = " ";uint8_t StartHH = 21, StartMM = 43, FinishHH = 21, FinishMM = 44, setMode = 0, setAlarm = 0, alarmMode=0;uint8_t StartHH2 = 21, StartMM2 = 44, FinishHH2 = 21, FinishMM2 = 45;uint8_t StartHH3 = 21, StartMM3 = 45, FinishHH3 = 21, FinishMM3 = 46;uint8_t StartHH4 = 21, StartMM4 = 46, FinishHH4 = 21, FinishMM4 = 47;uint8_t StartHH5 = 21, StartMM5 = 47, FinishHH5 = 21, FinishMM5 = 48;uint8_t StartHH6 = 21, StartMM6 = 48, FinishHH6 = 21, FinishMM6 = 49;uint8_t StartHH7 = 21, StartMM7 = 49, FinishHH7 = 21, FinishMM7 = 50;uint8_t StartHH8 = 21, StartMM8 = 50, FinishHH8 = 21, FinishMM8 = 51;//Eeprom Store Variableuint8_t HH,HH2,HH3,HH4,HH5,HH6,HH7,HH8;uint8_t MM,MM2,MM3,MM4,MM5,MM6,MM7,MM8;void setup(){// Setup Serial connection//Serial.begin(115200);pinMode(2,OUTPUT);digitalWrite (2,OFF);pinMode(3,OUTPUT);digitalWrite (3,OFF);pinMode(4,OUTPUT);digitalWrite (4,OFF);pinMode(5,OUTPUT);digitalWrite (5,OFF);pinMode(6,OUTPUT);digitalWrite (6,OFF);pinMode(7,OUTPUT);digitalWrite (7,OFF);pinMode(8,OUTPUT);digitalWrite (8,OFF);pinMode(9,OUTPUT);digitalWrite (9,OFF);//digitalWrite (RelayPin, OFF); // Uncomment untuk mematikan Relay saat Start awallcd.begin(); // memulai koneksi i2c dengan LCDrtc.begin(); // memulai koneksi i2c dengan RTClcd.createChar(0, dgrsChar); //Membuat custom char derajatlcd.setCursor (0,0);lcd.print(F(" Danis Timer "));lcd.setCursor (0,1);lcd.print(F(" 8Channel Relay "));delay (2000);lcd.clear();ReadEeprom();//Set RTC Untuk Pertama kali//rtc.setTime (10, 15, 00);//rtc.setDate(23, 8, 2016);}void loop(){t = rtc.getTime();Day = rtc.getDOWStr(1);if (setMode == 0){hh = t.hour,DEC;mm = t.min,DEC;ss = t.sec,DEC;dd = t.date,DEC;bb = t.mon,DEC;yy = t.year,DEC;}if (setAlarm <2){lcd.setCursor(1,0);if(setMode==0)lcd.print(Day); else {lcd.print(F("SET"));}lcd.print(F(" "));if (dd<10){ lcd.print(F("0"));}lcd.print(dd); lcd.print(F("-"));if (bb<10){ lcd.print(F("0"));}lcd.print(bb); lcd.print(F("-"));lcd.print(yy); lcd.print(F(" "));lcd.setCursor(0,1);if (hh<10){ lcd.print(F("0"));}lcd.print(hh); lcd.print(F(":"));if (mm<10){ lcd.print(F("0"));}lcd.print(mm); lcd.print(F(":"));if (ss<10){ lcd.print(F("0"));}lcd.print(ss); lcd.print(F(" "));lcd.print(rtc.getTemp());lcd.print((char)0);lcd.print(F("C"));lcd.print(F(" "));}blinking();setupClock();setTimer();Timer1 (StartHH, StartMM, FinishHH, FinishMM);Timer2 (StartHH2, StartMM2, FinishHH2, FinishMM2);Timer3 (StartHH3, StartMM3, FinishHH3, FinishMM3);Timer4 (StartHH4, StartMM4, FinishHH4, FinishMM4);Timer5 (StartHH5, StartMM5, FinishHH5, FinishMM5);Timer6 (StartHH6, StartMM6, FinishHH6, FinishMM6);Timer7 (StartHH7, StartMM7, FinishHH7, FinishMM7);Timer8 (StartHH8, StartMM8, FinishHH8, FinishMM8);}void blinking (){//BLINKING SCREENif (setAlarm <2 && setMode != 0) {delay (100);}if (setAlarm <2 && setMode == 1){lcd.setCursor(5,0); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 2 && bb>9){lcd.setCursor(8,0); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 2 && bb<=9){lcd.setCursor(8,0); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 3 && bb>9){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 3 && bb<=9){lcd.setCursor(10,0); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 4){lcd.setCursor(0,1); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 5){lcd.setCursor(3,1); lcd.print(F(" ")); delay(100);}if (setAlarm <2 && setMode == 6){lcd.setCursor(6,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm != 0 && setAlarm != 1) {delay (100);}//Relay1if (setMode == 0 && setAlarm == 2){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 3){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 4){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 5){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay2if (setMode == 0 && setAlarm == 6){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 7){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 8){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 9){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay3if (setMode == 0 && setAlarm == 10){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 11){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 12){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 13){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay4if (setMode == 0 && setAlarm == 14){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 15){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 16){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 17){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay5if (setMode == 0 && setAlarm == 18){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 19){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 20){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 21){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay6if (setMode == 0 && setAlarm == 22){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 23){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 24){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 25){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay7if (setMode == 0 && setAlarm == 26){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 27){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 28){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 29){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}//Relay8if (setMode == 0 && setAlarm == 30){lcd.setCursor(11,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 31){lcd.setCursor(14,0); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 32){lcd.setCursor(11,1); lcd.print(F(" ")); delay(100);}if (setMode == 0 && setAlarm == 33){lcd.setCursor(14,1); lcd.print(F(" ")); delay(100);}}//Seting Jam ,Tanggal,Alarm/Timervoid setupClock (void) {btnUP.read();btnDN.read();btnSET.read();btnALR.read();if (setMode == 7){lcd.setCursor (0,0);lcd.print (F("Set Date Finish "));lcd.setCursor (0,1);lcd.print (F("Set Time Finish "));delay (1000);rtc.setTime (hh, mm, ss);rtc.setDate (dd, bb, yy);lcd.clear();setMode = 0;}if (setAlarm == 34){lcd.setCursor (0,0);lcd.print (F("Set Timer Finish"));lcd.setCursor (0,1);lcd.print (F("-EEPROM Updated-"));WriteEeprom();delay (2000); lcd.clear();setAlarm=0;alarmMode=1;}if (setAlarm >0){ alarmMode=0;}switch (STATE) {case WAIT:if (btnSET.wasPressed()){ setMode = setMode+1;}if (btnALR.wasPressed()){ setAlarm = setAlarm+1;}if (btnUP.wasPressed())STATE = INCR;else if (btnDN.wasPressed())STATE = DECR;else if (btnUP.wasReleased())rpt = REPEAT_FIRST;else if (btnDN.wasReleased())rpt = REPEAT_FIRST;else if (btnUP.pressedFor(rpt)) {rpt += REPEAT_INCR;STATE = INCR;}else if (btnDN.pressedFor(rpt)) {rpt += REPEAT_INCR;STATE = DECR;}break;case INCR:if (setAlarm<2 && setMode==1 && dd<31)dd=dd+1;if (setAlarm<2 && setMode==2 && bb<12)bb=bb+1;if (setAlarm<2 && setMode==3 && yy<2050)yy=yy+1;if (setAlarm<2 && setMode==4 && hh<23)hh=hh+1;if (setAlarm<2 && setMode==5 && mm<59)mm=mm+1;if (setAlarm<2 && setMode==6 && ss<59)ss=ss+1;//Relay1if (setMode==0 && setAlarm==2 && StartHH<23)StartHH=StartHH+1;if (setMode==0 && setAlarm==3 && StartMM<59)StartMM=StartMM+1;if (setMode==0 && setAlarm==4 && FinishHH<23)FinishHH=FinishHH+1;if (setMode==0 && setAlarm==5 && FinishMM<59)FinishMM=FinishMM+1;//Relay2if (setMode==0 && setAlarm==6 && StartHH2<23)StartHH2=StartHH2+1;if (setMode==0 && setAlarm==7 && StartMM2<59)StartMM2=StartMM2+1;if (setMode==0 && setAlarm==8 && FinishHH2<23)FinishHH2=FinishHH2+1;if (setMode==0 && setAlarm==9 && FinishMM2<59)FinishMM2=FinishMM2+1;//Relay3if (setMode==0 && setAlarm==10 && StartHH3<23)StartHH3=StartHH3+1;if (setMode==0 && setAlarm==11 && StartMM3<59)StartMM3=StartMM3+1;if (setMode==0 && setAlarm==12 && FinishHH3<23)FinishHH3=FinishHH3+1;if (setMode==0 && setAlarm==13 && FinishMM3<59)FinishMM3=FinishMM3+1;//Relay4if (setMode==0 && setAlarm==14 && StartHH4<23)StartHH4=StartHH4+1;if (setMode==0 && setAlarm==15 && StartMM4<59)StartMM4=StartMM4+1;if (setMode==0 && setAlarm==16 && FinishHH4<23)FinishHH4=FinishHH4+1;if (setMode==0 && setAlarm==17 && FinishMM4<59)FinishMM4=FinishMM4+1;//Relay5if (setMode==0 && setAlarm==18 && StartHH5<23)StartHH5=StartHH5+1;if (setMode==0 && setAlarm==19 && StartMM5<59)StartMM5=StartMM5+1;if (setMode==0 && setAlarm==20 && FinishHH5<23)FinishHH5=FinishHH5+1;if (setMode==0 && setAlarm==21 && FinishMM5<59)FinishMM5=FinishMM5+1;//Relay6if (setMode==0 && setAlarm==22 && StartHH6<23)StartHH6=StartHH6+1;if (setMode==0 && setAlarm==23 && StartMM6<59)StartMM6=StartMM6+1;if (setMode==0 && setAlarm==24 && FinishHH6<23)FinishHH6=FinishHH6+1;if (setMode==0 && setAlarm==25 && FinishMM6<59)FinishMM6=FinishMM6+1;//Relay7if (setMode==0 && setAlarm==26 && StartHH7<23)StartHH7=StartHH7+1;if (setMode==0 && setAlarm==27 && StartMM7<59)StartMM7=StartMM7+1;if (setMode==0 && setAlarm==28 && FinishHH7<23)FinishHH7=FinishHH7+1;if (setMode==0 && setAlarm==29 && Finivisit link download