jojessegaming
Published

4 wheeled obstacle avoiding car

School project, code may require some changes with timing for driving/steering when an obstacle is detected.

AdvancedShowcase (no instructions)421
4 wheeled obstacle avoiding car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Adafruit L293 Motor Shield
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
DC Motor, Miniature
DC Motor, Miniature
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Story

Read more

Schematics

how to connect sro4

Code

Full code

Arduino
Install the libraries and it should work.
You can tweak around with timing to.
#include <Servo.h>

#include <NewPing.h>

#include <AFMotor.h>

Servo my_servo;

 
int trigPin = A0;    // Trigger
int echoPin = A1;    // Echo
long duration, cm, inches;
AF_DCMotor motor(2, MOTOR12_64KHZ);


 
void setup() {
  Serial.begin (9600);
  my_servo.attach(9);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor.setSpeed(200);
}
 
void loop() {
//servo
my_servo.write(0);
//ultrasoon
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // Convert the time into a distance
  cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
  if(cm <= 10)
  {
  motor.run(RELEASE);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  my_servo.write(180);
  motor.run(BACKWARD);
  delay(3000);
  my_servo.write(0);
  motor.run(FORWARD);
  delay(100);
  }
 else
 {
motor.run(FORWARD);
delay(100);
 }
 }
  

Credits

jojessegaming
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.