0

I'm just a new beginner and I'm doing my first project which is Obstacle avoiding robot!I use L298N motor shield, ultrasonic sensor for my project.

My problem is that I try to test my ultrasonic sensor. Specifically, if i put obstacle near sensor and the distance less than 10cm, the motor gears will run(what ever direction, just for testing). However, when i put my hand near the sensor, the L298N shield receives signal from Arduino board correctly, but the wheels are not running/

Here are my 2 picture to describe my problem I've already tested whether the motor are broken but they're not.

enter image description here enter image description here

This is my code:

#include <Servo.h>
Servo myServo;
int EA= 9;
int EB = 10;
int I1= 2;
int I2= 4;
int I3= 7;
int I4= 8;
int servoPin=5;
int trigOutput= 3;
int echoInput= 6;
void setup() {
  Serial.begin(9600);
  pinMode(EA,OUTPUT);
  pinMode(EB,OUTPUT);
  pinMode(I1,OUTPUT);
  pinMode(I2,OUTPUT);
  pinMode(I3,OUTPUT);
  pinMode(I4,OUTPUT);
 // myServo.attach(servoPin);
  pinMode(trigOutput, OUTPUT);
  pinMode(echoInput, INPUT);
}

void loop() {
  int timee;
  float distance;
  digitalWrite(trigOutput,LOW);
  delayMicroseconds(2);
  digitalWrite(trigOutput,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigOutput,LOW);
  timee= pulseIn(echoInput,HIGH);
  distance= 0.034*(timee/2);
  delay(300);
  if(distance<=10){
    straight();
  } else Brake();
}
void straight()
{
  analogWrite(EA,200);
  analogWrite(EB,200);
  digitalWrite(I1,LOW); //RB
  digitalWrite(I2,HIGH); //RF
  digitalWrite(I3,HIGH);  //LF
  digitalWrite(I4,LOW); //LB
  delay(100);
}
void Brake(){
  digitalWrite(I4,HIGH);
  digitalWrite(I3,HIGH);
  digitalWrite(I2,HIGH);
  digitalWrite(I1,HIGH);
  delay(40);
}
New contributor
phát trương is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Which ultrasonic sensor do you use? – PimV Jan 12 at 12:50
  • I use hc sr04! Thks – phát trương Jan 13 at 2:47
  • it is hard to think with logic where it should run if obstacle is near and break if the path is free – Juraj Jan 13 at 10:58
0

Break it down some. You have a STRAIGHT and a BRAKE function. Create a READ_DISTANCE function and put all your sensor reading code in it. Use serial monitor feedback to confirm you are actually reading the distances and reading them correctly.

Your loop() then calls read_distance and based on that information decides to brake, go straight, or call a TURN or BACK function.

Your Answer

phát trương is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.