Hi, so I've been working on a 4wd robot car controlled with arduino via wifi. Got it working with just the controls pretty nicely. But I've been struggling to implement the ultrasonic sensor into it.
Below is the code for the arduino I'm using. With that I have an wemos d1 mini esp8266 wifi board communicating between the arduino and phone. Pretty much just acting as wlan ap relaying the commands from the phone to the arduino. So far it is working nicely. When I press a button on the phone the car move/turn until I release the button, then it stops. Just like I wanted it.
But when I try to add the sensors into the code I just can't seem to find a way to make it work like I want. And what I want is to stop the car moving forward/backward if the corresponding sensor detects anything closer than 30cm (might change it closer if it feels too far). And also prevent the car from moving to the direction where it detects a block.
I got it to a part where it prevents the moving to a direction where it detects a block. But having really hard time to get it to also stop when already moving.
Also been playing a lot with multiple if and else if clauses but they seem to keep going even when I release the button on the phone.
I'm not a complete beginner with programming but not too experienced either. Any help would be much appreciated. I've checked endless number of other robot car projects but haven't found any with this functionality.
Ps. My circuitry is pretty basic, arduino connected to the wifi module with rx&tx pins, motor driver with pins 8-13, and ultrasonic sensor on pins 2-5. Future plans include on adding a camera with 2 servos moving the base and including the picture on the same app with the controls. Also maybe some leds to light up the camera image.
const int RightB = 8;
const int RightF = 9;
const int RightPWM = 10;
const int LeftPWM = 11;
const int LeftB = 12;
const int LeftF = 13;
const int TrigBack = 2;
const int EchoBack = 3;
const int TrigFront = 4;
const int EchoFront = 5;
long durationBack;
int distanceBack;
long durationFront;
int distanceFront;
int command;
void setup() {
pinMode(RightB, OUTPUT);
pinMode(RightF, OUTPUT);
pinMode(RightPWM, OUTPUT);
pinMode(LeftPWM, OUTPUT);
pinMode(LeftB, OUTPUT);
pinMode(LeftF, OUTPUT);
pinMode(TrigBack, OUTPUT);
pinMode(EchoBack, INPUT);
pinMode(TrigFront, OUTPUT);
pinMode(EchoFront, INPUT);
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
command = Serial.read();
switch (command) {
case 'F': {
forward();break;
}
case 'B': {
backward();break;
}
case 'R': {
right();break;
}
case 'L': {
left();break;
}
case 'S': {
stops();break;
}
}
}
}
int frontsensor() {
digitalWrite(TrigFront, LOW);
delayMicroseconds(2);
digitalWrite(TrigFront, HIGH);
delayMicroseconds(10);
digitalWrite(TrigFront, LOW);
durationFront = pulseIn(EchoFront, HIGH);
distanceFront= durationFront*0.034/2;
return distanceFront;
}
int backsensor() {
digitalWrite(TrigBack, LOW);
delayMicroseconds(2);
digitalWrite(TrigBack, HIGH);
delayMicroseconds(10);
digitalWrite(TrigBack, LOW);
durationBack = pulseIn(EchoBack, HIGH);
distanceBack= durationBack*0.034/2;
return distanceBack;
}
void forward() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, HIGH);
analogWrite(RightPWM, 170);
analogWrite(LeftPWM, 170);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, HIGH);
}
void backward() {
digitalWrite(RightB, HIGH);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 170);
analogWrite(LeftPWM, 170);
digitalWrite(LeftB, HIGH);
digitalWrite(LeftF, LOW);
}
void right() {
digitalWrite(RightB, HIGH);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 140);
analogWrite(LeftPWM, 140);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, HIGH);
}
void left() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, HIGH);
analogWrite(RightPWM, 140);
analogWrite(LeftPWM, 140);
digitalWrite(LeftB, HIGH);
digitalWrite(LeftF, LOW);
}
void stops() {
digitalWrite(RightB, LOW);
digitalWrite(RightF, LOW);
analogWrite(RightPWM, 0);
analogWrite(LeftPWM, 0);
digitalWrite(LeftB, LOW);
digitalWrite(LeftF, LOW);
}