ByronSpars
Published

Autonomous Car

This project is a self-driving car that is programmed to avoid obstacles and activate LED's while doing so.

IntermediateFull instructions provided23,806
Autonomous Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
Get a lot of these, just in case.
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
RobotGeek Continuous Rotation Servo
RobotGeek Continuous Rotation Servo
Get some large wheels for these.
×2
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

autonomouscar_X4t9VfPx5A.fzz

Code

Autonomous Car

Arduino
This is the code used to make the car detect obstacles. There is another LED light that activates when lights are off. To make the front lights blink and have the reverse lights activate when backing up, uncomment the commented sections, however this may break something else in the code.
#define trigPin 12
#define echoPin 13
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LightPin = 0;
int threshold = 980;
unsigned long previousMillis = 0;
const long interval = 1000;
int ledState = LOW;


int n;
long duration, distance;
String readString;

#include <Servo.h>
Servo myservo1;
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);
  myservo2.attach(9);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED5, OUTPUT);
}

void loop() {
  Serial.println(analogRead(LightPin));

  digitalWrite(trigPin, HIGH);
  _delay_ms(500);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;

  unsigned long currentMillis = millis();

  if (analogRead(LightPin) > threshold && currentMillis - previousMillis > interval) {
    if (ledState == HIGH) {
      ledState = LOW;
      Serial.println("low");
    } else {
      ledState = HIGH;
      Serial.println("high");
    }
    digitalWrite(LED5 , ledState);

  }
    if (distance < 30) {
      //  digitalWrite(LED1,LOW);
      //  digitalWrite(LED2,LOW);
      //  digitalWrite(LED3,HIGH);
      //  digitalWrite(LED4,HIGH);
      myservo1.write(n);
      myservo2.write(180 - n);
      delay(1000);
      myservo1.write(n);
      myservo2.write(90 - n);
      delay(500);
    }
    else {
      // digitalWrite(LED1, HIGH);
      // digitalWrite(LED2, HIGH);
      // digitalWrite(LED3, LOW);
      // digitalWrite(LED4,LOW);
      myservo1.write(180 - n);
      myservo2.write(n);

    }
  }

Credits

ByronSpars
0 projects • 2 followers

Comments