/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example shows how to use ESP8266 Shield (with AT commands)
to connect your project to Blynk.
WARNING!
It's very tricky to get it working. Please read this article:
http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware
Change WiFi ssid, pass, and Blynk auth token to run :)
Feel free to apply it to any other example. It's simple!
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "sw-SLfyt6ZDKTQ_xai_SNVJ16XMzl7NU";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "cuijianwei";
char pass[] = "393857616";
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
#define enA 5 //右电机
#define in1 A1 //右电机
#define in2 A2 //右电机
#define in3 A3 //左电机
#define in4 A4 //左电机
#define enB 6 //左电机
#define TrigPin_z 9
#define EchoPin_z 8
#define TrigPin_j 11
#define EchoPin_j 10
#define TrigPin_y 4
#define EchoPin_y 12
#define Fmq 7
#define Gyq 6
//--------------------------------------
int motorSpeedA = 0;
int motorSpeedB = 0;
int motorA = 0;
int motorB = 0;
int sd = 0;//控制速度的变量
int cm_z = 0;
float duration_z = 0;
int cm_j = 0;
float duration_j = 0;
int cm_y = 0;
float duration_y = 0;
int Zx = 0;
int Zy = 0;
int Yx = 0;
int Yy = 0;
int ms = 2;
BlynkTimer timer;
//----------------------------摇杆控制-----------------------
BLYNK_WRITE(V1) {//摇杆1的代码
int x = param[0].asInt();
int y = param[1].asInt();
Yx = x;
Yy = y;
if(ms == 2){
yaogan();
}
}
//----------------------------重力控制-------------------------
BLYNK_WRITE(V4){//重力感应代码
int x = param[0].asFloat();
int y = param[1].asFloat();
int z = param[2].asFloat();
Zx = x;
Zy = y;
if(ms == 3){
zhongli();
}
}
//----------------模式选择------------------
BLYNK_WRITE(V5) {//分段按钮 模式控制
int MS_value = param.asInt();
switch (MS_value) {
case 1:
ms = 1;
Serial.print("Z_D");
Serial.println(MS_value);
break;
case 2:
ms = 2;
Serial.print(":" "S_D");
Serial.println(MS_value);
break;
case 3:
ms = 3;
Serial.print(":" "Z_L");
Serial.println(MS_value);
break;
}
}
//----------------------滑块调速---------------------------
BLYNK_WRITE(V6)//在Blynk应用程序中,将值写入虚拟引脚V1(滑块控制)
{
int pinValue = param.asInt(); //将来自引脚V1的输入值分配给变量
sd = pinValue;
Serial.print("sd=");
Serial.println(sd);
// process received value
}
//---------------------档位调速--------------------------------
BLYNK_WRITE(V2) {//分段按钮控制档位
int DW_value = param.asInt();
switch (DW_value) {
case 1:
sd = 400;
break;
case 2:
sd = 500;
break;
case 3:
sd = 700;
break;
case 4:
sd = 800;
break;
case 5:
sd = 1023;
break;
default:
sd = 0;
break;
}
Serial.print("sd=");
Serial.println(sd);
}
void setup()
{
// Debug console
Serial.begin(9600);
delay(10);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass,"blynk.mixly.org",8080);
timer.setInterval(1000L, myTimerEvent);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(TrigPin_z, OUTPUT); //超声波输出
pinMode(EchoPin_z, INPUT); //超声波输入
pinMode(TrigPin_j, OUTPUT); //超声波输出
pinMode(EchoPin_j, INPUT); //超声波输入
pinMode(TrigPin_y, OUTPUT); //超声波输出
pinMode(EchoPin_y, INPUT); //超声波输入
pinMode(Fmq, OUTPUT);
timer.setInterval(1000L, myTimerEvent);//每秒调用一次myTimerEvent函数
}
void loop()
{
getDistance_j();
timer.run();
Blynk.run();
Serial.println(cm_j);
}
//-----------------向APP发送数值函数----------------
void myTimerEvent(){//向APP V3发送变量值
//此函数每秒将Arduino的启动时间发送到Virtual Pin(5)。
//在应用程序中,Widget的读取频率应设置为PUSH。这意味着
//定义向Blynk App发送数据的频率。
Blynk.virtualWrite(V3,cm_z);
Blynk.virtualWrite(V10,cm_j);
Blynk.virtualWrite(V11,cm_y);
}
//---------------------超声波测距函数--------------------
// 读取超声波模块左侧距离读数(单位为厘米)
int getDistance_z(){
digitalWrite(TrigPin_z, LOW);
delayMicroseconds(5);
digitalWrite(TrigPin_z, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin_z, LOW);
duration_z = pulseIn(EchoPin_z, HIGH);
cm_z = (duration_z/2) / 29.1;
return cm_z;
}
// 读取超声波模块距离读数(单位为厘米)
int getDistance_j(){
digitalWrite(TrigPin_j, LOW);
delayMicroseconds(5);
digitalWrite(TrigPin_j, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin_j, LOW);
duration_j = pulseIn(EchoPin_j, HIGH);
cm_j = (duration_j/2) / 29.1;
return cm_j;
}
int getDistance_y(){
digitalWrite(TrigPin_y, LOW);
delayMicroseconds(5);
digitalWrite(TrigPin_y, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin_y, LOW);
duration_y = pulseIn(EchoPin_y, HIGH);
cm_y = (duration_y/2) / 29.1;
return cm_y;
}
//-----------------------远程摇杆控制函数------------------------
void yaogan(){
int JL_z = getDistance_j();
if (Yy < 470) {
// Set Motor A backward 将电机A向后设置
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward 将电机B向后设置
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
//将下降的Y轴读数从470到0转换为PWM信号的0到255值,以提高电机速度
motorSpeedA = map(Yy, 470, 0, 0, sd);
motorSpeedB = map(Yy, 470, 0, 0, sd);
}
else if (Yy > 550 || JL_z >= 30) {
// Set Motor A forward 将电机A向前设置
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward 将电机B向前设置
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
// 将增加的Y轴读数从550转为1023,将其转换为PWM信号的0至255值,以提高电机速度
motorSpeedA = map(Yy, 550, 1023, 0, sd);
motorSpeedB = map(Yy, 550, 1023, 0, sd);
}
// If joystick stays in middle the motors are not moving
// 如果操纵杆位于中间,则电机不移动
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (Yx < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
// 将X轴读数从470到0的递减转换为0到255的递增值
int xMapped = map(Yx, 470, 0, 0, sd);
// Move to left - decrease left motor speed, increase right motor speed
// 向左移动-降低左电机速度,提高右电机速度
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > sd) {
motorSpeedB = sd;
}
}
if (Yx > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
// 将增加的X轴读数从550到1023转换为0到255值
int xMapped = map(Yx, 550, 1023, 0, sd);
// Move right - decrease right motor speed, increase left motor speed
// 向右移动-降低右电机速度,提高左电机速度
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > sd) {
motorSpeedA = sd;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
// 防止低速时发出嗡嗡声(根据您的电机进行调整。如果PWM值低于70,我的电机将无法启动)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A 向电机A发送PWM信号
analogWrite(enB, motorSpeedB);
Serial.print(motorSpeedA);
Serial.println(motorSpeedB);
}
//-----------------------重力控制函数------------------------
void zhongli(){
if (Zy < 0) {
// Set Motor A backward 将电机A向后设置
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward 将电机B向后设置
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
//将下降的Y轴读数从470到0转换为PWM信号的0到255值,以提高电机速度
motorA = map(Zy, 0, -9, 0, sd);
motorB = map(Zy, 0, -9, 0, sd);
}
else if (Zy > 0) {
// Set Motor A forward 将电机A向前设置
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward 将电机B向前设置
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
// 将增加的Y轴读数从550转为1023,将其转换为PWM信号的0至255值,以提高电机速度
motorA = map(Zy, 0, 9, 0, sd);
motorB = map(Zy, 0, 9, 0, sd);
}
// If joystick stays in middle the motors are not moving
// 如果操纵杆位于中间,则电机不移动
else {
motorA = 0;
motorB = 0;
}
if (Zx < 0) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
// 将X轴读数从470到0的递减转换为0到255的递增值
int xMapped = map(Zx, -1, -9, 0, sd);
// Move to left - decrease left motor speed, increase right motor speed
// 向左移动-降低左电机速度,提高右电机速度
motorA = motorA + xMapped;
motorB = motorB - xMapped;
if (motorA < 0) {
motorA = 0;
}
if (motorB > sd) {
motorB = sd;
}
}
if (Zx > 0) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
// 将增加的X轴读数从550到1023转换为0到255值
int xMapped = map(Zx, 1, 9, 0, sd);
// Move right - decrease right motor speed, increase left motor speed
// 向右移动-降低右电机速度,提高左电机速度
motorA = motorA - xMapped;
motorB = motorB + xMapped;
if (motorA > sd) {
motorA = sd;
}
if (motorB < 0) {
motorB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
// 防止低速时发出嗡嗡声(根据您的电机进行调整。如果PWM值低于70,我的电机将无法启动)
if (motorA < 200) {
motorA = 0;
}
if (motorB < 200) {
motorB = 0;
}
analogWrite(enA, motorA); // Send PWM signal to motor A 向电机A发送PWM信号
analogWrite(enB, motorB);
Serial.print("motorA=");
Serial.print(motorA);
Serial.print(":");
Serial.print("motorB=");
Serial.println(motorB);
}
//-----------------------自动控制函数----------------------
void Qian(){
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward 将电机B向后设置
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, sd); // Send PWM signal to motor A 向电机A发送PWM信号
analogWrite(enB, sd);
}
void Hou(){
// Set Motor A forward 将电机A向前设置
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward 将电机B向前设置
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, sd); // Send PWM signal to motor A 向电机A发送PWM信号
analogWrite(enB, sd);
}
void You(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, sd); // Send PWM signal to motor A 向电机A发送PWM信号
analogWrite(enB, sd);
}
void Zuo(){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, sd);
analogWrite(enB, sd);
}
void stopc(){
analogWrite(enA, 0);
analogWrite(enB, 0);
}