Issue with right IR sensor on Bluebot Car
Moderators: kgudger, bfinio, MadelineB, Moderators
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Issue with right IR sensor on Bluebot Car
The Right sensor does not seem to be detecting anything. We did run serial monitor and the Left IR changes value when approaching line - Right stays around 950 and never changes when placed on the line. We have switched the right and the left sensor to verify the sensor isn't bad and it failed on that as well. The right IR is A1 and and left is A0 - does this mean we have a bad connection?
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Hi - we can help you troubleshoot if you can post pictures of your breadboard. The pictures should be clear, well-lit, and in-focus, from a top-down view so we can see where wires are connected on the breadboard. If some connections are obscured by wires or tall parts like the MOSFETs, you may need to upload multiple photos from slightly different angles.
Also, can you clarify exactly what you mean by "switched the right and left sensors...and it failed on that as well"? e.g. did you unplug the sensors from the jumper wires and swap them? Or did you keep the sensors plugged into their respective wires, and swap the wires going to A0 and A1?
Also, can you clarify exactly what you mean by "switched the right and left sensors...and it failed on that as well"? e.g. did you unplug the sensors from the jumper wires and swap them? Or did you keep the sensors plugged into their respective wires, and swap the wires going to A0 and A1?
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
Changed out wires on breadboard and used serial monitor to analyze readings. We did not change out the A0 and A1. Readings on sensor fluctuated as expected on the left sensor but stayed at a reading of 950 on right sensor. Switching the wires made the right sensor work and then not the left.
Second Issue :
We also have more issues now where both motors will not work at the same speed-left was fast right wheel very slow. We have tested by changing the speed on both sides in the code and monitoring individually. We switched the wires for the motors and the right was then fast and left was very slow. We are getting quite frustrated. We have been working on this for weeks and the project is due when we return from Christmas break. At this point we are very fair behind schedule.
Second Issue :
We also have more issues now where both motors will not work at the same speed-left was fast right wheel very slow. We have tested by changing the speed on both sides in the code and monitoring individually. We switched the wires for the motors and the right was then fast and left was very slow. We are getting quite frustrated. We have been working on this for weeks and the project is due when we return from Christmas break. At this point we are very fair behind schedule.
- Attachments
-
- IMG_20241215_192444.jpg (774.71 KiB) Viewed 7433 times
-
- IMG_20241215_190113 (1).jpg (782.64 KiB) Viewed 7433 times
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Hi - we were able to access your photos. Separate issues:
1) Your H-bridge is upside-down. Look closely at Figure 3 in the project procedure https://www.sciencebuddies.org/science- ... #procedure and note that the semi-circular notch in the H-bridge chip is facing toward the top of the breadboard. In your photos it's facing down. This is likely causing the issues with your motors.
2) I cannot diagnose from these images whether there is anything wrong with one of your sensors. Based on what you've said so far, you have been taking the correct steps to identify a bad sensor by swapping them. Instead of swapping the wires on the breadboard, you could unplug the sensors from the female ends of the jumper wires and try swapping them there - that way you know the breadboard connections from the sensor that was working are still good, and you can confirm if one of the sensors is bad. It's possible that you may have burned out one of the IR LEDs in a sensor from a previous incorrect connection. If that is the case, please contact Home Science Tools and they may be able to send you a replacement sensor (Science Buddies does not ship the kits or replacement parts directly).
We're sorry that you're experiencing frustration with this project - could you let us know if you and/or the students you're working with have previous experience with Arduino and using a breadboard, or is this the first Arduino project you've tried?
Thanks,
Ben
1) Your H-bridge is upside-down. Look closely at Figure 3 in the project procedure https://www.sciencebuddies.org/science- ... #procedure and note that the semi-circular notch in the H-bridge chip is facing toward the top of the breadboard. In your photos it's facing down. This is likely causing the issues with your motors.
2) I cannot diagnose from these images whether there is anything wrong with one of your sensors. Based on what you've said so far, you have been taking the correct steps to identify a bad sensor by swapping them. Instead of swapping the wires on the breadboard, you could unplug the sensors from the female ends of the jumper wires and try swapping them there - that way you know the breadboard connections from the sensor that was working are still good, and you can confirm if one of the sensors is bad. It's possible that you may have burned out one of the IR LEDs in a sensor from a previous incorrect connection. If that is the case, please contact Home Science Tools and they may be able to send you a replacement sensor (Science Buddies does not ship the kits or replacement parts directly).
We're sorry that you're experiencing frustration with this project - could you let us know if you and/or the students you're working with have previous experience with Arduino and using a breadboard, or is this the first Arduino project you've tried?
Thanks,
Ben
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
We had removed the H bridge and reconnected it right before taking the pictures and probably put it back on incorrectly. We have changed it back to what you suggested but issue is not corrected - left wheel is rotating fast when it has resistance it continues to rotate- the right wheel is slow and when it has resistance it stops. Result -car spins in circles.
We are going to test the IR tonight.
We are going to test the IR tonight.
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Ok, for the motors:
1. You can confirm that there is nothing physically wrong with the motors by connecting them directly to 6V from the 4xAA battery pack using the breadboard power buses, bypassing the H-bridge circuit and Arduino entirely. If both motors spin at full speed when they are connected directly to the battery, then there is nothing physically wrong with them and the issue is somewhere in the code or H-bridge circuit.
2. Assuming both motors work when you try step 1, a common cause for a wheel spinning very slowly is that the pin controlling it is accidentally set as an input instead of an output. Double and triple-check all your pin settings in your code and Arduino-to-H-bridge connections to make sure that all pins controlling the H bridge are set as outputs, not inputs.
When you have time could you let us know what prior level of Arduino/breadboard experience the students working on this have, if any? That will help us further calibrate the support and warnings about required prerequisites for the project.
Thanks,
Ben
1. You can confirm that there is nothing physically wrong with the motors by connecting them directly to 6V from the 4xAA battery pack using the breadboard power buses, bypassing the H-bridge circuit and Arduino entirely. If both motors spin at full speed when they are connected directly to the battery, then there is nothing physically wrong with them and the issue is somewhere in the code or H-bridge circuit.
2. Assuming both motors work when you try step 1, a common cause for a wheel spinning very slowly is that the pin controlling it is accidentally set as an input instead of an output. Double and triple-check all your pin settings in your code and Arduino-to-H-bridge connections to make sure that all pins controlling the H bridge are set as outputs, not inputs.
When you have time could you let us know what prior level of Arduino/breadboard experience the students working on this have, if any? That will help us further calibrate the support and warnings about required prerequisites for the project.
Thanks,
Ben
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
No prior experience. My student searched on this website and found this project not realizing the level of knowledge required.
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
We have tried motors directly connected to 6V using the breadboard power buses, bypassing the H-bridge circuit and Arduino entirely. Motors are running fast this way but left motor is visibly faster than the right and car doesn't drive straight it veers to the right.
We are going to verify output per your suggestion and update post after that.
We are going to verify output per your suggestion and update post after that.
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Hi - in my experience these robots almost never drive *perfectly* straight, the motors are never perfectly symmetric and there will always be some drift to the left or right. You can calibrate for this by running the motors at two different speeds using analogWrite (i.e. run the motor that's spinning faster at a lower speed) to make the robot go straight. However, slowly drifting to one side is different than one motor barely moving at all and the robot just spinning in tight circles, in which case there might actually be something wrong with one of the motors - but I have never seen that happen.
FYI I will be on vacation starting tomorrow (Saturday) through New Year's and not checking the forums - is your project due immediately upon return from break?
FYI I will be on vacation starting tomorrow (Saturday) through New Year's and not checking the forums - is your project due immediately upon return from break?
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
The project is due January 10th. I think we have multiple issues but I do not believe it is the code. I can send the code we are using if I can do so not on this public forum. I am worried that we have 1 or more parts with an issue. I just do not know which ones to replace.
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Hi -
1) Is there a reason you don't want to post the code publicly? The starter code for the project is publicly available anyway, so there is no concern about this on our end. If you have a concern (e.g. using the robots in a competition with other teams of students who may see this post) then you can email us at [email protected]. Otherwise it is easier to post the code here to keep everything in one place.
2) It is pretty rare for multiple individual parts to be broken; it is far more likely that they are connected wrong or that there's an issue with the code. But without more details about what has changed since your previous posts, we can't help you troubleshoot potential issues with specific parts. If you can provide a detailed description of what's wrong and more photos of the circuit if needed, we can help troubleshoot.
Thanks,
Ben
1) Is there a reason you don't want to post the code publicly? The starter code for the project is publicly available anyway, so there is no concern about this on our end. If you have a concern (e.g. using the robots in a competition with other teams of students who may see this post) then you can email us at [email protected]. Otherwise it is easier to post the code here to keep everything in one place.
2) It is pretty rare for multiple individual parts to be broken; it is far more likely that they are connected wrong or that there's an issue with the code. But without more details about what has changed since your previous posts, we can't help you troubleshoot potential issues with specific parts. If you can provide a detailed description of what's wrong and more photos of the circuit if needed, we can help troubleshoot.
Thanks,
Ben
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Issue with right IR sensor on BlueBot Car
Code uploaded to car currently:
// declare variables for motor control pins
const int leftMotorForwardPin = 5;
const int leftMotorBackwardPin = 4;
const int rightMotorForwardPin = 3;
const int rightMotorBackwardPin = 2;
const int leftMotorSpeedPin = 9;
const int rightMotorSpeedPin = 10;
// declare variables for sensor pins for ultrasonic sensor
const int triggerPin = 11;
const int echoPin = 7;
// define pins for LEDs
const int leftLEDpin = 6;
const int rightLEDpin = 8;
// define constants for comparing sensor readings
const int leftIRthreshold = 200; // threshold for left IR sensor to detect line
const int rightIRthreshold = 200; // threshold for right IR sensor to detect line
const int brakingDistance = 8; // desired automatic stopping distance in centimeters
// define non-constant variables used in the program
int leftSpeed = 0; // left motor speed
int rightSpeed = 0; // right motor speed
int turnspeed1 = 80; // speed of outer wheel when turning
int turnspeed2 = 50; // speed of inner wheel when turning
int straightspeed = 80; // speed of both motors when driving straight
int leftIR = 0; // left IR sensor reading
int rightIR = 0; // right IR sensor reading
long distance = 0; // distance measured by the ultrasonic sensor in centimeters
// Define variables for millis-based timing
unsigned long previousMillis = 0;
const long interval = 100; // Interval time in milliseconds (adjust as needed)
// Threshold for ultrasonic distance detection
const long threshold = 15; // Example threshold for obstacle avoidance
void setup() { //setup code that only runs once
// set motor control pins as outputs
pinMode(leftMotorForwardPin, OUTPUT);
pinMode(leftMotorBackwardPin, OUTPUT);
pinMode(rightMotorForwardPin, OUTPUT);
pinMode(rightMotorBackwardPin, OUTPUT);
pinMode(leftMotorSpeedPin, OUTPUT);
pinMode(rightMotorSpeedPin, OUTPUT);
pinMode(leftLEDpin, OUTPUT);
pinMode(rightLEDpin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
// set H bridge pins to make car drive forward
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to update the motor control
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time you updated
// Get sensor readings
leftIR = analogRead(A0); // read left IR sensor
rightIR = analogRead(A1); // read right IR sensor
distance = readUltrasonic(); // read ultrasonic sensor and return distance in cm
// Print sensor readings to Serial Monitor
Serial.print("Left IR: ");
Serial.println(leftIR);
Serial.print("Right IR: ");
Serial.println(rightIR);
Serial.print("Ultrasonic Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Left motor: ");
Serial.println(leftSpeed);
Serial.print("Right Motor: ");
Serial.println(rightSpeed);
// Obstacle avoidance logic
if (distance < threshold) {
stopDriving(1000); // Stop if there's an obstacle within the threshold distance
driveBackward(500); // Drive backward if an obstacle is detected
turnRight(500); // Turn to avoid obstacle
return; // Skip line-following logic when obstacle is detected
}
// Line following logic based on IR sensors
if ((leftIR > leftIRthreshold) && (rightIR > rightIRthreshold)) {
// Move forward with both motors at max speed
driveForward(0, 50, 50);
}
else if ((leftIR < leftIRthreshold) && (rightIR > rightIRthreshold)) {
// Turn right if only the left sensor detects the line
turnRight(75); // Shorter turn time for smoother movement
}
else if ((leftIR > leftIRthreshold) && (rightIR < rightIRthreshold)) {
// Turn left if only the right sensor detects the line
driveLeft(75); // Shorter turn time for smoother movement
}
else {
// Instead of stopping abruptly, try to adjust with a small movement
driveForward(0, 75, 75); // Slow down slightly to regain the line
}
// Set motor speeds
analogWrite(leftMotorSpeedPin, leftSpeed);
analogWrite(rightMotorSpeedPin, rightSpeed);
}
}
// Function to read distance from ultrasonic sensor
long readUltrasonic() {
// Generate trigger pulse
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);
// Measure the return pulse
long duration = pulseIn(echoPin, HIGH);
// Convert duration to distance
long distance = duration / 58.2; // Calculate distance in cm
return distance;
}
// Function to make car drive forward
void driveForward(int delay_time, int leftSpeed, int rightSpeed) {
analogWrite(leftMotorSpeedPin, leftSpeed);
analogWrite(rightMotorSpeedPin, rightSpeed);
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
digitalWrite(leftLEDpin, HIGH); // Turn on left LED when moving forward
digitalWrite(rightLEDpin, HIGH); // Turn on right LED when moving forward
delay(delay_time);
digitalWrite(leftLEDpin, LOW); // Turn off left LED after moving
digitalWrite(rightLEDpin, LOW); // Turn off right LED after moving
}
void turnRight(int delay_time) {
analogWrite(leftMotorSpeedPin, turnspeed2); // Inner wheel
analogWrite(rightMotorSpeedPin, turnspeed1); // Outer wheel
digitalWrite(leftMotorForwardPin, LOW); // Stop left motor (inner wheel)
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH); // Move right motor (outer wheel)
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
void driveLeft(int delay_time) {
analogWrite(leftMotorSpeedPin, turnspeed1); // Outer wheel
analogWrite(rightMotorSpeedPin, turnspeed2); // Inner wheel
digitalWrite(leftMotorForwardPin, HIGH); // Move left motor (outer wheel)
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW); // Stop right motor (inner wheel)
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
// Function to stop the car
void stopDriving(int delay_time) {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
// Function to drive backward
void driveBackward(int delay_time) {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, HIGH);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, HIGH);
delay(delay_time);
}
// declare variables for motor control pins
const int leftMotorForwardPin = 5;
const int leftMotorBackwardPin = 4;
const int rightMotorForwardPin = 3;
const int rightMotorBackwardPin = 2;
const int leftMotorSpeedPin = 9;
const int rightMotorSpeedPin = 10;
// declare variables for sensor pins for ultrasonic sensor
const int triggerPin = 11;
const int echoPin = 7;
// define pins for LEDs
const int leftLEDpin = 6;
const int rightLEDpin = 8;
// define constants for comparing sensor readings
const int leftIRthreshold = 200; // threshold for left IR sensor to detect line
const int rightIRthreshold = 200; // threshold for right IR sensor to detect line
const int brakingDistance = 8; // desired automatic stopping distance in centimeters
// define non-constant variables used in the program
int leftSpeed = 0; // left motor speed
int rightSpeed = 0; // right motor speed
int turnspeed1 = 80; // speed of outer wheel when turning
int turnspeed2 = 50; // speed of inner wheel when turning
int straightspeed = 80; // speed of both motors when driving straight
int leftIR = 0; // left IR sensor reading
int rightIR = 0; // right IR sensor reading
long distance = 0; // distance measured by the ultrasonic sensor in centimeters
// Define variables for millis-based timing
unsigned long previousMillis = 0;
const long interval = 100; // Interval time in milliseconds (adjust as needed)
// Threshold for ultrasonic distance detection
const long threshold = 15; // Example threshold for obstacle avoidance
void setup() { //setup code that only runs once
// set motor control pins as outputs
pinMode(leftMotorForwardPin, OUTPUT);
pinMode(leftMotorBackwardPin, OUTPUT);
pinMode(rightMotorForwardPin, OUTPUT);
pinMode(rightMotorBackwardPin, OUTPUT);
pinMode(leftMotorSpeedPin, OUTPUT);
pinMode(rightMotorSpeedPin, OUTPUT);
pinMode(leftLEDpin, OUTPUT);
pinMode(rightLEDpin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
// set H bridge pins to make car drive forward
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to update the motor control
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Save the last time you updated
// Get sensor readings
leftIR = analogRead(A0); // read left IR sensor
rightIR = analogRead(A1); // read right IR sensor
distance = readUltrasonic(); // read ultrasonic sensor and return distance in cm
// Print sensor readings to Serial Monitor
Serial.print("Left IR: ");
Serial.println(leftIR);
Serial.print("Right IR: ");
Serial.println(rightIR);
Serial.print("Ultrasonic Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Left motor: ");
Serial.println(leftSpeed);
Serial.print("Right Motor: ");
Serial.println(rightSpeed);
// Obstacle avoidance logic
if (distance < threshold) {
stopDriving(1000); // Stop if there's an obstacle within the threshold distance
driveBackward(500); // Drive backward if an obstacle is detected
turnRight(500); // Turn to avoid obstacle
return; // Skip line-following logic when obstacle is detected
}
// Line following logic based on IR sensors
if ((leftIR > leftIRthreshold) && (rightIR > rightIRthreshold)) {
// Move forward with both motors at max speed
driveForward(0, 50, 50);
}
else if ((leftIR < leftIRthreshold) && (rightIR > rightIRthreshold)) {
// Turn right if only the left sensor detects the line
turnRight(75); // Shorter turn time for smoother movement
}
else if ((leftIR > leftIRthreshold) && (rightIR < rightIRthreshold)) {
// Turn left if only the right sensor detects the line
driveLeft(75); // Shorter turn time for smoother movement
}
else {
// Instead of stopping abruptly, try to adjust with a small movement
driveForward(0, 75, 75); // Slow down slightly to regain the line
}
// Set motor speeds
analogWrite(leftMotorSpeedPin, leftSpeed);
analogWrite(rightMotorSpeedPin, rightSpeed);
}
}
// Function to read distance from ultrasonic sensor
long readUltrasonic() {
// Generate trigger pulse
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);
// Measure the return pulse
long duration = pulseIn(echoPin, HIGH);
// Convert duration to distance
long distance = duration / 58.2; // Calculate distance in cm
return distance;
}
// Function to make car drive forward
void driveForward(int delay_time, int leftSpeed, int rightSpeed) {
analogWrite(leftMotorSpeedPin, leftSpeed);
analogWrite(rightMotorSpeedPin, rightSpeed);
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
digitalWrite(leftLEDpin, HIGH); // Turn on left LED when moving forward
digitalWrite(rightLEDpin, HIGH); // Turn on right LED when moving forward
delay(delay_time);
digitalWrite(leftLEDpin, LOW); // Turn off left LED after moving
digitalWrite(rightLEDpin, LOW); // Turn off right LED after moving
}
void turnRight(int delay_time) {
analogWrite(leftMotorSpeedPin, turnspeed2); // Inner wheel
analogWrite(rightMotorSpeedPin, turnspeed1); // Outer wheel
digitalWrite(leftMotorForwardPin, LOW); // Stop left motor (inner wheel)
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH); // Move right motor (outer wheel)
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
void driveLeft(int delay_time) {
analogWrite(leftMotorSpeedPin, turnspeed1); // Outer wheel
analogWrite(rightMotorSpeedPin, turnspeed2); // Inner wheel
digitalWrite(leftMotorForwardPin, HIGH); // Move left motor (outer wheel)
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW); // Stop right motor (inner wheel)
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
// Function to stop the car
void stopDriving(int delay_time) {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, LOW);
delay(delay_time);
}
// Function to drive backward
void driveBackward(int delay_time) {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, HIGH);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, HIGH);
delay(delay_time);
}
Last edited by thfine67 on Sun Jan 05, 2025 1:24 pm, edited 2 times in total.
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Stuck on self driving car project with Aurdino
Car is spinning in circles. Ultrasonic sensor is stopping when obstacle is placed in front of it and backs up slightly before continuing in circles once obstacle is removed. This project is due in 2 days for a competitive science fair. We really hope to at least have this do the basics which is drive around the roadway we built.
Moderator note: I merged this and your last post into the thread "Issue with right IR sensor Bluebot Car" so Ben, the expert on this, will see these two new posts. Thank you for your patience and best of luck at the fair.
Madeline
Moderator note: I merged this and your last post into the thread "Issue with right IR sensor Bluebot Car" so Ben, the expert on this, will see these two new posts. Thank you for your patience and best of luck at the fair.
Madeline
-
thfine67
- Posts: 9
- Joined: Thu Dec 05, 2024 12:44 pm
- Occupation: Parent
Re: Issue with right IR sensor on Bluebot Car
Serial Monitor reading
15:35:25.403 -> Left IR: 591
15:35:25.403 -> Right IR: 246
15:35:25.403 -> Ultrasonic Distance: 812 cm
15:35:25.403 -> Left motor: 100
15:35:25.403 -> Right Motor: 0
15:35:25.496 -> Left IR: 582
15:35:25.496 -> Right IR: 224
15:35:25.496 -> Ultrasonic Distance: 812 cm
15:35:25.496 -> Left motor: 100
15:35:25.496 -> Right Motor: 0
15:35:25.403 -> Left IR: 591
15:35:25.403 -> Right IR: 246
15:35:25.403 -> Ultrasonic Distance: 812 cm
15:35:25.403 -> Left motor: 100
15:35:25.403 -> Right Motor: 0
15:35:25.496 -> Left IR: 582
15:35:25.496 -> Right IR: 224
15:35:25.496 -> Ultrasonic Distance: 812 cm
15:35:25.496 -> Left motor: 100
15:35:25.496 -> Right Motor: 0
-
bfinio
- Expert
- Posts: 964
- Joined: Mon Aug 12, 2013 2:41 pm
- Occupation: Lead Staff Scientist, Science Buddies
- Project Question: Expert
- Project Due Date: n/a
- Project Status: Not applicable
Re: Issue with right IR sensor on Bluebot Car
Hi - I'm looking at your code and I'm confused about something - you're printing out the leftSpeed and Rightspeed variables:
Serial.print("Left motor: ");
Serial.println(leftSpeed);
Serial.print("Right Motor: ");
Serial.println(rightSpeed);
which are set as constants at the beginning of the program:
int leftSpeed = 0; // left motor speed
int rightSpeed = 0; // right motor speed
I can't see any other lines of code that ever change those variables. But in the example serial output you pasted in, the leftSpeed is showing as 100 somehow:
15:35:25.403 -> Left motor: 100
15:35:25.403 -> Right Motor: 0
The sensor readings that are printing, 591 and 246, are both above the thresholds you have set (200)
which should make this condition true:
if ((leftIR > leftIRthreshold) && (rightIR > rightIRthreshold))
which should then call your driveForward(0, 50, 50); function.
Looking at that function - you *might* be running into a problem with local and global variables getting mixed up. You're declaring leftSpeed and rightSpeed as global variables at the beginning of your program, but then you're also using them as the local variables in your driveForward function - I'm not sure if this could cause issues with overwriting the global variable values, but it's safest to avoid using the same variable name in both places. The local variables in your driveForward function are just placeholders for whatever numbers you use as arguments when you call the function, e.g. you could write:
void driveForward(int X, Y, Z){
analogWrite(leftMotorSpeedPin, Y);
analogWrite(rightMotorSpeedPin, Z);
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
digitalWrite(leftLEDpin, HIGH); // Turn on left LED when moving forward
digitalWrite(rightLEDpin, HIGH); // Turn on right LED when moving forward
delay(X);
}
then when you call the function with
driveForward(0, 50, 50);
locally in the driveForward function, X will be 0, Y will be 50, and Z will be 50, and this should avoid any potential issues with overwriting the global variable values, which *might* be what's happening.
That is just a guess on my part, that this may actually be a software issue with a conflict between global and local variables sharing names, and not a hardware issue. You can debug this by unplugging your motors so you don't have to worry about them spinning, and just holding the sensors over the line while looking at the serial monitor, and making sure you're getting the correct output. *However* you will need to change the output of your serial print statements to make sense - the example code has lines to print the left and right speed variables, but your code never changes those variables, so in theory (after fixing the local/global issue), it would still just print constants the entire time. You could insert serial print statements into each of your functions, e.g. "driving forward", "turning left" etc so you can see which function is being called.
Sorry for the frustration - again, this is an advanced project; most of our beginner Arduino projects don't involve functions like this.
Serial.print("Left motor: ");
Serial.println(leftSpeed);
Serial.print("Right Motor: ");
Serial.println(rightSpeed);
which are set as constants at the beginning of the program:
int leftSpeed = 0; // left motor speed
int rightSpeed = 0; // right motor speed
I can't see any other lines of code that ever change those variables. But in the example serial output you pasted in, the leftSpeed is showing as 100 somehow:
15:35:25.403 -> Left motor: 100
15:35:25.403 -> Right Motor: 0
The sensor readings that are printing, 591 and 246, are both above the thresholds you have set (200)
which should make this condition true:
if ((leftIR > leftIRthreshold) && (rightIR > rightIRthreshold))
which should then call your driveForward(0, 50, 50); function.
Looking at that function - you *might* be running into a problem with local and global variables getting mixed up. You're declaring leftSpeed and rightSpeed as global variables at the beginning of your program, but then you're also using them as the local variables in your driveForward function - I'm not sure if this could cause issues with overwriting the global variable values, but it's safest to avoid using the same variable name in both places. The local variables in your driveForward function are just placeholders for whatever numbers you use as arguments when you call the function, e.g. you could write:
void driveForward(int X, Y, Z){
analogWrite(leftMotorSpeedPin, Y);
analogWrite(rightMotorSpeedPin, Z);
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
digitalWrite(leftLEDpin, HIGH); // Turn on left LED when moving forward
digitalWrite(rightLEDpin, HIGH); // Turn on right LED when moving forward
delay(X);
}
then when you call the function with
driveForward(0, 50, 50);
locally in the driveForward function, X will be 0, Y will be 50, and Z will be 50, and this should avoid any potential issues with overwriting the global variable values, which *might* be what's happening.
That is just a guess on my part, that this may actually be a software issue with a conflict between global and local variables sharing names, and not a hardware issue. You can debug this by unplugging your motors so you don't have to worry about them spinning, and just holding the sensors over the line while looking at the serial monitor, and making sure you're getting the correct output. *However* you will need to change the output of your serial print statements to make sense - the example code has lines to print the left and right speed variables, but your code never changes those variables, so in theory (after fixing the local/global issue), it would still just print constants the entire time. You could insert serial print statements into each of your functions, e.g. "driving forward", "turning left" etc so you can see which function is being called.
Sorry for the frustration - again, this is an advanced project; most of our beginner Arduino projects don't involve functions like this.

