Need help with code for ultrasonic sensor car

Am pretty new to coding but find it cool, so i went and bought the (Playknowlogy Arduino Robotcar Starter set)
Built the car and uploaded the pre-written code and tried the car out. It was behaving rather erratic so i sat down and read through the code, fixed some errors in it and tweaked some bits.

It’s definitely driving better but it seems there is some error in the code that makes it so it only spins in one direction, and only backwards, never forward… which makes it crash into stuff while spinning Have spent hours trying to figure out where something is wrong but can’t find it. If someone would be able to help id be extatic! Would like the car to drive forward a bit and then check distance forward, left and right and adapt accordingly while avoiding collision.

Link to product & current Code below:

Website is in swedish but hoping y’all get translation in browser :slight_smile:
Original code and instructions can be found on website.
playknowlogy arduino robotcar

#include <Servo.h>
int pinLB=6; // define pin6 as left back connect with IN1
int pinLF=9; // define pin9 as left forward connect with IN2
int pinRB=10; // define pin10 as right back connect with IN3
int pinRF=11; // define pin11 as right forward connect with IN4
int inputPin = A0; // define ultrasonic receive pin (Echo)
int outputPin =A1; // define ultrasonic send pin(Trig)
int Fspeedd = 0; // forward distance
int Rspeedd = 0; // right distance
int Lspeedd = 0; // left distance
int directionn = 0; //
Servo myservo; // new myservo
int delay_time = 250; // set stable time
int Fgo = 8;
int Rgo = 6;
int Lgo = 4;
int Bgo = 2;
// forward
// turn right
// turn left
// back
void setup()
{
Serial.begin(9600);
pinMode(pinLB,OUTPUT);
pinMode(pinLF,OUTPUT);
pinMode(pinRB,OUTPUT);
pinMode(pinRF,OUTPUT);
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
myservo.attach(5); // define the servo pin(PWM)
}
void advance(int a) // forward
{
digitalWrite(pinRB,LOW);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW);
digitalWrite(pinLF,HIGH);
delay(a * 600);
}
void turnR(int d) //turn right
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,LOW);
digitalWrite(pinLF,HIGH);
delay(d * 10);
}
void turnL(int e) //turn left
{
digitalWrite(pinRB,LOW);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW);
delay(e * 10);
}
void stopp(int f) //stop
{
digitalWrite(pinRB,LOW);
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,LOW);
digitalWrite(pinLF,LOW);
delay(f * 400);
}
void back(int g) //back
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW);
delay(g * 200);
}
void detection() //test the distance of different directions
{
int delay_time = 250; //
delay(200);
ask_pin_F(); // read forward distance
if(Fspeedd < 10) // if distance less then 10
{
stopp(1);
back(2);
}
if(Fspeedd < 25) // if distance less then 25
{
stopp(1);
ask_pin_L();
delay(delay_time);
ask_pin_R();
delay(delay_time);
if(Lspeedd > Rspeedd) //if left distance more than right distance
{
directionn = Rgo;
}
if(Lspeedd <= Rspeedd)//if left distance not more than right distance
{
directionn = Lgo;
}
if (Lspeedd < 10 && Rspeedd < 10) //if left distance and right distance both are less than 10
{
directionn = Bgo;
}
}
else
{
directionn = Fgo; // forward go
}
}
void ask_pin_F() // test forward distance
{
myservo.write(105);
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
float Fdistance = pulseIn(inputPin, HIGH);
Fdistance= Fdistance/5.8/10;
Serial.print("F distance:");
Serial.println(Fdistance);
Fspeedd = Fdistance;
}
void ask_pin_L() // test left distance
{
myservo.write(175);
delay(delay_time);
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
float Ldistance = pulseIn(inputPin, HIGH);
Ldistance= Ldistance/5.8/10;
Serial.print("L distance:");
Serial.println(Ldistance);
Lspeedd = Ldistance;
}
void ask_pin_R() // test right distance
{
myservo.write(20);
delay(delay_time);
digitalWrite(outputPin, LOW);
delayMicroseconds(2);
digitalWrite(outputPin, HIGH);
delayMicroseconds(10);
digitalWrite(outputPin, LOW);
float Rdistance = pulseIn(inputPin, HIGH);
Rdistance= Rdistance/5.8/10;
Serial.print("R distance:");
Serial.println(Rdistance);
Rspeedd = Rdistance;
}
void loop()
{
myservo.write(105);
detection();
if(directionn == 2)
{
back(3);
turnL(2);
Serial.print(" Reverse ");
}
if(directionn == 6)
{
back(1);
turnR(6);
Serial.print(" Right ");
}
if(directionn == 4)
{
back(1);
turnL(6);
Serial.print(" Left ");
}
if(directionn == 8)
{
advance(1);
Serial.print(" Advance ");
Serial.print(" ");
}
}

What about the serial prints? Does it ever print, "Advance"?

Try using the IDE’s auto-format tool ; it’s a great help for visualising code structure.

And all those delays - they have to go.

The picture on the website shows a 2-wheel drive robotcar. The code seems to imply, at least to me, 4-wheel drive with variables named pinLB, pinLF, pinRB and pinRF.

I think the first step is to replace the contents of loop() with

void loop()
{
  advance(1);
}

hold the car off the ground and see if the wheels spin.

I would continue to test each function one at a time.

Thanks for the replies!

aarg:
What about the serial prints? Does it ever print, “Advance”?

Not that i can see no. But i’m new to Arduino so i don’t really get how it all works… :confused:
The Serial Monitor doesn’t show anything at all, but in the Serial Plotter it loops the following:
Reverse, F, distance
L, distance, distance
R, distance, distance
(attached img showing it)

TheMemberFormerlyKnownAsAWOL:
Try using the IDE’s auto-format tool ; it’s a great help for visualising code structure.

And all those delays - they have to go.

Thanks! That made it much easier to read the code.
And yeah those delays didn’t really seem to change a lot in the behavior, don’t really understand how they work tbh…

I figured (since it is a 2-wheel driven car) that they where variables for spinning the wheels in both directions.
For example, when turning the car left, one would call on pinLB (left back) and pinRF (right forward). Or am i thinking wrong?

I tried doing what you wrote, and i figured out that the variables seem to trigger the wrong reaction, so it might be that the manual has a faulty connection table. I checked it several times to make sure i put it all at the correct places, so i am assuming that is it, or the pre-written code that followed is just badly written… Anyway, i will keep testing the different functions and see what i can come up with. :slight_smile:

What in the code do i edit to change the amount of time that each command is run?
Is it not the delay times?

Would like to shorten the time that the car drives backwards and turns etc. because now it does like a 360 so it doesn't really work :o :confused: