CAN SOMEONE HELP? Sensor mode and manual mode arduino car with application

Hello,

Thanks for your time on reading this. I have built a robot exactly like this one (Arduino autonomous Robot Project: A DIY obstacle avoiding robot using an SG90 servo - YouTube) + a bluetooth stick HC-06. So i have two codes, the code that lets the sensor(eyes) see where to go automatically and i have another code for the manual steering with an application. I am using the app Bluetooth RC controller.

I am new with coding and i want some help with combining the two codes that i have into 1. I learned a little that if i have a void with a name that name can be used in the case 'R' as example. I am reading everything i can to learn it for 4 hours now but i still can't come to a solution that's why i am asking it here. I tried some more stuff but without success.

This is the code for the Sensor arduino riding automatic:

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 300 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(3, MOTOR12_1KHZ);

Servo myservo;   

boolean goesForward=false;
int distance = 800;
int speedSet = 0;

void setup() {

  myservo.attach(9);  
  myservo.write(66); 
  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;
 delay(40);
 
 if(distance<=20)
 {
  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(29); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(66); 
    return distance;
}

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

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

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

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);      
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(235);
    motor2.setSpeed(225+MAX_SPEED_OFFSET);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(235);
    motor2.setSpeed(225+MAX_SPEED_OFFSET);
    delay(5);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);     
  delay(300);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(FORWARD);     
  delay(300);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
}

And this is the code for the car riding manual with application:

#include <AFMotor.h>
#include <Servo.h> 
//creates two objects to control the terminal 3 and 4 of motor shield 
AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(3, MOTOR12_1KHZ);

char command; 
Servo myservo; 

void setup() 
{       
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
   myservo.attach(9);
  myservo.write(66); 
}

void loop(){
  if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.   
    //Serial.println(command);
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      right();
      break;
    case 'R':
      left();
      break;
    }
  } 
}

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

void back()
{
  motor1.setSpeed(235); 
  motor1.run(BACKWARD); //rotate the motor counterclockwise
  motor2.setSpeed(230); 
  motor2.run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
  motor1.setSpeed(235); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

void right()
{
  motor1.setSpeed(0);
  motor1.run(RELEASE); //turn motor1 off
  motor2.setSpeed(235); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

Btw i found a code of an guy which i tried to apply on my arduino robot(he has the 2 codes in 1). When i touch a button in the application it uses the servo and sensor to look left and right and goes backward and then left or right but it doesnt go forward it just stops after that, it seems that that code doesnt go in a loop it seems. So that would be perfect if it just goes automatic servosensor after i push a button in the application. I have the code from that guy below here (void otoac and case X makes the arduino go automatic with sensors but stops like i explained above)

void otoac()
{
 long duration, distance; 
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 
 if(distance<=24)
 {
  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();
 }

void loop(){
   if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    case 'G':  
      onsol();
      break;
    case 'I':  
      onsag();
      break;
    case 'H':  
      arkasag();
      break;
    case 'J':
      arkasol();
      break;
    case 'W':  
      onac();
      break;
    case 'w':  
      onkapa();
      break;
     case 'X':  
      otoac();
      break;
    case 'x':  
      otokapa();
      break;
    }
  } 
}

I uploaded the whole code of that guy into an attachment because it didn't fit in this text (.ino file)

Thanks for your help!

Greetings,

Gester[/code]

Bluetooth_controlled_obstacle_avoiding_arduino_robot_park_sensor.ino (5.24 KB)

In the future please edit your post when there is a problem instead of double posting, which leads to wasted time due to duplicate effort and forces the moderators to spend time deleting one of them.

Gester1989:
I attached my code into textfiles because i find it ugly to just copy paste it in here.

And I find it ugly to have to download and open .doc files.

You drastically reduce your chances for getting help by doing that.

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment but not as a stupid .doc file, just attach the .ino sketch files.

LOL every day something new n the forum.

Today it's code in .doc files. Never even thought of using a word processor to write code myself. It doesn't even do proper layout and code highlighting, and it's even more messed up and unreadable than when posted in the forum without code tags.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

pert:
In the future please edit your post when there is a problem instead of double posting, which leads to wasted time due to duplicate effort and forces the moderators to spend time deleting one of them.
And I find it ugly to have to download and open .doc files.

You drastically reduce your chances for getting help by doing that.

If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's ok to add it as an attachment but not as a stupid .doc file, just attach the .ino sketch files.

Ooh, i didnt know that i double posted it. I remember that it wouldn't upload so i had to wait 5 minutes. I think something went wrong there. But okey, now i have learned something about posting on a forum. I will edit this post then. Can you guy's help me after i changed the post?

Greetings from Holland (:

wvmarle:
LOL every day something new n the forum.

Today it's code in .doc files. Never even thought of using a word processor to write code myself. It doesn't even do proper layout and code highlighting, and it's even more messed up and unreadable than when posted in the forum without code tags.

Yes i know i know. I am a new one okay haha. And i never post something on a forum until now. But i changed it now in the way its supposed to be. Can you help me with the codes if you have time?

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Heey! Thanks for your post, it really helped alot. Can you try to look at my post again if you have time?

Greetings,

Gester

Does someone has an answer please?