#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
char auth[] = "6shj0yJKzVZnjYgH4H28kq9ww3UKfOai";
char ssid[] = "SIMOR#2019";
char pass[] = "simor123";
Servo servo;
Servo servo1;
int PWMA=5;//Right side
int PWMB=4;//Left side
int DA=0;//Right reverse
int DB=2;//Left reverse
void setup(){
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
servo.attach(14); // 14 means D5 pin of ESP8266
servo.write(0);
servo1.attach(12); // 12 means D6 pin of ESP8266
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DA, OUTPUT);
pinMode(DB, OUTPUT);
}
void loop(){
Blynk.run();
}
BLYNK_WRITE(V3) {
int molen1 = param.asInt();
if(molen1 == 1 ){
if(!servo1.attached())servo1.attach(12) ;
servo1.write(0);
}else {
servo1.detach();
}
}
BLYNK_WRITE(V2) {
int molen = param.asInt();
if(molen == 1 ){
servo.write(30);
}else if(molen == 0){
servo.write(0);
}
}
BLYNK_WRITE(V0) {
int capit = param.asInt();
if(capit == 1 ){
servo.write(65);
//delay(15);
}else if(capit == 0){
servo.write(0);
}
}
// Handling Joystick data
BLYNK_WRITE(V1){
int x = param[0].asInt();
int y = param[1].asInt();
if(x==-1 && y==-1){ //Backward and Left
digitalWrite(PWMA, LOW);
digitalWrite(DA, LOW);
digitalWrite(PWMB, HIGH);
digitalWrite(DB, HIGH);
}else if(x==-1 && y==0){ //Left Turn
digitalWrite(PWMA, 450);
digitalWrite(DA, HIGH);
digitalWrite(PWMB, 450);
digitalWrite(DB, LOW);
}else if(x==-1 && y==1){ //Forward and Left
digitalWrite(PWMA, LOW);
digitalWrite(DA, LOW);
digitalWrite(PWMB, HIGH);
digitalWrite(DB, LOW);
}else if(x==0 && y==-1){ //Backward
digitalWrite(PWMA, HIGH);
digitalWrite(DA, HIGH);
digitalWrite(PWMB, HIGH);
digitalWrite(DB, HIGH);
}else if(x==0 && y==0){ //Stay
digitalWrite(PWMA, LOW);
digitalWrite(DA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DB, LOW);
}else if(x==0 && y==1){ //Forward
digitalWrite(PWMA, HIGH);
digitalWrite(DA, LOW);
digitalWrite(PWMB, HIGH);
digitalWrite(DB, LOW);
}else if(x==1 && y==-1){ //Backward and Right
digitalWrite(PWMA, HIGH);
digitalWrite(DA, HIGH);
digitalWrite(PWMB, LOW);
digitalWrite(DB, LOW);
}else if(x==1 && y==0){ //Right turn
digitalWrite(PWMA, 450);
digitalWrite(DA, LOW);
digitalWrite(PWMB, 450);
digitalWrite(DB, HIGH);
}else if(x==1 && y==1){ //Forward and Right
digitalWrite(PWMA, HIGH);
digitalWrite(DA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DB, LOW);
}
}
Tidak ada komentar:
Posting Komentar