-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy.cpp
130 lines (108 loc) · 3.15 KB
/
legacy.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <SoftwareSerial.h>
int BluetoothRX = 2;
int BluetoothTX = 3;
int VectorLeftPIN = 7;
int VectorRightPIN = 7;
int TurnPIN = 8;
int StopPIN = 9;
SoftwareSerial HCBluetooth(BluetoothRX, BluetoothTX); // RX, TX
/*
5 is 101 go to ahead
4 is 100 have to vector to 0
3 is 011 stop vehicle
2 is 010 stop movement
1 is 001 have to vector to 1
*/
class Controller {
// работает после сетапа
public:
bool isArrived = false;
Controller() {
setUpPins();
}
void toRide () {
bool WillBeInThePoint = true;
setVectorOfMoving();
startMoving();
while (WillBeInThePoint and !isArrived) {
WillBeInThePoint = checkWillBeInThePoint();
}
toRide();
}
void setUpPins() {
pinMode(VectorLeftPIN, INPUT);
pinMode(VectorRightPIN, INPUT);
pinMode(TurnPIN, INPUT);
pinMode(StopPIN, INPUT);
}
private:
int lastResolution = 0;
void stopVehicle() {
digitalWrite(TurnPIN, LOW);
digitalWrite(StopPIN, HIGH);
}
void stopMoving() {
digitalWrite(TurnPIN, LOW);
}
void powerVehicle () {
digitalWrite(StopPIN, LOW);
}
void startMoving() {
if (digitalRead(StopPIN) == HIGH) {
powerVehicle();
}
digitalWrite(TurnPIN, HIGH);
digitalWrite(VectorLeftPIN, LOW);
digitalWrite(VectorRightPIN, LOW);
}
void setVectorOfMoving () {
while (!HCBluetooth.available()) {
// waiting ...
}
if (HCBluetooth.available() > 0) {
lastResolution = HCBluetooth.read();
switch (lastResolution) {
case 5:
startMoving();
break;
case 4:
digitalWrite(VectorLeftPIN, HIGH);
digitalWrite(VectorRightPIN, LOW);
break;
case 3:
stopVehicle();
break;
case 2:
stopMoving();
break;
case 1:
digitalWrite(VectorLeftPIN, LOW);
digitalWrite(VectorRightPIN, HIGH);
break;
default:
setVectorOfMoving();
break;
}
}
}
void checkWillBeInThePoint () {
while (!HCBluetooth.available()) {
// waiting ...
}
int currentResolution = HCBluetooth.read();
return lastResolution == currentResolution;
}
};
Controller vehicle = Controller();
// setup initializes serial and the button pin
void setup() {
Serial.begin(9600);
HCBluetooth.begin(9600);
vehicle.setUpPins();
}
// loop checks the button pin each time,
// and will send serial if it is pressed
void loop() {
delay(1000);
vehicle.toRide();
}