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)