Harrison RohrerMatt Shockley
Published

Wall Avoiding Car

This is a small prototype car that uses an ultrasonic sensor so a car will turn to avoid hitting a wall with a remote on off capability.

BeginnerFull instructions provided296
Wall Avoiding Car

Things used in this project

Hardware components

Argon
Particle Argon
×2
Breadboard (generic)
Breadboard (generic)
×1
Foam Wheels
×4
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
DC motor (generic)
×2
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
KY-004 3 Pin Button Switch
×1
Battery Holder, AA x 4
Battery Holder, AA x 4
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Zip-Ties

Story

Read more

Custom parts and enclosures

Chassis CAD

Schematics

Circuit Diagram- Vehicle PINOUT

Wiring for the vehicle Argon

Circuit Diagram- Remote PINOUT

Wiring for the argon remote control

Code

WiFi Remote Start/Stop

Arduino
Wireless WiFi code which publishes events to the particle event stream to start and stop the vehicle as long as both are connected to the internet
const int buttonpin = D5;
const int ledpin = D7;

boolean oldbuttonstate = LOW;
boolean newbuttonstate = LOW;
boolean ledstatus = LOW;




void setup() {
    pinMode(ledpin, OUTPUT);
    digitalWrite(ledpin, LOW);
    pinMode(buttonpin, INPUT);
    Particle.subscribe("Left Turn", WallDetect);
    Particle.subscribe("Right Turn", WallDetect);


}


void WallDetect(const char *event, const char *data){
    digitalWrite(ledpin, HIGH);
    delay(1000);
    digitalWrite(ledpin, LOW);

}



void loop() {
    
    newbuttonstate = digitalRead(buttonpin);
    if (newbuttonstate != oldbuttonstate){
        
        if (newbuttonstate == HIGH){
            
            if (ledstatus == LOW){
                
                Particle.publish("PowerON");
                delay(1000);
                ledstatus = HIGH;
                
            }else{
                
                Particle.publish("PowerOFF");
                delay(1000);
                ledstatus = LOW;
                
            }
        }
        
        oldbuttonstate = newbuttonstate;
    }


}

Vehicle Obstacle Avoidance Code

Arduino
Uses ultrasonic sensor to determine distance and avoid obstacles in the immediate path before randomly choosing a direction to turn. The code subscribes to the particle event stream to allow for remote starts and stops. Publishes it's left versus right turns which are simply graphed in a google spreadsheet with IFTTT
//Defining Pins: Ultrasonic
const int trigPin = A1;
const int echoPin = A5;
const int ledpin = D7;
//Defining Pins: Motors/Motor Driver
const int MotorR1 = D6;
const int MotorR2 = D5;
const int MotorL1 = D3;
const int MotorL2 = D2;

//Defines Variables: Ultrasonic
long duration;
int distance;
//Defines Variables: Direction
int direction;



//creates a state for driving which is argued by cloud functions
boolean ledstatus = LOW;

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(MotorR1, OUTPUT); //Sets Motor Right pin 1 as an Output
  pinMode(MotorR2, OUTPUT); //Sets Motor Right pin 2 as an Output
  pinMode(MotorL1, OUTPUT); //Sets Motor Left pin 1 as an Output
  pinMode(MotorL2, OUTPUT); //Sets Motor Left pin 2 as an Output
  pinMode(ledpin, OUTPUT);  //sets onboard argon ledpin as output
  digitalWrite(ledpin, LOW); //clears ledpin to be off for startup
  Particle.subscribe("PowerON", Start);
  Particle.subscribe("PowerOFF", Stop);
  
 

}

//creates the start function which reads off cloud events. this is marked simply by the of the argon's onboard led
void Start(const char *event, const char *data){
    
    digitalWrite(ledpin, HIGH);
    ledstatus   =   HIGH;
    
}
//creates a stop function for the car that reads off cloud events. This is marked as off by the argon's onboard led turning off
void Stop(const char *event, const char *data){
    
    digitalWrite(ledpin, LOW);
    ledstatus   =   LOW;
    digitalWrite(MotorR1, LOW);
    digitalWrite(MotorR2, LOW);
    digitalWrite(MotorL1, LOW);
    digitalWrite(MotorL2, LOW);
    
}


void loop() {
    // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  
  //Driving Function
if (ledstatus   ==  HIGH){
  if (distance < 7) {
       //ultrasonic detects obect at ~~ 7 centimeters, pauses, then reverses
       delay(500);
       digitalWrite(MotorR1, LOW);
       digitalWrite(MotorR2, LOW);
       digitalWrite(MotorL1, LOW);
       digitalWrite(MotorL2, LOW);
       delay(200);
       digitalWrite(MotorR1, LOW);
       digitalWrite(MotorR2, HIGH);
       digitalWrite(MotorL1, LOW);
       digitalWrite(MotorL2, HIGH);
       delay(2000); 
       direction = random (1,3); //direction is chosen from a random integer between the lower bound and the excluded upper bound eg (1,3) == 1 or 2
       if (direction == 1) {
           
           digitalWrite(MotorR1, HIGH);
           digitalWrite(MotorR2, LOW);
           digitalWrite(MotorL1, LOW);
           digitalWrite(MotorL2, HIGH);
           delay(random(800,8001));
           digitalWrite(MotorR1, LOW);
           digitalWrite(MotorR2, LOW);
           digitalWrite(MotorL1, LOW);
           digitalWrite(MotorL2, LOW);
           Particle.publish("Right Turn");
           delay(500);
       }else{
               
              
               digitalWrite(MotorR1, LOW);
               digitalWrite(MotorR2, HIGH);
               digitalWrite(MotorL1, HIGH);
               digitalWrite(MotorL2, LOW);
               delay(random(800,8001));
               digitalWrite(MotorR1, LOW);
               digitalWrite(MotorR2, LOW);
               digitalWrite(MotorL1, LOW);
               digitalWrite(MotorL2, LOW);
               Particle.publish("Left Turn");
               delay(500);
       }
  }else {
      //car drives straight
      digitalWrite(MotorR1, HIGH);
      digitalWrite(MotorR2, LOW);
      digitalWrite(MotorL1, HIGH);
      digitalWrite(MotorL2, LOW);
      
      }
  }
  
}

Credits

Harrison Rohrer
1 project • 0 followers
Contact
Matt Shockley
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.