// simulation for https://forum.arduino.cc/t/variable-wont-increment-in-if-statement/964895/2?u=davex
//Fix the typo in the last if statement for proper operation
#include <Ultrasonic.h>
#include <Servo.h>
//#include <ESP8266WiFi.h>
#include <math.h>
#define motorRpin 2
#define motorLpin 3
Servo motorR;
Servo motorL;
//Ultrasonic usFront (D8, D5);
//Ultrasonic usRight (D9, D6);
//Ultrasonic usLeft (D10, D7);
int distFront = 0;
int distRight = 0;
int distLeft = 0;
int wall = 0;
void setup() {
Serial.begin(115200);
motorR.attach(motorRpin);
motorL.attach(motorLpin);
motorR.write(90); //Right Forward = 0
motorL.write(90); //Left Forward = 180
}
void loop() {
// distFront = usFront.read(CM) * 10;
//distRight = usRight.read(CM) * 10;
//distLeft = usLeft.read(CM) * 10;
distFront = random(1,100) * 10;
distRight = random(1,100) * 10;
distLeft = random(1,100) * 10;
// Serial.println(distFront);
// Serial.println(distRight);
// Serial.println(distLeft);
if(wall != 0){
Serial.print(" ");
Serial.print(wall);
Serial.print(" ");
}
//delay(100);
if (wall < 12 && distFront > 150 && distRight > 40 && distLeft > 40) {
motorR.write(52); //move forward if no obstacles
motorL.write(180);
Serial.print("F");
}
if (wall < 12 && distFront <= 150) {
Serial.print("W");
motorR.write(90); //stop if wall in front
motorL.write(90);
delay(500);
motorR.write(0); //turn left 90 degrees
motorL.write(0);
delay(150);
motorR.write(52);
motorL.write(180); //go forwards again
wall++;
Serial.print(wall);
distFront = 1000;
delay(500);
}
if (wall < 12 && distFront > 150 && distRight <= 40) {
Serial.print("L");
motorR.write(50); //turn left slightly if wall detected on right
motorL.write(90);
delay(20);
motorR.write(52); //forwards again
motorL.write(180);
}
if (wall < 12 && distFront > 150 && distLeft <= 40) {
Serial.print("R");
motorR.write(90); //turn right slightly if wall detected on left
motorL.write(130);
delay(25);
motorR.write(52); //forwards again
motorL.write(180);
}
// the next statement uses `=` insteead of `==`
if (wall = 12 && distFront <= 150) { // typo & fails
//if (wall == 12 && distFront <= 150) {
motorR.write(90); //stop when race is finished
motorL.write(90);
wall = 13;
}
}