Hi, so I'm supposed to make an autonomous robot that can go through a simple maze-like an L shape. I have built the Sparkfun inventors kit project 5c. It's my first time trying to code even tho I'm using the sik example code. What I want the robot to do is go forward and do a 90degree turn if it detects an obstacle with the distance sensor and what I've been struggling with is a way for the code to find out if it did a 90-degree turn and it detects an object again to make a 180-degree turn and go forward and resume the original code of if it detects an object take a right turn. It seems simple but when all you have to detect the walls is the distance sensor I can't wrap my brain around it. Any help is greatly appreciated
example code if it helps:
/*
SparkFun Inventor’s Kit
Circuit 5C - Autonomous Robot
This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
Check out the rest of the book at
https://www.sparkfun.com/products/14326
This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
This code is completely free for any use.
View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v41
Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/
//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
const int PWMA = 11; //speed control pin on the motor driver for the right motor
//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10; //speed control pin on the motor driver for the left motor
const int BIN2 = 9; //control pin 2 on the motor driver for the left motor
const int BIN1 = 8; //control pin 1 on the motor driver for the left motor
//distance variables
const int trigPin = 6;
const int echoPin = 5;
int switchPin = 7; //switch to turn the robot on and off
float distance = 0; //variable to store the distance measured by the distance sensor
//robot behaviour variables
int backupTime = 300; //amount of time that the robot will back up when it senses an object
int turnTime = 200; //amount that the robot will turn once it has backed up
/********************************************************************************/
void setup()
{
pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pulses out from the distance sensor
pinMode(echoPin, INPUT); //this pin will sense when the pulses reflect back to the distance sensor
pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped
//set the motor control pins as outputs
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
Serial.begin(9600); //begin serial communication with the computer
Serial.print("To infinity and beyond!"); //test the serial connection
}
/********************************************************************************/
void loop()
{
//DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" in"); // print the units
if (digitalRead(switchPin) == LOW) { //if the on switch is flipped
if (distance < 10) { //if an object is detected
//back up and turn
Serial.print(" ");
Serial.print("BACK!");
//stop for a moment
rightMotor(0);
leftMotor(0);
delay(100);
//back up
rightMotor(-255);
leftMotor(-255);
delay(200);
//take a 90 degree turnish
rightMotor(-255);
leftMotor(255);
delay(500);
//stop for a moment
rightMotor(0);
leftMotor(0);
delay(100);
} else if (distance < 10) {
Serial.print(" ");
Serial.print("180-turn");
//stop for a bit
rightMotor(0);
leftMotor(0);
delay(100);
//do a 180 turn
rightMotor(-255);
leftMotor(255);
delay(1000);
} else { //if no obstacle is detected drive forward
Serial.print(" ");
Serial.print("Moving...");
rightMotor(255);
leftMotor(255);
}
} else { //if the switch is off then stop
//stop the motors
rightMotor(0);
leftMotor(0);
}
delay(100); //wait 50 milliseconds between readings
}
/********************************************************************************/
void rightMotor(int motorSpeed) //function for driving the right motor
{
if (motorSpeed > 0) //if the motor should drive forward (positive speed)
{
digitalWrite(AIN1, HIGH); //set pin 1 to high
digitalWrite(AIN2, LOW); //set pin 2 to low
}
else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, HIGH); //set pin 2 to high
}
else //if the motor should stop
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, LOW); //set pin 2 to low
}
analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
}
/********************************************************************************/
void leftMotor(int motorSpeed) //function for driving the left motor
{
if (motorSpeed > 0) //if the motor should drive forward (positive speed)
{
digitalWrite(BIN1, HIGH); //set pin 1 to high
digitalWrite(BIN2, LOW); //set pin 2 to low
}
else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
{
digitalWrite(BIN1, LOW); //set pin 1 to low
digitalWrite(BIN2, HIGH); //set pin 2 to high
}
else //if the motor should stop
{
digitalWrite(BIN1, LOW); //set pin 1 to low
digitalWrite(BIN2, LOW); //set pin 2 to low
}
analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
}
/********************************************************************************/
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calculatedDistance; //variable to store the distance calculated from the echo time
//send out an ultrasonic pulse that's 10ms long
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
//pulse to bounce back to the sensor
calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
return calculatedDistance; //send back the distance that was calculated
}
Hi, so I'm supposed to make an autonomous robot that can go through a simple maze-like an L shape. I have built the Sparkfun inventors kit project 5c. It's my first time trying to code even tho I'm using the sik example code. What I want the robot to do is go forward and do a 90degree turn if it detects an obstacle with the distance sensor and what I've been struggling with is a way for the code to find out if it did a 90-degree turn and it detects an object again to make a 180-degree turn and go forward and resume the original code of if it detects an object take a right turn. It seems simple but when all you have to detect the walls is the distance sensor I can't wrap my brain around it. Any help is greatly appreciated
example code if it helps:
/*
SparkFun Inventor's Kit
Circuit 5C - Autonomous Robot
This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
Check out the rest of the book at
https://www.sparkfun.com/products/14326
This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
This code is completely free for any use.
View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v41
Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/
//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13; //control pin 1 on the motor driver for the right motor
const int AIN2 = 12; //control pin 2 on the motor driver for the right motor
const int PWMA = 11; //speed control pin on the motor driver for the right motor
//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10; //speed control pin on the motor driver for the left motor
const int BIN2 = 9; //control pin 2 on the motor driver for the left motor
const int BIN1 = 8; //control pin 1 on the motor driver for the left motor
//distance variables
const int trigPin = 6;
const int echoPin = 5;
int switchPin = 7; //switch to turn the robot on and off
float distance = 0; //variable to store the distance measured by the distance sensor
//robot behaviour variables
int backupTime = 300; //amount of time that the robot will back up when it senses an object
int turnTime = 200; //amount that the robot will turn once it has backed up
/********************************************************************************/
void setup()
{
pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pulses out from the distance sensor
pinMode(echoPin, INPUT); //this pin will sense when the pulses reflect back to the distance sensor
pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped
//set the motor control pins as outputs
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
Serial.begin(9600); //begin serial communication with the computer
Serial.print("To infinity and beyond!"); //test the serial connection
}
/********************************************************************************/
void loop()
{
//DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" in"); // print the units
if (digitalRead(switchPin) == LOW) { //if the on switch is flipped
if (distance < 10) { //if an object is detected
//back up and turn
Serial.print(" ");
Serial.print("BACK!");
//stop for a moment
rightMotor(0);
leftMotor(0);
delay(100);
//back up
rightMotor(-255);
leftMotor(-255);
delay(200);
//take a 90 degree turnish
rightMotor(-255);
leftMotor(255);
delay(500);
//stop for a moment
rightMotor(0);
leftMotor(0);
delay(100);
} else if (distance < 10) {
Serial.print(" ");
Serial.print("180-turn");
//stop for a bit
rightMotor(0);
leftMotor(0);
delay(100);
//do a 180 turn
rightMotor(-255);
leftMotor(255);
delay(1000);
} else { //if no obstacle is detected drive forward
Serial.print(" ");
Serial.print("Moving...");
rightMotor(255);
leftMotor(255);
}
} else { //if the switch is off then stop
//stop the motors
rightMotor(0);
leftMotor(0);
}
delay(100); //wait 50 milliseconds between readings
}
/********************************************************************************/
void rightMotor(int motorSpeed) //function for driving the right motor
{
if (motorSpeed > 0) //if the motor should drive forward (positive speed)
{
digitalWrite(AIN1, HIGH); //set pin 1 to high
digitalWrite(AIN2, LOW); //set pin 2 to low
}
else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, HIGH); //set pin 2 to high
}
else //if the motor should stop
{
digitalWrite(AIN1, LOW); //set pin 1 to low
digitalWrite(AIN2, LOW); //set pin 2 to low
}
analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
}
/********************************************************************************/
void leftMotor(int motorSpeed) //function for driving the left motor
{
if (motorSpeed > 0) //if the motor should drive forward (positive speed)
{
digitalWrite(BIN1, HIGH); //set pin 1 to high
digitalWrite(BIN2, LOW); //set pin 2 to low
}
else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
{
digitalWrite(BIN1, LOW); //set pin 1 to low
digitalWrite(BIN2, HIGH); //set pin 2 to high
}
else //if the motor should stop
{
digitalWrite(BIN1, LOW); //set pin 1 to low
digitalWrite(BIN2, LOW); //set pin 2 to low
}
analogWrite(PWMB, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed
}
/********************************************************************************/
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calculatedDistance; //variable to store the distance calculated from the echo time
//send out an ultrasonic pulse that's 10ms long
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
//pulse to bounce back to the sensor
calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
return calculatedDistance; //send back the distance that was calculated
}
You need to explain the parts of the kit that can: (1) tell your controller that the robot is moving and not turning to one side, (2) tell you that an object is in front of the robot, (3) that the robot is moving and has turned 90 degrees.
I am not going to go look at a book. Please use your own words to describe what you are doing and what problem you have. Include any code you are using to do the project. Also, a block diagram showing the components you are using, including the power, and show their connections. A schematic can come later.
Paul_KD7HB:
You need to explain the parts of the kit that can: (1) tell your controller that the robot is moving and not turning to one side, (2) tell you that an object is in front of the robot, (3) that the robot is moving and has turned 90 degrees.
I am not going to go look at a book. Please use your own words to describe what you are doing and what problem you have. Include any code you are using to do the project. Also, a block diagram showing the components you are using, including the power, and show their connections. A schematic can come later.
Paul
okay, so what I'm doing is trying to figure out the code so that when the robot detects a wall/object in front it's going to do a right turn. Then if it detects an object/wall again it's going to turn around and move forward. the end goal is to have a robot that can move and avoid objects and get to the other side of the obstacle course by itself. Thank you for any help.
here's the schematic/part list.
okay sorry for that it doesn't have to be an exact 90-degree turn I just need it to turn and when it turns if it detects another object with the distance sensor to make a u-turn and then continue. For example, if there's a wall in front its going to back up turn right and if there's another wall there it's going to take a u-turn and all I have is the distance sensor to make it happen.
The code you posted has everything you need, but your logic is wack. Programs do what they're told, not what you want.
First, this thing:
if (distance < 10) { //if an object is detected
//CODE to TURN 90 DEG
} else if (distance < 10) {
//CODE to U-TURN
}
This shouldn't be on the same level. You want to U-Turn AFTER you turn 90 deg, not at the same time. Even then, the program would only execute the first if and ignore the else if when distance is < 10.
The next problem I see is that you want to READ -> TURN RIGHT -> READ -> U-TURN.
What you're doing is READ -> TURN RIGHT/U-TURN.
Pretend the robot is your little brother and you're trying to explain how to go through the maze, but its a video game. The controller has 2 sticks right motor fwd/back left motor fwd/back.