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();
}
No comments:
Post a Comment