I'm trying to make an obstacle avoiding robot that turns on and off using bluetooth.
The robot moves forward when turns on but it doesn't want to turn on. I can't really figure out what's going on.
I'm using an Adafruit Motor Shield v2.3
Arduino Uno
6v power for motors
9v Power for arduino
Maybe that info could be of assistance.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
int maximumRange = 25; // Maximum range needed
long duration, distance; // Duration used to calculate distance
char val; // variable to receive data from the serial port
void setup()
{
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT); // set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT); // set the echo pin to input (recieve sound waves)
AFMS.begin();
AFMS.getMotor(1)->setSpeed(150); //set the speed of the motors, between 0-255
AFMS.getMotor(2)->setSpeed(150);
}
void loop()
{
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2; // convert the distance to centimeters.
if ( Serial.available() )
{
val = Serial.read(); // read it and store it in 'val'
if ( val == '0' ) // if '0' was received robot is switched off
{
AFMS.getMotor(1)->run(RELEASE);
AFMS.getMotor(2)->run(RELEASE);
}
else if ( val == '1' ) // if '1' was received robot turns on.
{
if (distance > maximumRange) // if there's an obstacle 25 centimers, ahead, do the following:
{
AFMS.getMotor(1)->run(FORWARD); // Turn as long as there's an obstacle ahead.
AFMS.getMotor(2)->run(BACKWARD);
}
else {
AFMS.getMotor(1)->run(FORWARD); // if there's no obstacle ahead, Go Forward!
AFMS.getMotor(2)->run(FORWARD);
}
}
}
}
distance = duration / 58.2; // convert the distance to centimeters.
distance and duration are both integer types. What do you think this calculation does with only integers?
if (distance > maximumRange) // if there's an obstacle 25 centimers, ahead, do the following:
You mean don't do the following?
Why is the whole thing wrapped in Serial.available()? It only runs forward or turns for a few microseconds and then waits for the next serial character.
I was told it converts the integer into centimeters. Also, it worked fine without the bluetooth off/on switch. Here's the code before I added a bluetooth.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
int maximumRange = 20; // Maximum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
AFMS.begin();
AFMS.getMotor(1)->setSpeed(135); //set the speed of the motors, between 0-255
AFMS.getMotor(2)->setSpeed(135);
}
void loop() {
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2; // convert the distance to centimeters.
if (distance > maximumRange) { /*if there's an obstacle 25 centimers, ahead, do the following: */
AFMS.getMotor(1)->run(FORWARD); // Turn as long as there's an obstacle ahead.
AFMS.getMotor(2)->run(BACKWARD);
}
else {
AFMS.getMotor(1)->run(FORWARD); //if there's no obstacle ahead, Go Forward!
AFMS.getMotor(2)->run(FORWARD);
}
}
For my project, I have to turn it on and of remotely via bluetooth.
Exactly. The earlier version doesn't have to wait for there to be a serial character until it applies the rest of the movement logic.
Use the serial character to set a variable such as "run" or "movementEnabled". Then the main loop can apply its movement logic whenever that variable is true. At the end of the loop (or the beginning) check if there's a serial character available and decide if you need to change movementEnabled.