[solved] random + if statement

Hello World,

Is there any brave programmer who can help me with following case?
I am coding very simple robot with:

  • 2 x servo 360
  • one distance sensor HC SR04

I want the robot to move forward by default and when it approaches a wall it has to make backward rotation (approximately 90 degrees in random way either leftwards or rightwards), then go forward again etc. Here’s the code which is almost working (robot goes forward and when it approaches a barrier he goes backward to the right):

#include <Servo.h>
const int RForward = 1350;
const int RBackward = 1650;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 1500;
const int LNeutral = 1500;
const int RSlowForward = 1450;
const int RSlowBackward = 1550;
const int LSlowForward = RSlowBackward;
const int LSlowBackward = RSlowForward;
const int dangerThresh = 80;
Servo leftMotor;
Servo rightMotor;

void setup(){

void loop(){
int distanceFront = frontSensor();

if (distanceFront > dangerThresh || distanceFront < 0){ //sometimes the HCSR04 shows minus values…

else if (distanceFront < dangerThresh){


long frontSensor()
pinMode (trigFront, OUTPUT);
pinMode (echoFront, INPUT);

//Front HC-SR04
int timeFront, distFront;
long duration, cm;
digitalWrite (trigFront, HIGH);
delayMicroseconds (200);
digitalWrite (trigFront, LOW);
delayMicroseconds (200);
timeFront = pulseIn (echoFront, HIGH);
distFront = (timeFront/2)/29.1;
return distFront;

void moveForward(){

void moveBackward(){
** int randBack;**
** randBack = random(2);**
** Serial.print(“randBack”);**
** Serial.println(randBack);**

if (randBack = 0){
** leftMotor.writeMicroseconds(LBackward);**
** rightMotor.writeMicroseconds(RSlowBackward);**
if (randBack = 1){
** leftMotor.writeMicroseconds(LSlowBackward);**
** rightMotor.writeMicroseconds(RBackward);**

I have troubles with the highlighted section (basically the robot moves backward only in one way, disregarding the if statements and random issues…)

Anyone willing to help me?


P.S. You can see the robot in the shrimp_02.png file below

if (randBack = 0){
if (randBack = 1){

= is the assignment operator - you want the comparison operator ==.

Also, the second 'if' is redundant (if randBack is not 0 it must be 1) so you should use an 'else' instead.


Thank you!
I'm amazed by the fastness of your response.
I checked the robot - now he's "thrilled" of the barriers, shaking it's body when as close as 80cm to the wall.
Any idea how to use a random value (0 or 1) and make the 90 degree backward movement?
I know that I can use delay, but is there anything "better"?

I'll try to upload a video for you!

I know that I can use delay, but is there anything “better”?

It would be better to use a non-blocking design and have a little state machine to keep track of what your 'bot is doing - this will make it possible to control multiple independent functions in future. However, the simplest change just to make your existing sketch do what you want is just to put a delay() call in so that it reverses for a fixed time after detecting the obstacle. I do recommend that you start learning about non-blocking designs though, it will make it possible to do much more with your 'bot.


Thanks for advices. I want to make the code a bit more complex and add two more sensors.
I hope I can find answers in this forum.

Here's video from the code you helped me develop:

Have a nice day!