Changing modes in a robot car

hi, so I am a beginner in Arduino programming and I don't have much experience in programming recently I made a obstacle avoiding car and a Bluetooth controlled car and now I want to make a car where I can have those two features and I can change modes from obstacle avoiding and Bluetooth controlling just by pressing a button from my phone I have trying to do it for a few days but I was unsuccessful somebody please tell me how I can add this feature

Welcome to the forum

Please post your best attempt at combining the 2 sketches, using code tags when you do, and describe the problems that you have had

I don't normally complain about grammar, but you really need to use punctuation to allow your text to be read normally...

1 Like

And once you have actual sentences, it's customary to capitalize the first word of each one.

1 Like
 
#include <AFMotor.h>
#include <SoftwareSerial.h>
#include <NewPing.h>
#include <Servo.h> 


#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

SoftwareSerial bluetoothSerial(0, 1); // RX, TX 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo; 

char command;


boolean goesForward=false;
int distance = 100;
int speedSet = 0;
int test = 33;

void setup()
{
  bluetoothSerial.begin(9600); 
  
  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
  if (bluetoothSerial.available() > 0) {
    command = bluetoothSerial.read();

    Stop();
    
    switch (command) {
      case 'F':
        forward();
        break;
      case 'B':
        back();
        break;
      case 'L':
        left();
        break;
      case 'R':
        right();
        break;
      case 'V':
        test = 33;
        break;
      case 'v':
        test = 34;
        break;
    }
  }
  if(test=34){
    if(distance<=30)
    {
     moveStop();
     delay(400);
     moveBackward();
     delay(300);
         }
    else{}
     }else{
       if(distance<=30)
          {  
            moveStop();
            delay(100);
            moveBackward();
            delay(300);
            moveStop();
            delay(200);
            distanceR = lookRight();
            delay(200);
            distanceL = lookLeft();
            delay(200);
          
            if(distanceR>=distanceL)
            {
              turnRight();
              moveStop();
            }else
            {
              turnLeft();
              moveStop();
            }
           }else
           {
            moveForward(); 
                }}

  
 distance = readPing();
}

int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(0);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 100;
  }
  return cm;
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  } 

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) 
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
  }
}
  

void forward()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD);  //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD);  //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(255); //Define maximum velocity
  motor1.run(FORWARD);  //rotate the motor clockwise
  motor2.setSpeed(255); //Define maximum velocity
  motor2.run(FORWARD);  //rotate the motor clockwise
  motor3.setSpeed(255); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(255); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void Stop()
{
  motor1.setSpeed(0);  //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0);  //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0);  //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0);  //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}

void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);
    motor3.run(FORWARD); 
    motor4.run(FORWARD);     
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) 
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    motor3.setSpeed(speedSet);
    motor4.setSpeed(speedSet);
    delay(5);
   }
  }
}
  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);     
  delay(500);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);  
  motor3.run(FORWARD);
  motor4.run(FORWARD);   
  delay(500);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}  

This is my recent attempt on this project
So I am using an android app called Bluetooth RC Controller. and I have used the horn button to change the modes when I uploaded the code to my Arduino UNO the Bluetooth controlling works fine but when I turn it on the obstacle avoiding mode it doesn't work

Sorry for that

Sorry for that.

This is always true because you use the "=" operator to assign 34 to test, then the expression evaluates to non-zero and triggers the if(...) clause, and then you do a about a second's worth of object avoidance before you loop again and get a chance to read another bluetooth character.

1 Like

Really? A software serial instance on the existing, functional hardware serial pins? Why not just use the existing hardware serial port?

Why? What does this accomplish?

That's a non-blocking delay().

a7

2 Likes

Thanks

your code needs at least 2 sub-functions: one that monitors for commands and a second that runs the robot, presumably autonomously in obstacle avoidance mode.

the obstacle avoidance sub-function is repeatedly invoke depending on a mode flag that can be toggled using a command. when NOT in obstacle avoidance mode, user controlled commands (i.e. "teleop")
are allowed. the mode flag can be used to check for or ignore those commands by calling a 3rd sub-function

it's about organizing the code

but it looks like your obstacle avoidance code, autonomously executes various move commands, is blocking and would need to complete before seeing another command

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.