Skip to content

Commit 03fb342

Browse files
committed
feat: add "curcuit breaker" to avoid getting stuck
1 parent bcdbb7e commit 03fb342

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

smart-car/smart-car.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#define SENSOR_DELAY 10
44

5+
#define MAX_QUIRKS 3
6+
57
#define ENA_PIN 10
68
#define ENB_PIN 9
79
#define IN1_PIN 2
@@ -41,6 +43,9 @@ int prd;
4143
int pld;
4244
char pdir;
4345

46+
// quirk counter - triggers "circuir breaker" on reaching MAX_QUIRKS
47+
int qc = 0;
48+
4449
void setup() {
4550
#ifdef DEBUG
4651
Serial.begin(9600);
@@ -98,9 +103,19 @@ int lDist() {
98103
}
99104

100105
char chooseDirection(int cdist, int rdist, int ldist) {
101-
// below "0" is a sensor quirk: continue with previously chosen direction in this case
106+
// below or equal "0" is a sensor quirk ...
102107
if ((cdist <= 0) or (rdist <= 0) or (ldist <= 0)) {
103-
return pdir;
108+
// ... continue with previously chosen direction before reaching MAX_QUIRKS
109+
if (qc < MAX_QUIRKS) {
110+
qc++;
111+
return pdir;
112+
}
113+
// ... pull back after reaching MAX_QUIRKS
114+
qc = 0;
115+
return _BCK;
116+
} else {
117+
// reset quirk counter if sensor quirk is not the case
118+
qc = 0;
104119
}
105120

106121
// we got stuck: spin left or right ...

0 commit comments

Comments
 (0)