Object Avoidance Robot Car Project

 

Autonomous robot car project – Using Ultrasonic sensor to avoid objects in its path. Current detection range is set to 30cm however this can be changed in the code. I also added a lighting system; RGB front head lights, red back lights and white reverse lights. The front lights comes on automatically when it gets dark, this is done using LDR in series with a 100k resister (potential divide). Its not finished yet, I would have preferred to use a servo to look left right and then make the decision in which direction to move however I didn’t have one in hand and the reverse lights were very dim because it not enough power from the 5V DC (Arduino) -> 2x white LED in series. I need to make a separate power circuit using transistors.

 

Arduino Code

#include 
#include       
#define PIN A5 //RGB strip
#define N_LEDS 8 //number of led
const int reverseLED = A4;
const int backLight = A2;
const int trigger = A1;
const int echo = A0;
const int LDR = A3;
long duration;
int distance;
//Motor stuff
int curDist = 0;
String motorSet = "";
int speedSet = 220;
int minDistance = 30; // stopping distance in cm
int nightVal = 100;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
AF_DCMotor leftMotor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);// create motor #3, using M3 output, set to 1kHz PWM frequency
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);// create motor #4, using M4 output, set to 1kHz PWM frequency

void setup() {
  delay(4000); // delay for one seconds
  pinMode(reverseLED, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(backLight, OUTPUT);
  pinMode(LDR, INPUT);
  strip.begin();
  strip.show();
  Serial.begin(9600);
}
void loop() {
  distancex();
  light();
  if (distance > minDistance) {
    moveForward();
  }
  else if (distance < minDistance) {
    moveStop();
    moveBackward(500);
    moveStop();
    turnRight(1000);
  }
}
void reverseLight(int val) {
  analogWrite(reverseLED, val);
}
void frontLight(int power) {
  strip.setBrightness(power);
  for (int x = 0; x < N_LEDS; x++ ) { strip.setPixelColor(x, 255, 255, 255); x = x + 6; } analogWrite(backLight, 255); strip.show(); } void distancex() { digitalWrite(trigger, LOW); delayMicroseconds(2); digitalWrite(trigger, HIGH); delayMicroseconds(10); digitalWrite(trigger, LOW); duration = pulseIn(echo, HIGH); distance = duration * 0.034 / 2; //Serial.println(distance); } void moveStop() { // stop the motors leftMotor1.run(RELEASE); leftMotor2.run(RELEASE); rightMotor1.run(RELEASE); rightMotor2.run(RELEASE); delay(250); } void moveForward() { leftMotor1.run(FORWARD); leftMotor2.run(FORWARD); rightMotor1.run(FORWARD); rightMotor2.run(FORWARD); leftMotor1.setSpeed(speedSet); leftMotor2.setSpeed(speedSet); rightMotor1.setSpeed(speedSet); rightMotor2.setSpeed(speedSet); delay(5); } void moveBackward(int delayx) { reverseLight(255); leftMotor1.run(BACKWARD); leftMotor2.run(BACKWARD); rightMotor1.run(BACKWARD); rightMotor2.run(BACKWARD); leftMotor1.setSpeed(speedSet); leftMotor2.setSpeed(speedSet); rightMotor1.setSpeed(speedSet); rightMotor2.setSpeed(speedSet); delay(delayx); reverseLight(0); } void turnRight(int delayx) { leftMotor1.run(FORWARD); leftMotor2.run(BACKWARD); rightMotor1.run(FORWARD); rightMotor2.run(BACKWARD); leftMotor1.setSpeed(speedSet); leftMotor2.setSpeed(speedSet); rightMotor1.setSpeed(speedSet); rightMotor2.setSpeed(speedSet); delay(delayx); // run motors this way for delayx } void light(){ if (analogRead(LDR)>nightVal){
    frontLight(200);
    
  }
  else{
    frontLight(0);
  }
}

Video