-
Notifications
You must be signed in to change notification settings - Fork 1
/
RobotSumoSoft.ino
187 lines (146 loc) · 3.85 KB
/
RobotSumoSoft.ino
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#define DEBUG true
//Déclaration des pins
#define A_1 8 // Pont en H
#define A_2 9
#define B_2 10
#define B_1 11
#define BP1 4 // Bouton de démarrage
#define BP2 3
#define SENSORBR A3 // Capteur de ligne
#define SENSORBL A4
#define SENSORBB A5
#define GREEN 6 // Led d'état
#define BLUE 5
#define RED 7
#define RSENS A1
#define LSENS A2
#define BTRY A0
#define BACK 0
#define RIGHT 1
#define LEFT 2
void initHBridge(int p1, int p2, int p3, int p4); // Initialisation pins H Bridge
void controlMotor(int __speeda, int __speedb); // speed : -255 to 255
void changeState(int newState);
int frontLeftSensor(void);
int frontRightSensor(void);
void myDelay(int __ms);
int start = 0;
void setup(void) {
initHBridge(A_1, A_2, B_1, B_2);
pinMode(BP1, INPUT);
pinMode(BP2, INPUT);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(SENSORBB, INPUT);
pinMode(SENSORBL, INPUT);
pinMode(SENSORBR, INPUT);
if (DEBUG)
Serial.begin(115200);
changeState(0);
do {
if (digitalRead(BP1) == 0 || digitalRead(BP2) == 0) {
start = 1;
changeState(1);
}
} while (start != 1);
delay(5000);
changeState(2);
}
int found = 0;
void loop(void) {
if (found == 0) {
while (found != 1) {
controlMotor(0, 60);
if (frontRightSensor() > 150 && frontLeftSensor() > 150) {
found = 1;
controlMotor(150, 150);
}/*else if (frontLeftSensor() > 100) {
found = 1;
controlMotor(150, 150);
}*/
}
}
int difMesure = frontLeftSensor() - frontRightSensor();
Serial.println(analogRead(SENSORBL));
difMesure *= 3;
if (frontLeftSensor() > 400 || frontRightSensor() > 400) {
controlMotor(255, 255);
} else if (frontLeftSensor() == 0 && frontRightSensor() == 0) {
found = 0;
} else if (analogRead(SENSORBB) > 100) {
controlMotor(255, 255);
myDelay(1000);
} else if (analogRead(SENSORBL) > 100) {
controlMotor(-255, -255);
myDelay(1000);
} else if (analogRead(SENSORBR) > 100) {
controlMotor(-255, -255);
myDelay(1000);
} else {
if (difMesure < 20 && difMesure > -20) {
controlMotor(150, 150);
} else if (difMesure > 0) {
controlMotor(150, 150 - difMesure);
} else if (difMesure < 0) {
difMesure = (int)sqrt(pow(difMesure, 2));
controlMotor(150 - difMesure, 150);
}
}
delay(100);
}
void changeState(int newState) { // TODO : state of the led in function of the state variable
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
switch (newState) {
case 0:
digitalWrite(BLUE, LOW);
break;
case 1:
digitalWrite(GREEN, LOW);
break;
case 2:
digitalWrite(RED, LOW);
break;
}
}
void initHBridge(int p1, int p2, int p3, int p4) {
pinMode(p1, OUTPUT);
pinMode(p2, OUTPUT);
pinMode(p3, OUTPUT);
pinMode(p4, OUTPUT);
}
int frontLeftSensor(void) {
int m = analogRead(LSENS);
if (m < 20)
return 0;
return m;
}
int frontRightSensor(void) {
int m = analogRead(RSENS);
if (m < 20)
return 0;
return m;
}
void controlMotor(int __speeda, int __speedb) {
if (__speeda < 0) {
__speeda = 255 - __speeda;
digitalWrite(A_1, HIGH);
} else {
digitalWrite(A_1, LOW);
}
analogWrite(A_2, __speeda);
if (__speedb < 0) {
__speedb = 255 - __speedb;
digitalWrite(B_1, HIGH);
} else {
digitalWrite(B_1, LOW);
}
analogWrite(B_2, __speedb);
}
void myDelay(int __ms) {
for (int i = 0; i < __ms; ++i) {
delayMicroseconds(1000);
}
}