1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| #include <BlynkSimpleSerialBLE.h>
#define BLYNK_USE_DIRECT_CONNECT
char auth[] = "Blynk Auth";
int d[2] = {0};
int car[2][2] = { {5, 6}, {11, 10} }; //in 1,2,4,3
int en[2] = {3, 9};
void Controller();
void Move(int, int, int, int, int, int);
BLYNK_WRITE(V0){
for(int i=0; i<2; ++i){
d[i] = param[i].asInt();
if(d[i]<=64) d[i] = d[i]*2-255;
else if(d[i]>64 && d[i]<=192) d[i]=0;
}
}
void setup() {
Serial.begin(9600);
Blynk.begin(Serial, auth);
for(int i=0; i<2; ++i){
pinMode(en[i], OUTPUT);
for(int j=0; j<2; ++j){
pinMode(car[i][j], OUTPUT);
}
}
}
void loop() {
Blynk.run();
Controller();
}
void Controller() {
if(d[0]>0){
if(d[1]>0) Move(0, 1, 1, 0, min(d[0], d[1]), max(d[0], d[1]));
else if(d[1]==0) Move(0, 0, 1, 0, 0, d[0]);
else Move(1, 0, 0, 1, min(d[0], abs(d[1])), max(d[0], abs(d[1])));
}
else if(d[0]==0){
if(d[1]>0) Move(1, 0, 1, 0, d[1], d[1]);
else if(d[1]==0) Move(0, 0, 0, 0, 0, 0);
else Move(0, 1, 0, 1, abs(d[1]), abs(d[1]));
}
else{
if(d[1]>0) Move(1, 0, 0, 1, max(abs(d[0]), d[1]), min(abs(d[0]), d[1]));
else if(d[1]==0) Move(1, 0, 0, 0, abs(d[0]), 0);
else Move(0, 1, 1, 0, max(abs(d[0]), abs(d[1])), min(abs(d[0]), abs(d[1])));
}
}
void Move(int a1,int a2, int a3, int a4, int e1, int e2) {
digitalWrite(car[0][0], a1);
digitalWrite(car[0][1], a2);
digitalWrite(car[1][0], a3);
digitalWrite(car[1][1], a4);
analogWrite(en[0], e1);
analogWrite(en[1], e2);
}
|