Go Down

Topic: Obstacle avoiding robot bluetooth problem (Read 126 times) previous topic - next topic

Empty

Nov 26, 2016, 02:29 pm Last Edit: Nov 26, 2016, 02:35 pm by Empty
Hi again.
After failing with a RC car, I just did the simple arduino robot with the usual vehicle kit.
It all works, but I want to try too add bluetooth commands. The basic commands as "forward", "right" and so on, works normally, but i want to send a command that makes the robot go into the "obstacle avoiding state", and go on its own.

Heres the code
Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial BT(5,6);
#define trigPinL 2
#define echoPinL 7

#define trigPinR 10//  
#define echoPinR 4

#define LeftMotor 12
#define RightMotor 13

#define LeftBrakes 9
#define RightBrakes 8
#define LeftSpeed 3
#define RightSpeed 11
 
void setup() {
  Serial.begin(9600);
  Serial.println("Motor test!");
  
  pinMode(trigPinL, OUTPUT);//
  pinMode(echoPinL, INPUT);//

  pinMode(trigPinR, OUTPUT);//
  pinMode(echoPinR, INPUT);// s

  pinMode(LeftMotor, OUTPUT);
  pinMode(RightMotor, OUTPUT);

  pinMode(LeftBrakes, OUTPUT);
  pinMode(RightBrakes, OUTPUT);

  digitalWrite(LeftBrakes, LOW);
  digitalWrite(RightBrakes, LOW);
  
/*
  
*/
  BT.begin(9600);
}
 char a;
void loop() {  

  
  
  
  
  
  

 if (BT.available())

{
  a=(BT.read());

  
  if (a=='F') // forward
  {

   digitalWrite(LeftBrakes, LOW); //
   digitalWrite(RightBrakes, LOW); // P
  
   digitalWrite(LeftMotor, HIGH);
   analogWrite(LeftSpeed, 120);

   digitalWrite(RightMotor, HIGH);
   analogWrite(RightSpeed, 120);
  }

  if (a=='S')
  {
    digitalWrite(LeftBrakes, HIGH); //
    digitalWrite(RightBrakes, HIGH); //
  
   analogWrite(LeftSpeed, 0);

  
   analogWrite(RightSpeed, 0);
  }

  if (a=='L') //i prieki kairen
  {
    digitalWrite(LeftBrakes, LOW); //
    digitalWrite(RightBrakes, LOW); //
    
    digitalWrite(LeftMotor, HIGH);
    analogWrite(LeftSpeed, 120);

    digitalWrite(RightMotor, LOW);
    analogWrite(RightSpeed, 120);
  }

  if(a=='R')
  {

    digitalWrite(LeftBrakes, LOW); //
    digitalWrite(RightBrakes, LOW); //
    
    digitalWrite(LeftMotor, LOW);
    analogWrite(LeftSpeed, 120);

    digitalWrite(RightMotor, HIGH);
    analogWrite(RightSpeed, 120);
  }

  if(a=='B') //atbuliniu
  {

    digitalWrite(LeftBrakes, LOW); //
    digitalWrite(RightBrakes, LOW); //
    
   digitalWrite(LeftMotor, LOW);
   analogWrite(LeftSpeed, 120);

   digitalWrite(RightMotor, LOW);
   analogWrite(RightSpeed, 120);
  }


  if(a=='C')
{
    digitalWrite(LeftBrakes, LOW); //
    digitalWrite(RightBrakes, LOW); //

 long durationL, distanceL, durationR, distanceR; // start the scan

  
  digitalWrite(trigPinL, LOW);  
  delayMicroseconds(2);
  digitalWrite(trigPinL, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinL, LOW);
  durationL = pulseIn(echoPinL, HIGH);
  distanceL = (durationL/2) / 29.1;//

  
  digitalWrite(trigPinR, LOW);  
  delayMicroseconds(2); //
  digitalWrite(trigPinR, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinR, LOW);
  durationR = pulseIn(echoPinR, HIGH);
  distanceR = (durationR/2) / 29.1;
  

  
  if (distanceL > 25 && distanceR > 25)
  {
   digitalWrite(LeftMotor, HIGH);
   analogWrite(LeftSpeed, 120);

   digitalWrite(RightMotor, HIGH);
   analogWrite(RightSpeed, 120);
  }

  else if (distanceL>25 && distanceR < 25)
  {
    digitalWrite(LeftMotor, LOW);
    analogWrite(LeftSpeed, 120);

    digitalWrite(RightMotor, HIGH);
    analogWrite(RightSpeed, 120);
  }

  else if (distanceL < 25 && distanceR > 25)
  {
    digitalWrite(LeftMotor, HIGH);
    analogWrite(LeftSpeed, 120);

    digitalWrite(RightMotor, LOW);
    analogWrite(RightSpeed, 120);
  }

  else if (distanceL < 25 && distanceR < 25 ) //
  {
   digitalWrite(LeftMotor, HIGH);
   analogWrite(LeftSpeed, 120);

   digitalWrite(RightMotor, HIGH);
   analogWrite(RightSpeed, 120);

   delay(15);
    digitalWrite(LeftMotor, HIGH);
    analogWrite(LeftSpeed, 120);

    digitalWrite(RightMotor, LOW);
    analogWrite(RightSpeed, 120);
  
  }
  
}
}
  
  
  
 
}


Theres a problem that I understand but cant think of a solution.
The problem is, that when I send the command C, which is for the obstacle avoiding, it will scan the distance once, and as a result will only go there, where the result tells him, without scanning further and avoiding other obstacles.
This also happens if i put the distance scan outside the "C" case. It only reads the scan  one time.
Is there any solution to this?
and if not, how do I add a "STOP" function, that stops the wheels till I send the signal, and continues the obstacle avoiding state, when i dont.

PaulS

Code: [Select]
void loop() { 

 
 
 
 
 
 

 if (BT.available())

The ONLY excuse for putting the { on the line with the function/statement that holds water is to save real estate. You look pretty dumb doing that and then wasting real estate with a s**tload of blank lines.

Quote
The problem is, that when I send the command C, which is for the obstacle avoiding, it will scan the distance once, and as a result will only go there, where the result tells him, without scanning further and avoiding other obstacles.
Where do you make the code that deals with the 'c' loop? Nowhere that I can see. So, the fact that it doesn't loop is your fault.

What you probably should be doing is separating the reading of serial data from using the serial data that was read. Let loop() take care of doing the action over and over. Send a different command to change the behavior.

If you want a command to be executed once, add a = ' '; in the body of the block that deals with the command.
The art of getting good answers lies in asking good questions.

Empty

#2
Nov 26, 2016, 03:14 pm Last Edit: Nov 26, 2016, 03:23 pm by Empty
Thank You for the response. Im still new to this, and trying to learn. How do I make the 'c' code loop? What functions I should use for that?

EDIT: never mind, seems to work now. Again, thank You, very appreciated

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy