// https://forum.arduino.cc/t/arduino-uno-unable-to-work-with-external-power-supple/1465166/
bool debug = true; // for debug print statements
int trig[] = {12, 10, 8};
int echo[] = {13, 11, 9};
int duration[3];
int distance[3];
bool obstacle[3]; // bit packed array
int unsafeDistance = 5;
int obsTotal;
int stp = 0; // moved from setup() to global variables
int L298Npin[] = {2, 3, 4, 5, 6, 7}; // array to set pins as a batch
int enA = 3; // left motor // was 2 (a DIO pin) but needs a PWM pin (3)
int in1 = 2; // was 3 but only needs a DIO pin
int in2 = 4;
int enB = 5; // right motor // PWM pin
int in3 = 6;
int in4 = 7;
void setup() {
if (debug) Serial.begin(115200); // need this for Serial printing
for (byte i = 0; i < 3; i++) {
pinMode(trig[i], OUTPUT);
pinMode(echo[i], INPUT);
pinMode(L298Npin[i], OUTPUT); // in1, ena, in2
pinMode(L298Npin[i * 2], OUTPUT); // enB, in3, in4
digitalWrite(L298Npin[i], LOW); // left motor (A) pins off
digitalWrite(L298Npin[i * 2], LOW); // right motor (B) pins off
}
analogWrite(enA, 230); // moved these two, single-call lines from loop() to setup()
analogWrite(enB, 250); // mismatched motors?
}
void loop() {
while (stp == 0) {
for (byte i = 0; i < 3; i++) { // three HC-SR04
digitalWrite(trig[i], LOW);
delayMicroseconds(2);
digitalWrite(trig[i], HIGH);
delayMicroseconds(10);
digitalWrite(trig[i], LOW);
duration[i] = pulseIn(echo[i], HIGH); // "time" changed to "duration" to avoid keyword conflict
distance[i] = duration[i] * (0.034 / 2); //speed of sound = 0.034 cm/microsecond
if (debug) debugoutput(distance[i], DEC, " Distance");
if (distance[i] <= unsafeDistance)
obstacle[i] = 1; // obstacle detected
else
obstacle[i] = 0; // obstacle not detected
}
obsTotal = obstacle[0] * 4 + obstacle[1] * 2 + obstacle[2];
if (debug) debugoutput(obsTotal, BIN, "| obsTotal (BIN)");
switch (obsTotal) {
case B000: // stop
drive (LOW, LOW, LOW, LOW);
Serial.print(" end of line: stop");
stp = 6;
break;
case B001: // left
drive(LOW, LOW, HIGH, LOW);
continue; // lets switch() test all case:s
case B010: // left for 0.5 sec
drive(LOW, LOW, HIGH, LOW);
delay(500);
continue;
case B011: // left
drive(LOW, LOW, HIGH, LOW);
continue;
case B100: // right
drive(HIGH, LOW, LOW, HIGH);
continue;
case B101: // forward
drive(HIGH, LOW, HIGH, LOW);
continue;
case B110: // right (again?)
drive(HIGH, LOW, LOW, HIGH);
continue;
case B111: //left for 0.5 sec
drive (LOW, LOW, HIGH, LOW);
delay(500);
continue;
default: // error
drive(LOW, LOW, LOW, LOW);
stp = 5;
break;
}
if (debug) Serial.println();
}
}
void drive(bool i1, bool i2, bool i3, bool i4) { // four motor input signals
digitalWrite(in1, i1);
digitalWrite(in2, i2);
digitalWrite(in3, i3);
digitalWrite(in4, i4);
}
void debugoutput(int value, byte base, char text[]) {
Serial.print(text);
Serial.print(":");
if (base == DEC) { // " " placeholders
if (value < 100)
Serial.print(" ");
if (value < 10)
Serial.print(" ");
}
if (base == BIN) { // "0" placeholders
if (value < 4)
Serial.print("0");
if (value < 2)
Serial.print("0");
}
Serial.print(value, base);
Serial.print(" ");
if (base == BIN)
Serial.println(); // end of line CRLF
}ENB
IN4
IN3
ENA
IN2
IN1