Translate

Membuat Alat Frekuensi & Duty Cycle Generator dan Timer Arduino

Membuat Alat Frekuensi & Duty Cycle Generator dan Timer Arduino


           Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat menghasilkan frekuensi dan duty cycle yang dapat diatur melalui tombol dan juga timer yang bisa disetting maksimal di 30 menit. untuk frekuensi yaitu berada di range 0 - 100 Hz dan duty cycle 0 - 100 persen. untuk lebih jelasnya berikut adalah koding dan skemanya.


a. Skema




b. Program Arduino IDE

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PWM.h>
#include <EEPROM.h>
#define DS3231_I2C_ADDRESS 0x68

LiquidCrystal_I2C lcd(0x27,16,2);

int relay1 = A0;
int relay2 = A1;
int relay3 = A2;
int buzzer = 8;
int btset = 3;
int btok = 4;
int btup = 5;
int btdown = 6;
int btsetx;
int btokx;
int btupx;
int btdownx;
int32_t frekuensi,frekuensi2;
int dutycycle,dutycycle2;
int waktu;
bool success;
float duty,duty2;

int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int val1;
int val2;
int val3;
int simpan1;
int simpan2;
int simpan3;
int simpan4;
int simpan5;

int outpin1 = 9;
int outpin2 = 10;   

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

void setup() {
Serial.begin(9600);
lcd.begin();  
lcd.clear();
lcd.noCursor();
InitTimersSafe();
success = SetPinFrequencySafe(outpin1, frekuensi);
success = SetPinFrequencySafe(outpin2, frekuensi);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(outpin1,OUTPUT);
pinMode(outpin2,OUTPUT);
pinMode(buzzer,OUTPUT);
digitalWrite(relay1,LOW); //OFF
digitalWrite(relay2,LOW); //OFF
digitalWrite(relay3,LOW); //OFF
digitalWrite(buzzer,LOW); //OFF
pinMode(btset,INPUT_PULLUP);
pinMode(btok,INPUT_PULLUP);
pinMode(btup,INPUT_PULLUP);
pinMode(btdown,INPUT_PULLUP);
  
Wire.begin();
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,0,0,6,5,10,18);

frekuensi = EEPROM.read(addr1);
dutycycle = EEPROM.read(addr2);
waktu = EEPROM.read(addr3);
frekuensi2 = EEPROM.read(addr4);
dutycycle2 = EEPROM.read(addr5);

digitalWrite(buzzer,HIGH); //ON
delay(3000);
digitalWrite(buzzer,LOW); //OFF
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

void loop() {

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
  
lcd.setCursor(0,0);
lcd.print("EMS GENERATOR");
lcd.setCursor(0,1);
lcd.print("SETTING ");

if(btsetx == 0){
lcd.clear();
delay(200);
setfreq1();
setduty1();
setfreq2();
setduty2();
settimer();
setDS3231time(0,0,0,6,5,10,18); 
mulai();  
}

}

void setfreq1(){

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,0);
lcd.print("SET 1 FREQ HZ");
lcd.setCursor(0,1);
lcd.print(frekuensi);  
lcd.print("   ");

if(btupx == 0){
  delay(200);
  frekuensi++;
}

if(btdownx == 0){
  delay(200);
  frekuensi--;
}

if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr1, frekuensi);
  return;
}

if(frekuensi > 100){
frekuensi = 0;  
}

if(frekuensi < 0){
frekuensi = 100;  
}

setfreq1();
}

void setfreq2(){

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,0);
lcd.print("SET 2 FREQ HZ");
lcd.setCursor(0,1);
lcd.print(frekuensi2);  

if(btupx == 0){
  delay(200);
  frekuensi2++;
}

if(btdownx == 0){
  delay(200);
  frekuensi2--;
}

if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr4, frekuensi2);
  return;
}

if(frekuensi2 > 100){
frekuensi2 = 0;  
}

if(frekuensi2 < 0){
frekuensi2 = 100;  
}

setfreq2();
}

void setduty1(){

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,0);
lcd.print("SET 1 DUTY %");
lcd.setCursor(0,1);
lcd.print(dutycycle);  
lcd.print("   ");

if(btupx == 0){
  delay(200);
  dutycycle++;
}

if(btdownx == 0){
  delay(200);
  dutycycle--;
}

if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr2, dutycycle);
  return;
}

if(dutycycle > 100){
dutycycle = 0;  
}

if(dutycycle < 0){
dutycycle = 100;  
}

setduty1();
}

void setduty2(){

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,0);
lcd.print("SET 2 DUTY %");
lcd.setCursor(0,1);
lcd.print(dutycycle2);  
lcd.print("   ");

if(btupx == 0){
  delay(200);
  dutycycle2++;
}

if(btdownx == 0){
  delay(200);
  dutycycle2--;
}

if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr5, dutycycle2);
  return;
}

if(dutycycle2 > 100){
dutycycle2 = 0;  
}

if(dutycycle2 < 0){
dutycycle2 = 100;  
}

setduty2();
}

void settimer(){

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,0);
lcd.print("SET WAKTU");
lcd.setCursor(0,1);
lcd.print(waktu);  
lcd.print(" MENIT ");

if(btupx == 0){
  delay(200);
  waktu++;
}

if(btdownx == 0){
  delay(200);
  waktu--;
}

if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr3, waktu);
  return;
}

if(waktu > 59){
waktu = 0;  
}

if(waktu < 0){
waktu = 59;  
}

settimer();
}

void mulai(){

  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);

  lcd.setCursor(0,0);
  // send it to the serial monitor
  lcd.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
 
//duty = pwmon / 255;
duty = (dutycycle / 100.0) * 255.0;
duty2 = (dutycycle2 / 100.0) * 255.0;

digitalWrite(relay1,HIGH); //ON 
digitalWrite(relay2,HIGH); //ON 
digitalWrite(relay3,HIGH); //ON 

btsetx = digitalRead(btset);
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);
   
lcd.setCursor(0,1); 
lcd.print("F/D:");
lcd.print(frekuensi); 
lcd.print("/");
lcd.print(dutycycle); 
lcd.print("/");
lcd.print(frekuensi2); 
lcd.print("/");
lcd.print(dutycycle2); 

  InitTimersSafe();
  success = SetPinFrequencySafe(outpin1, frekuensi);
  success = SetPinFrequencySafe(outpin2, frekuensi2);
 
 pwmWrite(outpin1, duty);
 pwmWrite(outpin2, duty2);
 delay(1000);

if(minute == waktu){
  digitalWrite(relay1,LOW); //OFF
  digitalWrite(relay2,LOW); //OFF 
  digitalWrite(relay3,LOW); //OFF  
  digitalWrite(buzzer,HIGH); //ON
  delay(3000);
  digitalWrite(buzzer,LOW); //OFF
  pwmWrite(outpin1, 0);
  pwmWrite(outpin2, 0);
  lcd.clear();
  delay(2000);
  return;
}
    
mulai();  
}



c. VIDEO HASILNYA



No comments:

Post a Comment