Translate

Arduino Timer Count Down Relay + EEPROM

Arduino Timer Count Down Relay + EEPROM


            Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang berfungsi untuk timer count down sebagai waktu untuk kendali on/off relay. fungsi utama dari alat ini yaitu untuk mengatur berapa lama waktu relay ON dan berapa lama waktu relay OFF. alat ini dilengkapi dengan fitur EEPROM sebagai backup data saat power dimatikan sehingga nilai setting masih tersimpan. untuk lebih jelasnya berikut adalah program dan daftar komponennya. 



a. Arduino




b. Relay Modul




c. Lcd + I2C




d. Rotary Encoder






e. Program Arduino IDE

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define outputA 2
#define outputB 3

float timertcm = 1.5;
int timertcmdetik = 5;
int timertcmmenit = 1;
int timertdm = 1;
int timertdmdetik;
int timertdmmenit = 1;
int savetdmdetik;
int savetdmmenit;
int savetcmdetik;
int savetcmmenit;

float counter = 0;
int aState;
int aLastState;
int btrotary = 11;  //pin bt rotary
int x;
int relay = 10;  //pin relay
int tanda;
int cacah;
int waktu;
int bttrigger = 12;  //pin trigger
int y;
int mark;

int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;


void setup() {
  lcd.begin();
  lcd.clear();
  lcd.noCursor();

   pinMode(outputA,INPUT);
   pinMode(outputB,INPUT);
   pinMode(btrotary,INPUT_PULLUP);
   pinMode(bttrigger,INPUT_PULLUP);
   pinMode(relay,OUTPUT);
 
   Serial.begin(9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA); 
savetdmdetik = EEPROM.read(addr1);
savetcmdetik = EEPROM.read(addr2);
savetdmmenit = EEPROM.read(addr3);
savetcmmenit = EEPROM.read(addr4);

}


void loop() {

  lcd.setCursor(0,0);
  lcd.print("TCM:");
  lcd.print(savetcmmenit);
  lcd.print(".");
  lcd.print(savetcmdetik);
  lcd.print(" TDM:");
  lcd.print(savetdmmenit);

  lcd.setCursor(0,1);
  lcd.print("ON");
  lcd.print("00");
  lcd.print(":");
  lcd.print("00");
  lcd.print(" OF");
  lcd.print("00");
  lcd.print(":");
  lcd.print("00");
  lcd.print("   ");
 
y = digitalRead(bttrigger);

if(y == 0){
  timertdmdetik = savetdmdetik;
  timertcmdetik = savetcmdetik;
  timertdmmenit = savetdmmenit;
  timertcmmenit = savetcmmenit;

  mulaitdm();
  mulaitcm();
  digitalWrite(relay,LOW);
  }


x = digitalRead(btrotary);

if((x == 1)&&(mark = 1)){
  delay(200);
  digitalWrite(relay,LOW);
  mark = 0;
  }
 
if((x == 0)&&(mark == 0)){
  delay(200);
  digitalWrite(relay,HIGH);
  tanda = 1;
  cacah++;
  mark = 1;
  }
 
if(tanda == 1){
  waktu++;
  }

if(waktu > 10){
 tanda = 0;
 cacah = 0;
 waktu = 0;
 tanda = 0;


if((waktu <= 10)&&(cacah >= 3)){
lcd.clear();
delay(2000);
setting1();
setting2();
savetdmdetik = timertdmdetik;
savetcmdetik = timertcmdetik;
savetdmmenit = timertdmmenit;
savetcmmenit = timertcmmenit;

EEPROM.write(addr1, savetdmdetik);
EEPROM.write(addr2, savetcmdetik);
EEPROM.write(addr3, savetdmmenit);
EEPROM.write(addr4, savetcmmenit);

x = 1;
cacah = 0;
}
   
}

void setting1(){

digitalWrite(relay,LOW);

x = digitalRead(btrotary);

aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){   
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) {
       timertcmdetik = timertcmdetik + 1;
     } else {
       timertcmdetik = timertcmdetik - 1;
     }

     if(timertcmdetik > 59){
      timertcmdetik = 0;
      timertcmmenit++;
      }

      if(timertcmdetik <= 0){
      timertcmmenit--;
      timertcmdetik = 59;
      }

      if(timertcmmenit <= 0){
      timertcmmenit = 0;
      }
 
   lcd.setCursor(0,0);
   lcd.print("SETTING TCM");
   lcd.setCursor(0,1);
   lcd.print(timertcmmenit);
   lcd.print(".");
   lcd.print(timertcmdetik);
   lcd.print("   ");
 
 } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 
if(x == 0){
  lcd.clear();
  delay(2000);
  return;
  }

setting1(); 
}

void setting2(){
 
x = digitalRead(btrotary);

aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){   
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) {
       timertdmmenit = timertdmmenit + 1;
     } else {
       timertdmmenit = timertdmmenit - 1;
     }
   
   lcd.setCursor(0,0);
   lcd.print("SETTING TDM");
   lcd.setCursor(0,1);
   lcd.print(timertdmmenit);
   lcd.print("        ");
 
 } 
   aLastState = aState; // Updates the previous state of the outputA with the current state

if(timertdmmenit < 1){
  timertdmmenit = 1;
}
 
if(x == 0){
  lcd.clear();
  delay(2000);
  return;
  }

setting2(); 
}

void mulaitdm(){

y = digitalRead(bttrigger);

if(y == 1){
  return;
  }
 
  digitalWrite(relay,LOW);
 
  lcd.setCursor(0,0);
  lcd.print("TCM:");
  lcd.print(savetcmmenit);
  lcd.print(".");
  lcd.print(savetcmdetik);
  lcd.print(" TDM:");
  lcd.print(savetdmmenit);

  lcd.setCursor(0,1);
  lcd.print("ON");
  lcd.print(timertcmmenit);
  lcd.print(":");
  lcd.print(timertcmdetik);
  lcd.print(" OF");
  lcd.print(timertdmmenit);
  lcd.print(":");
  lcd.print(timertdmdetik);
  lcd.print("   ");
 
  timertdmdetik --;
  delay(1000); 

if(timertdmdetik < 0){
  timertdmdetik = 59;
  timertdmmenit--;
  }

if(timertdmmenit < 0){
  timertdmmenit = 0;
  } 

if((timertdmdetik == 0)&&(timertdmmenit == 0)) 
{
  return;
}

 
mulaitdm(); 
}

void mulaitcm(){

  y = digitalRead(bttrigger);

if(y == 1){
  return;
  }
 
  digitalWrite(relay,HIGH);
 
  lcd.setCursor(0,0);
  lcd.print("TCM:");
  lcd.print(savetcmmenit);
  lcd.print(".");
  lcd.print(savetcmdetik);
  lcd.print(" TDM:");
  lcd.print(savetdmmenit);

  lcd.setCursor(0,1);
  lcd.print("ON");
  lcd.print(timertcmmenit);
  lcd.print(":");
  lcd.print(timertcmdetik);
  lcd.print(" OF");
  lcd.print(timertdmmenit);
  lcd.print(":");
  lcd.print(timertdmdetik);
  lcd.print("   ");
 
  timertcmdetik --;
  delay(1000); 

if(timertcmmenit < 0){
  timertcmmenit = 0;
  }
 
 
if(timertcmdetik < 0){
  timertcmdetik = 59;
  timertcmmenit--;
  }

if((timertcmdetik == 0)&&(timertcmmenit == 0)) 
{
  return;
}

 
mulaitcm(); 
}





f. VIDEO HASILNYA








1 comment:

  1. Bang.. mau tanya kalau diubah TCMnya ke JAM bisa juga kah?

    ReplyDelete