Hi i have motor shield with ESP8266 and i need control rccar by blynk.
Motor A use for fluently foward and back and motor B for turn left and right.
https://www.youtube.com/watch?v=RdZYaJCn04M
This sketch working for motorA foward and back. But i dont know how change this sketch for motor B: turn left and right. I try it something but no good.
Thanks.
// Simple WiFi Car using Blynk Joystick Widget
// Pins are based on looking at rear of car.
// -- Motor Leads --
// Looking at rear of car, left motor leads, A- -> red, A+ -> black
// Looking at rear of car, right motor leads, B- -> black, B+ -> red
// -- Blynk Settings --
// ---Use a Joystick Widget --
// ->Merge not Split should be selected
// ->Select "Virtual Pin 1"
// ->Turn autorotate off, use the phone in a portrait not landscape position
// ->Leave autoreturn on...too hard to control with it off
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define RightMotorSpeed 4
#define RightMotorDir 0 //B
#define LeftMotorSpeed 5 //A
#define LeftMotorDir 2
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
// Use your own WiFi settings
char auth[] = "b00396c3365b86c5";
char ssid[] = "ASUS";
char pass[] = "Jersin72";
// neutral zone settings for x and y
// joystick must move outside these boundary numbers to activate the motors
// makes it a little easier to control the wifi car
int minRange = 312;
int maxRange = 712;
// analog speeds from 0 (lowest) - 1023 (highest)
// 3 speeds used -- 0 (noSpeed), 350 (minSpeed), 850 (maxSpeed).
// use whatever speeds you want...too fast made it a pain in the ass to control
int minSpeed = 450;
int maxSpeed = 1023;
int noSpeed = 0;
void moveControl(int x, int y)
{
// movement logic
// move forward
// y je vetsi jak maxrange a souÄasnÄ x je vetsi jak minRange a souÄasne mensi jak max range
if(y >= maxRange && x >= minRange && x <= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move forward right
else if(x >= maxRange && y >= maxRange) //zataci R
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move forward left
else if(x <= minRange && y >= maxRange)
{
digitalWrite(RightMotorDir,HIGH);
digitalWrite(LeftMotorDir,HIGH);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
// neutral zone
else if(y < maxRange && y > minRange && x < maxRange && x > minRange)
{
analogWrite(RightMotorSpeed,noSpeed);
analogWrite(LeftMotorSpeed,noSpeed);
}
// move back
else if(y <= minRange && x >= minRange && x <= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and right
else if(y <= minRange && x <= minRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,minSpeed);
analogWrite(LeftMotorSpeed,maxSpeed);
}
// move back and left
else if(y <= minRange && x >= maxRange)
{
digitalWrite(RightMotorDir,LOW);
digitalWrite(LeftMotorDir,LOW);
analogWrite(RightMotorSpeed,maxSpeed);
analogWrite(LeftMotorSpeed,minSpeed);
}
}
void setup()
{
// initial settings for motors off and direction forward
pinMode(RightMotorSpeed, OUTPUT);
pinMode(LeftMotorSpeed, OUTPUT);
pinMode(RightMotorDir, OUTPUT);
pinMode(LeftMotorDir, OUTPUT);
digitalWrite(RightMotorSpeed, LOW);
digitalWrite(LeftMotorSpeed, LOW);
digitalWrite(RightMotorDir, HIGH);
digitalWrite(LeftMotorDir,HIGH);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1)
{
int x = param[0].asInt();
int y = param[1].asInt();
moveControl(x,y);
}