Translate

Kendali Pagar MANUAL / OTOMATIS via Sensor PIR dan IOT BLYNK 2.0

Kendali Pagar MANUAL / OTOMATIS via Sensor PIR dan IOT BLYNK 2.0


         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mengendalikan sebuah pintu pagar dengan 2 mode yaitu manual dan otomatis. untuk cara manual yaitu dengan menggunakan fitur IOT blynk yang terdapat 2 buah tombol untuk kendalinya sedangkan untuk fitur otomatis yaitu buka tutup pagar dengan menggunakan sensor PIR. untuk lebih jelasnya berikut adalah koding dan skemanya.



a. Skema

Sumber: 
https://projectiot123.com/product/ibt2-bts7960b-bts7960-43a-h-bridge-motor-driver-in-pakistan/




b. Program Arduino IDE

#define BLYNK_TEMPLATE_ID "TMPL6wOC--xxx"
#define BLYNK_TEMPLATE_NAME "monitor daya"
#define BLYNK_AUTH_TOKEN "VOK0cWiFN5ycHj3SV_snEDXTfTfeixxx"
#define BLYNK_PRINT Serial  

#define  BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

//int buzzer = D3;
int sw1 = D7;
int sw2 = D3;
int sw1x = 0;
int sw2x = 0;

int pwm1 = D5;
int pwm2 = D6;
int mark = 0;
int pinValue;
int pinValue2;
int sensorpir;
int tanda;

BlynkTimer timer;

char ssid[] = "hotspothpku";
char pass[] = "12345678";

BLYNK_WRITE(V0)
{
  pinValue = param.asInt();   

  if((pinValue == 1)&&(tanda == 1)){
    lcd.clear();
    bukapagarx();
  }
  
  if((pinValue == 0)&&(tanda == 1)) {
     lcd.clear(); 
    tutuppagarx(); 
  }
  
//  Serial.print("button value is: "); 
//  Serial.println(pinValue);
}

BLYNK_WRITE(V1)
{
  pinValue2 = param.asInt();   

  if(pinValue2 == 1){
    tanda = 1;
  }
  
  if(pinValue2 == 0) {
    tanda = 0;
  }

}

void sendSensor()
 delay(100);
}

void setup() {
  // put your setup code here, to run once:
  lcd.begin();
  lcd.clear();
  lcd.noCursor();

  //pinMode(buzzer,OUTPUT);
  //digitalWrite(buzzer,LOW);
  pinMode(sw1,INPUT_PULLUP);
  pinMode(sw2,INPUT_PULLUP);

  pinMode(pwm1,OUTPUT);
  pinMode(pwm2,OUTPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L, sendSensor);
}

void loop() {

sensorpir = analogRead(A0);
sw1x = digitalRead(sw1);
sw2x = digitalRead(sw2);

  lcd.setCursor(0,0);
  lcd.print("SW1= ");
  lcd.print(sw1x);
  lcd.print("  SW2= ");
  lcd.print(sw2x);

  lcd.setCursor(0,1);
  lcd.print("PIR= ");
  lcd.print(sensorpir);
  lcd.print("  ");
  lcd.print(tanda);
  
//buka pagar
if((sensorpir >= 100)&&(mark == 0)&&(tanda == 0)){
 lcd.clear(); 
 bukapagar(); 
 delay(5000); 
}

//tutup pagar
if((sensorpir < 100)&&(mark == 1)&&(tanda == 0)){
 lcd.clear(); 
 tutuppagar();  
}

  Blynk.run();
  timer.run();
  delay(200);  
}

void bukapagar(){
lcd.setCursor(0,1);
lcd.print("BUKA    ");

analogWrite(pwm1,100);  
analogWrite(pwm2,0);  

sw1x = digitalRead(sw1);

if(sw1x == 1){
analogWrite(pwm1,0);  
analogWrite(pwm2,0);
mark = 1;
lcd.clear(); 
return; 
}
  
bukapagar();  
}

void tutuppagar(){
lcd.setCursor(0,1);
lcd.print("TUTUP  ");
  
analogWrite(pwm1,0);  
analogWrite(pwm2,100);  

sw2x = digitalRead(sw2);

if(sw2x == 1){
analogWrite(pwm1,0);  
analogWrite(pwm2,0);
mark = 0;
//digitalWrite(buzzer,HIGH);
//delay(3000);
//digitalWrite(buzzer,LOW);
lcd.clear(); 
return; 
}
  
tutuppagar();  
}

void bukapagarx(){
lcd.setCursor(0,1);
lcd.print("BUKA    ");

analogWrite(pwm1,100);  
analogWrite(pwm2,0);  

sw1x = digitalRead(sw1);

if(sw1x == 1){
analogWrite(pwm1,0);  
analogWrite(pwm2,0);
lcd.clear(); 
return; 
}
  
bukapagarx();  
}

void tutuppagarx(){
lcd.setCursor(0,1);
lcd.print("TUTUP  ");
  
analogWrite(pwm1,0);  
analogWrite(pwm2,100);  

sw2x = digitalRead(sw2);

if(sw2x == 1){
analogWrite(pwm1,0);  
analogWrite(pwm2,0);
//digitalWrite(buzzer,HIGH);
//delay(3000);
//digitalWrite(buzzer,LOW);
lcd.clear(); 
return; 
}
  
tutuppagarx();  
}



c. Interface IOT BLYNK


d. VIDEO HASILNYA





Simulasi Kendali Motor DC Menggunakan Proteus 7 dan Fitur EEPROM

Simulasi Kendali Motor DC Menggunakan Proteus 7 dan Fitur EEPROM


       Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah simulasi dengan menggunakan Arduino untuk mengendalikan putaran motor DC yang dikendalkan kecepatannya dari pelan hingga cepat kemudian dari cepat ke pelan dan stop. untuk lebih jelasnya berikut adalah skema dan kodingnya.



a. Skema Alat




b. VIDEO HASILNYA




c. Program Arduino IDE versi 1

/*
Tombol 1 pin no - 6
Tombol 2 pin no - 5
Tombol 3 pin no 4
Tombol 4 pin no - 3
Driver Motor 1 RPWM pin no - 11
Driver Motor 1 LapWM pin no - 10
Driver Motor 2 RPWM pin no - 9
Driver Motor 2 LPWM pin no 8
Sensor motor 1 a pin no A0 start
Sensor motor 1 b pin no A1 stop
Sensor motor 2 a pin no A2 start
Sensor motor 2 b pin no A3 stop
LCD SDA pin A4
LCD SCL pin A5
*/

#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 20, 4);

int dataadc1;
int dataadc2;
int dataadc3;
int dataadc4;

int btset = 3;
int btok = 4;
int btup = 6;
int btdown = 5;
int motor1a = 12;
int motor1b = 13;
int ena = 10;

int motor2a = 7;
int motor2b = 8;
int enb = 9;

int key = 2;

int btsetx;
int btokx;
int btupx;
int btdownx;

int vslowstart1;
int vforwardspeed1;
int vreversespeed1;
int vslowstop1;

int vdelaystart2;
int vslowstart2;
int vforwardspeed2;
int vreversespeed2;
int vslowstop2;
int kec1;
int kec2;
int keyx;
int waktu;

int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int addr6 = 5;
int addr7 = 6;
int addr8 = 7;
int addr9 = 8;
int addr10 = 9;

int simvslowstart1;
int simvforwardspeed1;
int simvreversespeed1;
int simvslowstop1;
int simvslowstart2;
int simvforwardspeed2;
int simvreversespeed2;
int simvslowstop2;
int simvdelaystart2;
int simpan10;


void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  pinMode(btset,INPUT_PULLUP);
  pinMode(btok,INPUT_PULLUP);
  pinMode(btup,INPUT_PULLUP);
  pinMode(btdown,INPUT_PULLUP);
  pinMode(key,INPUT_PULLUP);
  pinMode(motor1a,OUTPUT);
  pinMode(motor1b,OUTPUT);
  pinMode(motor2a,OUTPUT);
  pinMode(motor2b,OUTPUT);
  pinMode(ena,OUTPUT);
  pinMode(enb,OUTPUT);
  pinMode(A0,INPUT_PULLUP);
  pinMode(A1,INPUT_PULLUP);
  pinMode(A2,INPUT_PULLUP);
  pinMode(A3,INPUT_PULLUP);
  
  simvslowstart1 = EEPROM.read(addr1);
  simvforwardspeed1 = EEPROM.read(addr2);
  simvreversespeed1 = EEPROM.read(addr3);
  simvslowstop1 = EEPROM.read(addr4);
  simvslowstart2 = EEPROM.read(addr5);
  simvforwardspeed2 = EEPROM.read(addr6);
  simvreversespeed2 = EEPROM.read(addr7);
  simvslowstop2 = EEPROM.read(addr8);
  simvdelaystart2 = EEPROM.read(addr9);

  vslowstart1 = simvslowstart1;
  vforwardspeed1 = simvforwardspeed1;
  vreversespeed1 = simvreversespeed1;
  vslowstop1 = simvslowstop1;
  vslowstart2 = simvslowstart2;
  vforwardspeed2 = simvforwardspeed2;
  vreversespeed2 = simvreversespeed2;    
  vslowstop2 = simvslowstop2; 
  vdelaystart2 = simvdelaystart2; 

}


void loop() {

 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

 lcd.setCursor(0,0);
 lcd.print("PILIH SETTING");
 lcd.setCursor(0,1);
 lcd.print("1. SET MOTOR 1");
 lcd.setCursor(0,2);
 lcd.print("2. SET MOTOR 2");

 if(btupx == 0){
 delay(2000);
 lcd.clear();
 slowstart1();
 forwardspeed1();
 reversespeed1();
 slowstop1(); 
 }

 if(btdownx == 0){
 delay(2000);
 lcd.clear();
 delaystart2();
 slowstart2();
 forwardspeed2();
 reversespeed2();
 slowstop2(); 
 }

 if(btsetx == 0){
 delay(2000);
 lcd.clear();
 waktu = 0;
 mulaistart();
 mulaistop();
 mulaistart2();
 mulaistop2();
 }
 
 delay(200);
}



void slowstart1(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW START 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstart1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstart1();  
}


void forwardspeed1(){
 lcd.setCursor(0,0);
 lcd.print("SET FORWARD SPEED 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vforwardspeed1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

forwardspeed1();  
}


void reversespeed1(){
 lcd.setCursor(0,0);
 lcd.print("SET REVERSE SPEED 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vreversespeed1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

reversespeed1();  
}


void slowstop1(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW STOP 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstop1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstop1();  
}


//=======================================================

void slowstart2(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW START 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstart2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstart2();  
}


void forwardspeed2(){
 lcd.setCursor(0,0);
 lcd.print("SET FORWARD SPEED 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vforwardspeed2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

 if(btokx == 0){
  lcd.clear();
  delay(2000);
  vforwardspeed2 = vforwardspeed2 * 2;
  EEPROM.write(addr6, vforwardspeed2);
  return;
  }   

forwardspeed2();  
}


void reversespeed2(){
 lcd.setCursor(0,0);
 lcd.print("SET REVERSE SPEED 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vreversespeed2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

 if(btokx == 0){
  lcd.clear();
  delay(2000);
  vreversespeed2 = vreversespeed2 * 2;
  EEPROM.write(addr7, vreversespeed2);
  return;
  }   

reversespeed2();  
}


void slowstop2(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW STOP 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstop2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstop2();  
}

void delaystart2(){
 lcd.setCursor(0,0);
 lcd.print("SET DELAY START 2");
 lcd.setCursor(0,1);
 lcd.print("Ms=: ");  
 lcd.print(vdelaystart2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

 if(vdelaystart2 < 0){
  vdelaystart2 = 5000;
  }

 if(vdelaystart2 > 5000){
  vdelaystart2 = 0;
  } 

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

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

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

delaystart2();  
}

//==============================================================================================
void mulaistart(){

 dataadc1 = digitalRead(A0);
 dataadc2 = digitalRead(A1);
 dataadc3 = digitalRead(A2);
 dataadc4 = digitalRead(A3);

 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);
 
 lcd.setCursor(0,0);
 lcd.print("FSPEED1:");
 lcd.print(vforwardspeed1);
 lcd.print("/");  
 lcd.print(vreversespeed1);
 lcd.print(" ");
 
 lcd.setCursor(0,1);
 lcd.print("FSPEED2:");
 lcd.print(vforwardspeed2);
 lcd.print("/");  
 lcd.print(vreversespeed2);
 lcd.print(" ");
 
 lcd.setCursor(0,2);
 lcd.print("T:");
 lcd.print(waktu);
 lcd.print("/");  
 lcd.print(vdelaystart2);
 lcd.print(" "); 
   
waktu++;  
vslowstart1 = vslowstart1 + 1;
kec1 = vslowstart1;

if(vslowstart1 >= vforwardspeed1){
   vslowstart1 = vforwardspeed1; 
   kec1 = vforwardspeed1; 
}

if(vslowstart2 > vforwardspeed2){
   vslowstart2 = vforwardspeed2; 
   kec2 = vforwardspeed2;  
}

  //arah CW
  digitalWrite(motor1a,HIGH);
  digitalWrite(motor1b,LOW);  
  analogWrite(ena,kec1);

if(waktu >= vdelaystart2){
  vslowstart2 = vslowstart2 + 1;
  kec2 = vslowstart2;
  waktu = vdelaystart2;
  
  //arah CW
  digitalWrite(motor2a,HIGH);
  digitalWrite(motor2b,LOW);  
  analogWrite(enb,kec2);
}

if(dataadc2 == 0){
  lcd.clear();
  return;
}

if(dataadc4 == 0){
  lcd.clear();
  return;
}

delay(100);

mulaistart();  
}

void mulaistop(){

 btsetx = digitalRead(btset);
 keyx = digitalRead(key);
  
 lcd.setCursor(0,0);
 lcd.print("SLOW1 :");
 lcd.print(vslowstart1);
 lcd.print("/");  
 lcd.print(vslowstop1);
 lcd.print("   ");

 lcd.setCursor(0,1);
 lcd.print("SLOW2 :");
 lcd.print(vslowstart2);
 lcd.print("/");  
 lcd.print(vslowstop2);
 lcd.print("   ");
 
 if(btokx == 0){
  lcd.clear();
  delay(2000);
  waktu = 0;
  return;
  } 

vforwardspeed1 = vforwardspeed1 - 1;
kec1 = vforwardspeed1;

vforwardspeed2 = vforwardspeed2 - 1; 
kec2 = vforwardspeed2; 

if(vforwardspeed1 <= 0){
kec1 = 0; 
vforwardspeed1 = 0; 
}

if(vforwardspeed2 <= 0){
kec2 = 0;  
vforwardspeed2 = 0;
}

  //arah CW
  digitalWrite(motor1a,HIGH);
  digitalWrite(motor1b,LOW);  
  analogWrite(ena,kec1);
    
  digitalWrite(motor2a,HIGH);
  digitalWrite(motor2b,LOW);  
  analogWrite(enb,kec2);
  
if(keyx == 1){
  lcd.clear();
  delay(1000);
  waktu = 0;
  return;
}

delay(100);
mulaistop();  
}

//====================================================

void mulaistart2(){

 dataadc1 = digitalRead(A0);
 dataadc2 = digitalRead(A1);
 dataadc3 = digitalRead(A2);
 dataadc4 = digitalRead(A3);

 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);
  
 lcd.setCursor(0,0);
 lcd.print("RSPEED :");
 lcd.print(vforwardspeed1);
 lcd.print("/");  
 lcd.print(vreversespeed1);
 lcd.print("   ");

 lcd.setCursor(0,1);
 lcd.print("RSPEED2 :");
 lcd.print(vforwardspeed2);
 lcd.print("/");  
 lcd.print(vreversespeed2);
 lcd.print("/");  
 lcd.print(vdelaystart2);
 lcd.print("   ");
   
waktu++;  
vslowstart1 = vslowstart1 + 1;
kec1 = vslowstart1 + 1;

if(vslowstart1 >= vreversespeed1){
vslowstart1 = vreversespeed1;    
kec1 = vreversespeed1;  
}

if(vslowstart2 >= vreversespeed2){
vslowstart2 = vreversespeed2;   
kec2 = vreversespeed2;  
}
 
  //arah CCW
  digitalWrite(motor1a,LOW);
  digitalWrite(motor1b,HIGH);  
  analogWrite(ena,kec1);

if(waktu >= vdelaystart2){
  vslowstart2 = vslowstart2 + 1;
  kec2 = vslowstart2;
  waktu = vdelaystart2;
  //arah CCW
  digitalWrite(motor2a,LOW);
  digitalWrite(motor2b,HIGH);  
  analogWrite(enb,kec2);
}

if(dataadc1 == 0){
  return;
}

if(dataadc3 == 0){
  return;
}

delay(1);
mulaistart2();  
}


void mulaistop2(){

 btsetx = digitalRead(btset);
 keyx = digitalRead(key);
  
 lcd.setCursor(0,0);
 lcd.print("SLOW1 :");
 lcd.print(vslowstart1);
 lcd.print("/");  
 lcd.print(vslowstop1);
 lcd.print("   ");

 lcd.setCursor(0,2);
 lcd.print("SLOW2 :");
 lcd.print(vslowstart2);
 lcd.print("/");  
 lcd.print(vslowstop2);
 lcd.print("   ");

vreversespeed1 = vreversespeed1 - 1; 
kec1 = vreversespeed1;
vreversespeed2 = vreversespeed2 - 1;
kec2 = vreversespeed2; 

if(vreversespeed1 <= 0){
kec1 = 0;  
vreversespeed1 = 0;
}

if(vreversespeed2 <= 0){
vreversespeed2 = 0;
kec2 = 0;  
}

  //arah CCW
  digitalWrite(motor1a,LOW);
  digitalWrite(motor1b,HIGH);  
  analogWrite(ena,kec1);
  digitalWrite(motor2a,LOW);
  digitalWrite(motor2b,HIGH);  
  analogWrite(enb,kec2);

if(keyx == 1){
  lcd.clear();
  delay(1000);
  waktu = 0;
  return;
}

delay(1);
mulaistop2();  
}



  
d. Program Arduino IDE versi 2

/*
Tombol 1 pin no - 6
Tombol 2 pin no - 5
Tombol 3 pin no 4
Tombol 4 pin no - 3
Driver Motor 1 RPWM pin no - 11
Driver Motor 1 LapWM pin no - 10
Driver Motor 2 RPWM pin no - 9
Driver Motor 2 LPWM pin no 8
Sensor motor 1 a pin no A0 start
Sensor motor 1 b pin no A1 stop
Sensor motor 2 a pin no A2 start
Sensor motor 2 b pin no A3 stop
LCD SDA pin A4
LCD SCL pin A5
*/

#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

int dataadc1;
int dataadc2;
int dataadc3;
int dataadc4;

/*
int btset = 3;
int btok = 4;
int btup = 6;
int btdown = 5;
int motor1a = 12;
int motor1b = 13;
int ena = 10;

int motor2a = 7;
int motor2b = 8;
int enb = 9;
*/

int btset = 3;
int btok = 4;
int btup = 6;
int btdown = 5;
int motor1a = 11;
int motor1b = 10;
int motor2a = 9;
int motor2b = 8;

int key = 2;

int btsetx;
int btokx;
int btupx;
int btdownx;

int vslowstart1;
int vforwardspeed1;
int vreversespeed1;
int vslowstop1;

int vdelaystart2;
int vslowstart2;
int vforwardspeed2;
int vreversespeed2;
int vslowstop2;
int kec1;
int kec2;
int keyx;
int waktu;

int addr1 = 0;
int addr2 = 1;
int addr3 = 2;
int addr4 = 3;
int addr5 = 4;
int addr6 = 5;
int addr7 = 6;
int addr8 = 7;
int addr9 = 8;
int addr10 = 9;

int simvslowstart1;
int simvforwardspeed1;
int simvreversespeed1;
int simvslowstop1;
int simvslowstart2;
int simvforwardspeed2;
int simvreversespeed2;
int simvslowstop2;
int simvdelaystart2;
int simpan10;


void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  pinMode(btset,INPUT_PULLUP);
  pinMode(btok,INPUT_PULLUP);
  pinMode(btup,INPUT_PULLUP);
  pinMode(btdown,INPUT_PULLUP);
  pinMode(key,INPUT_PULLUP);
  pinMode(motor1a,OUTPUT);
  pinMode(motor1b,OUTPUT);
  pinMode(motor2a,OUTPUT);
  pinMode(motor2b,OUTPUT);
  //pinMode(ena,OUTPUT);
  //pinMode(enb,OUTPUT);
  pinMode(A0,INPUT_PULLUP);
  pinMode(A1,INPUT_PULLUP);
  pinMode(A2,INPUT_PULLUP);
  pinMode(A3,INPUT_PULLUP);
  
  simvslowstart1 = EEPROM.read(addr1);
  simvforwardspeed1 = EEPROM.read(addr2);
  simvreversespeed1 = EEPROM.read(addr3);
  simvslowstop1 = EEPROM.read(addr4);
  simvslowstart2 = EEPROM.read(addr5);
  simvforwardspeed2 = EEPROM.read(addr6);
  simvreversespeed2 = EEPROM.read(addr7);
  simvslowstop2 = EEPROM.read(addr8);
  simvdelaystart2 = EEPROM.read(addr9);

  vslowstart1 = simvslowstart1;
  vforwardspeed1 = simvforwardspeed1;
  vreversespeed1 = simvreversespeed1;
  vslowstop1 = simvslowstop1;
  vslowstart2 = simvslowstart2;
  vforwardspeed2 = simvforwardspeed2;
  vreversespeed2 = simvreversespeed2;    
  vslowstop2 = simvslowstop2; 
  vdelaystart2 = simvdelaystart2; 

}


void loop() {

 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);
 keyx = digitalRead(key);
 
 lcd.setCursor(0,0);
 lcd.print("PILIH SETTING");
 lcd.setCursor(0,1);
 lcd.print("1. SET MOTOR 1");
 lcd.setCursor(0,2);
 lcd.print("2. SET MOTOR 2");

 if(btupx == 0){
 delay(2000);
 lcd.clear();
 slowstart1();
 forwardspeed1();
 reversespeed1();
 slowstop1(); 
 }

 if(btdownx == 0){
 delay(2000);
 lcd.clear();
 delaystart2();
 slowstart2();
 forwardspeed2();
 reversespeed2();
 slowstop2(); 
 }

 if(keyx == 1){
 delay(2000);
 lcd.clear();
 waktu = 0;
 mulaistart();
 mulaistop();
 mulaistart2();
 mulaistop2();
 }
 
 delay(200);
}



void slowstart1(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW START 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstart1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstart1();  
}


void forwardspeed1(){
 lcd.setCursor(0,0);
 lcd.print("SET FORWARD SPEED 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vforwardspeed1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

forwardspeed1();  
}


void reversespeed1(){
 lcd.setCursor(0,0);
 lcd.print("SET REVERSE SPEED 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vreversespeed1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

reversespeed1();  
}


void slowstop1(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW STOP 1");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstop1);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstop1();  
}


//=======================================================

void slowstart2(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW START 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstart2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstart2();  
}


void forwardspeed2(){
 lcd.setCursor(0,0);
 lcd.print("SET FORWARD SPEED 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vforwardspeed2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

 if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr6, vforwardspeed2);
  vforwardspeed2 = vforwardspeed2 * 2;
  return;
  }   

forwardspeed2();  
}


void reversespeed2(){
 lcd.setCursor(0,0);
 lcd.print("SET REVERSE SPEED 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vreversespeed2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

 if(btokx == 0){
  lcd.clear();
  delay(2000);
  EEPROM.write(addr7, vreversespeed2);
  vreversespeed2 = vreversespeed2 * 2;
  return;
  }   

reversespeed2();  
}


void slowstop2(){
 lcd.setCursor(0,0);
 lcd.print("SET SLOW STOP 2");
 lcd.setCursor(0,1);
 lcd.print("SPEED : ");  
 lcd.print(vslowstop2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

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

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

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

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

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

slowstop2();  
}



void delaystart2(){
 lcd.setCursor(0,0);
 lcd.print("SET DELAY START 2");
 lcd.setCursor(0,1);
 lcd.print("Ms=: ");  
 lcd.print(vdelaystart2);
 lcd.print("  ");  
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);

 if(vdelaystart2 < 0){
  vdelaystart2 = 5000;
  }

 if(vdelaystart2 > 5000){
  vdelaystart2 = 0;
  } 

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

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

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

delaystart2();  
}

//==============================================================================================
void mulaistart(){

 dataadc1 = digitalRead(A0);
 dataadc2 = digitalRead(A1);
 dataadc3 = digitalRead(A2);
 dataadc4 = digitalRead(A3);
 
 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);
 
 lcd.setCursor(0,0);
 lcd.print("FSPEED1:");
 lcd.print(vforwardspeed1);
 lcd.print("/");  
 lcd.print(vreversespeed1);
 lcd.print(" ");
 
 lcd.setCursor(0,1);
 lcd.print("FSPEED2:");
 lcd.print(vforwardspeed2);
 lcd.print("/");  
 lcd.print(vreversespeed2);
 lcd.print(" ");
 
 lcd.setCursor(0,2);
 lcd.print("T:");
 lcd.print(waktu);
 lcd.print("/");  
 lcd.print(vdelaystart2);
 lcd.print(" "); 
   
waktu++;  
vslowstart1 = vslowstart1 + 1;
kec1 = vslowstart1;

if(vslowstart1 >= vforwardspeed1){
   vslowstart1 = vforwardspeed1; 
   kec1 = vforwardspeed1; 
}

if(vslowstart2 > vforwardspeed2){
   vslowstart2 = vforwardspeed2; 
   kec2 = vforwardspeed2;  
}

  //arah CW
  //digitalWrite(motor1a,HIGH);
  //digitalWrite(motor1b,LOW);  
  //analogWrite(ena,kec1);
  //arah CW
  analogWrite(motor1a,kec1);
  analogWrite(motor1b,0);

if(waktu >= vdelaystart2){
  vslowstart2 = vslowstart2 + 1;
  kec2 = vslowstart2;
  waktu = vdelaystart2;
  
  //arah CW
  //digitalWrite(motor2a,HIGH);
  //digitalWrite(motor2b,LOW);  
  //analogWrite(enb,kec2);
    //arah CW
  analogWrite(motor2a,kec2);
  analogWrite(motor2b,0);
}

if(dataadc2 == 1){
  lcd.clear();
  return;
}

if(dataadc4 == 1){
  lcd.clear();
  return;
}

delay(100);

mulaistart();  
}



void mulaistop(){

 btsetx = digitalRead(btset);
 keyx = digitalRead(key);
  
 lcd.setCursor(0,0);
 lcd.print("SLOW1 :");
 lcd.print(vslowstart1);
 lcd.print("/");  
 lcd.print(vslowstop1);
 lcd.print("   ");

 lcd.setCursor(0,1);
 lcd.print("SLOW2 :");
 lcd.print(vslowstart2);
 lcd.print("/");  
 lcd.print(vslowstop2);
 lcd.print("   ");
 
 if(btokx == 0){
  lcd.clear();
  delay(2000);
  waktu = 0;
  return;
  } 

vforwardspeed1 = vforwardspeed1 - 1;
kec1 = vforwardspeed1;

vforwardspeed2 = vforwardspeed2 - 1; 
kec2 = vforwardspeed2; 

if(vforwardspeed1 <= 0){
kec1 = 0; 
vforwardspeed1 = 0; 
}

if(vforwardspeed2 <= 0){
kec2 = 0;  
vforwardspeed2 = 0;
}

  //arah CW
  //digitalWrite(motor1a,HIGH);
  //digitalWrite(motor1b,LOW);  
  //analogWrite(ena,kec1);
    
  //digitalWrite(motor2a,HIGH);
  //digitalWrite(motor2b,LOW);  
  //analogWrite(enb,kec2);
  
  //arah CW
  analogWrite(motor1a,kec1);
  analogWrite(motor1b,0);
  analogWrite(motor2a,kec2);
  analogWrite(motor2b,0);

if(keyx == 1){
  lcd.clear();
  delay(1000);
  waktu = 0;
  return;
}

delay(100);
mulaistop();  
}

//====================================================

void mulaistart2(){

 dataadc1 = digitalRead(A0);
 dataadc2 = digitalRead(A1);
 dataadc3 = digitalRead(A2);
 dataadc4 = digitalRead(A3);

 btsetx = digitalRead(btset);
 btokx = digitalRead(btok);
 btupx = digitalRead(btup);
 btdownx = digitalRead(btdown);
  
 lcd.setCursor(0,0);
 lcd.print("RSPEED :");
 lcd.print(vforwardspeed1);
 lcd.print("/");  
 lcd.print(vreversespeed1);
 lcd.print("   ");

 lcd.setCursor(0,1);
 lcd.print("RSPEED2 :");
 lcd.print(vforwardspeed2);
 lcd.print("/");  
 lcd.print(vreversespeed2);
 lcd.print("/");  
 lcd.print(vdelaystart2);
 lcd.print("   ");
   
waktu++;  
vslowstart1 = vslowstart1 + 1;
kec1 = vslowstart1 + 1;

if(vslowstart1 >= vreversespeed1){
vslowstart1 = vreversespeed1;    
kec1 = vreversespeed1;  
}

if(vslowstart2 >= vreversespeed2){
vslowstart2 = vreversespeed2;   
kec2 = vreversespeed2;  
}
 
  //arah CCW
  //digitalWrite(motor1a,LOW);
  //digitalWrite(motor1b,HIGH);  
  //analogWrite(ena,kec1);

  //arah CCW
  analogWrite(motor1a,0);
  analogWrite(motor1b,kec1);

if(waktu >= vdelaystart2){
  vslowstart2 = vslowstart2 + 1;
  kec2 = vslowstart2;
  waktu = vdelaystart2;
  //arah CCW
  //digitalWrite(motor2a,LOW);
  //digitalWrite(motor2b,HIGH);  
  //analogWrite(enb,kec2);
  analogWrite(motor2a,0);
  analogWrite(motor2b,kec2);
}

if(dataadc1 == 0){
  return;
}

if(dataadc3 == 0){
  return;
}

delay(1);
mulaistart2();  
}


void mulaistop2(){

 btsetx = digitalRead(btset);
 keyx = digitalRead(key);
  
 lcd.setCursor(0,0);
 lcd.print("SLOW1 :");
 lcd.print(vslowstart1);
 lcd.print("/");  
 lcd.print(vslowstop1);
 lcd.print("   ");

 lcd.setCursor(0,2);
 lcd.print("SLOW2 :");
 lcd.print(vslowstart2);
 lcd.print("/");  
 lcd.print(vslowstop2);
 lcd.print("   ");

vreversespeed1 = vreversespeed1 - 1; 
kec1 = vreversespeed1;
vreversespeed2 = vreversespeed2 - 1;
kec2 = vreversespeed2; 

if(vreversespeed1 <= 0){
kec1 = 0;  
vreversespeed1 = 0;
}

if(vreversespeed2 <= 0){
vreversespeed2 = 0;
kec2 = 0;  
}

  //arah CCW
  //digitalWrite(motor1a,LOW);
  //digitalWrite(motor1b,HIGH);  
  //analogWrite(ena,kec1);
  //digitalWrite(motor2a,LOW);
  //digitalWrite(motor2b,HIGH);  
  //analogWrite(enb,kec2);

  analogWrite(motor1a,0);
  analogWrite(motor1b,kec1);
  analogWrite(motor2a,0);
  analogWrite(motor2b,kec2);

if(keyx == 1){
  lcd.clear();
  delay(1000);
  waktu = 0;
  return;
}

delay(1);
mulaistop2();  
}