1

Topic: obstacle avoiding car with remotexy bluetooth control

i want to merge code of remotexy car mentioned http://remotexy.com/en/examples/car/
with sr04 ultrasonic sensor so that the robot stops or turns left/right when it detects a obstacle.
The code of ultrasonic sensor is http://howtomechatronics.com/tutorials/ … r-hc-sr04/

how can I  do that?
Any help/suggestion is appreciated..
Thank you.

2

Re: obstacle avoiding car with remotexy bluetooth control

Hi Kanish,

Not sure if this helps give you some ideas. It's what i used for obstacle avoiding before i went onto RemoteXY.

I'm looking to do something similar. A switch on the graphic interface that makes the car toggle between automatic obstacle avoiding/ remote control.

This is what i used for the obstacle avoiding robot. Note I set 'speed' to 240, not max 255 (i.e. PWM voltage 0,255)

///////////////////////////////////////////////
//////Initialise pins///////////////////////////
///////////////////////////////////////////////
//////Global variables//////////////////////////
///////////////////////////////////////////////


// connect motor controller pins to Arduino digital pins
// motor one
//right
int enA = 10;
int in1 = 9;
int in2 = 8;

//left
// motor two
int enB = 5;
int in3 = 7;
int in4 = 4; //changed from 6


//sensor
const int trigPin = 11;
const int echoPin = 3;
long duration;
int distance;

//set the threshold for obstacle avoidance (cm)
int threshold = 20;

//set a counter.
//Modulo division will produce a crude randomiser for responding function
int count = 0;


///////////////////////////////////////////////
///////////The set up function/////////////////
///////////////////////////////////////////////


void setup() {
// put your setup code here, to run once:

//radar pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
 
//initial delay creates a pause before Jeff acts
delay(2000);
}


///////////////////////////////////////////////
//////////////Radar function///////////////////
///////////////////////////////////////////////

void radar()
{
//updates the global variable distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;
Serial.print("distance: ");
Serial.println(distance);
delay(10);
}


/////////////////////////////////////////////////
////////Individual Motor functions///////////////
/////////////////////////////////////////////////
////////Stop, Forward and Back///////////////
/////////////////////////////////////////////////

//motor A stop
void right_stop()
{
  // turn off motor A (Right)
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);
}

//motor B stop
void left_stop()
{
  // turn off motor B (left)
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 200);
}

//motor A forward
void right_forward()
{
      // turn on motor A (Right)
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);
}

//motor B forward
void left_forward()
{
  // turn on motor B (left)
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 200);
}

//motor A backward
void right_backward()
{
      // turn on motor A (Right)
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);
}

//motor B backward
void left_backward()
{
  // turn on motor B (left)
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 200);
}

////////////////////////////////////////////
/////Driving functions//////////////////////
////////////////////////////////////////////
/////Forward, reverse, right, left, still///
////////////////////////////////////////////

void forward()
{
right_forward();
left_forward();
}

void reverse()
{
right_backward();
left_backward();
}

void right()
{
//A back, B forward
right_backward();
left_forward();
}

void left()
{
//B back, A forward
right_forward();
left_backward();
}

void still()
{
right_stop();
left_stop(); 
}


///////////////////////////////////////////////
////////////Responding to obstacle function////
///////////////////////////////////////////////
//////Stop, reverse, and a random turn/////////
///////////////////////////////////////////////
///modulo division on count determines direction
////////////////////////////////////////////////

void respond()
{
  if (count % 2 == false)
  {
     still();
    delay(750);
    reverse();
    delay(500);
    right();
    delay(300);
  } else {
     still();
    delay(750);
    reverse();
    delay(500);
    left();
    delay(300);
  }
}


//////////////////////////////////////////////
//////////////////////////////////////////////
////////Main drive function///////////////////
//////////////////////////////////////////////

void drive()
{

//get the distance
radar();

  // if the distance above threshold go forward
  if (distance > threshold) {
    forward();
    Serial.print("going forward ");
  }
 
  //If distance less than threshold respond
  else {
   respond();
  Serial.print("responding ");
  }

//added serial readouts for testing and debugging.
}



///////////////////////////////////////////////
///////////Main loop function//////////////////
///////////////////////////////////////////////

void loop() {
// put your main code here, to run repeatedly:

count +=1;
drive();
//delay to steady the signal
delay(10);

}