hi forum. i build avoidance robot using bluephill processor (STM32F103C8) and l298n motor driver and HCSR 04 as range sensor.
Its work when i run seperate code eg. run only L298N, run distance measurement with range sensor only, run servo only or run servo and the range sensor unfortunately get error when i combined those three. thank you in advanced
here is my code
"timn" refer timers within processor
#include <Servo.h> //add Servo Motor library
#include <NewPing.h> //add Ultrasonic sensor library
#define TRIG_PIN PB3 //tim2 Pin A0 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN PB4 //tim3 Pin A1 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 300 // sets maximum useable sensor measuring distance to 300cm
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to meas
Servo servo_motor;
#define ENA PB6 //tim4
#define IN1 PA6 // tim3
#define IN2 PA7 // tim3
#define ENB PB7 //tim4
#define IN3 PB0 //tim3
#define IN4 PB1 //tim3
int FrontDist = 0;
int RightDist = 0;
int LeftDist = 0;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo_motor.attach(PB9);// tim4
}
void loop() {
servo_motor.write(90);
delay(50);
checkFront();
if (FrontDist <= 30){
brake();
servo_motor.write(10); // servo turn right
delay(50);
checkRight();
servo_motor.write(170); // servo turn left
delay(80);
checkLeft();
if (LeftDist > RightDist){
GoLeft();
}
else{
GoRight();
}
}
else
{
Forward();
}
}
void GoRight(){
turnRight();
}
void GoLeft(){
turnLeft();
}
int checkFront(){
unsigned int uS = sonar.ping();
int FrontDist = uS / US_ROUNDTRIP_CM;
delay(50);
return FrontDist;
}
int checkRight(){
unsigned int uS = sonar.ping();
int RightDist = uS / US_ROUNDTRIP_CM;
delay(50);
return RightDist;
}
int checkLeft(){
unsigned int uS = sonar.ping();
int LeftDist = uS / US_ROUNDTRIP_CM;
delay(50);
return LeftDist;
}
void Forward()
{
motorAForward();
motorBForward();
}
void Backward()
{
motorABackward();
motorBBackward();
}
void turnLeft()
{
motorABackward();
motorBForward();
}
void turnRight()
{
motorAForward();
motorBBackward();
}
void coast()
{
motorACoast();
motorBCoast();
}
void brake()
{
motorABrake();
motorBBrake();
}
////////////////////////////////MOTOR A CONTROL
void motorAForward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 75);
}
void motorABackward()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 75);
}
////////////////////////////////MOTOR B CONTROL
void motorBForward()
{
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 75);
}
void motorBBackward()
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 75);
}
//////////////////////////////////////////COASTING AND BREAKING
void motorACoast()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
void motorABrake()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
}
void motorBCoast()
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void motorBBrake()
{
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
}
On every pass through loop(), regardless of what any sensor indicates, set the servo position/speed. Does THAT make sense?
You are overly fond of delay(), aren't you. Do YOU walk down the street, crossing roads, etc. with your eyes closed most of the time? I didn't think so. So, why does your robot try to run blind most of the time?
What do your Serial.print() statements tell you is happening? Why not?
Why do you have three nearly identical functions? The names of the local variables in the functions mean nothing when the function ends. All that matters is the name/address of the variable where you store the result of reading the ping sensor, and that does not require three different functions.
@PaulS i try to simplified my code but still with the same issue
actually i dont know how to debug using Serial.print();
if you dont mind would you like to show how and where in the code i have to write Serial.print();
Also, keep in mind that whiles many BluePills come with 128K of flash, they only have 20K of RAM. Once the 20K of RAM limit is reached the BluePill address counter will roll over and, with no warning, the wonky thing will happen. I use BluePills for a single task, such as just running a MMU.
I would reckon, using a BluePill, you are using something similar to a FTD1232 set to 3.3V and hooked to pins A9 and A10 of the BluePill with the Arduino IDE. If so that connection to the computer through the FTD will provide you with serial print feedback on through the Arduino IDE.
@Idahowalker i already managed to be able to program the bluephill without external serial hardware or program directly via bluephill usb port. i meant i already able to communicate to bluephill via its usb port with my laptop, so my confusion is how to apply Serial.print(); somewhere else within my code for debugging purpouse
so my confusion is how to apply Serial.print(); somewhere else within my code for debugging purpouse
What do you want to print? The way to debug your code is to ask yourself "Does it get here?". The answer to that is usually dependent on some value, like you can't get to the place where you call brake() unless the value in FrontDist is less than or equal 30. So, what IS the value in FrontDist?
And, don't just print the value. Print something before the value, so you know what the value represents.
@PaulS if i set serial.print(); and kept the usb cable connect between bluephill and the laptop the 4WD car run as expected and not work if i unplug the usb cable even with serial.print(); within the code also not work if i still comment the serial.print();
#include <Servo.h> //add Servo Motor library
#include <NewPing.h> //add Ultrasonic sensor library
#define TRIG_PIN PB3 //tim2 Pin A0 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN PB4 //tim3 Pin A1 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 170 // sets maximum useable sensor measuring distance y
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to meas
Servo servo_motor;
#define ENA PB6 //tim4
#define IN1 PA6 // tim3
#define IN2 PA7 // tim3
#define ENB PB7 //tim4
#define IN3 PB0 //tim3
#define IN4 PB1 //tim3
int FrontDist = 0;
int RightDist = 0;
int LeftDist = 0;
void setup() {
Serial.begin(115200);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
servo_motor.attach(PB9);// tim4
servo_motor.write(90);
}
void loop() {
FrontDist = sonar.ping_cm();
delay(500);
Serial.print(FrontDist);
if (FrontDist <= 30){
brake();
servo_motor.write(15); // servo turn right
RightDist = sonar.ping_cm();
Serial.print(RightDist);
delay(1000);
servo_motor.write(175); // servo turn left
LeftDist = sonar.ping_cm();
Serial.print(LeftDist);
delay(1000);
servo_motor.write(90);
if (LeftDist > RightDist){
GoLeft();
}
else{
GoRight();
}
}
else
{
Forward();
}
}
void GoRight(){
turnRight();
}
void GoLeft(){
turnLeft();
}
void Forward()
{
motorAForward();
motorBForward();
}
void Backward()
{
motorABackward();
motorBBackward();
}
void turnLeft()
{
motorABackward();
motorBForward();
}
void turnRight()
{
motorAForward();
motorBBackward();
}
void coast()
{
motorACoast();
motorBCoast();
}
void brake()
{
motorABrake();
motorBBrake();
}
////////////////////////////////MOTOR A CONTROL
void motorAForward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 75);
}
void motorABackward()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 75);
}
////////////////////////////////MOTOR B CONTROL
void motorBForward()
{
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 75);
}
void motorBBackward()
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 75);
}
//////////////////////////////////////////COASTING AND BREAKING
void motorACoast()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
void motorABrake()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
}
void motorBCoast()
{
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void motorBBrake()
{
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
}