Things used in this project

Hardware components:
Ard nano
Arduino Nano R3
×1
Tens70
9V battery (generic)
×1
11026 02
Jumper wires (generic)
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
E switch eg1218 image 75px
Slide Switch
×1
Software apps and online services:
Ide web
Arduino IDE

Code

ProgramArduino
/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8

 */


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define Pin 9
#define Pin 10
#define Pin 11
#define Pin 12

int maximumRange = 55; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

int forward = 9; // forward pin
int front = 10; // front pin
int reverse = 11; // reverse pin
int back = 12; // back pin


void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(forward, OUTPUT);
 pinMode(front, OUTPUT);
 pinMode(reverse, OUTPUT);
 pinMode(back, OUTPUT);
}

void go_forward()
{
digitalWrite(forward,HIGH); // turn forward motor on
digitalWrite(front,HIGH); // turn front motor on
digitalWrite(reverse,LOW); // turn reverse motor off
digitalWrite(back,LOW); // turn back motor off
}
void go_rvrs()
{
digitalWrite(forward,LOW); // turn forward motor off
digitalWrite(front,LOW); // turn front motor off
digitalWrite(reverse,HIGH); // turn reverse motor on
digitalWrite(back,HIGH); // turn back motor on
}

void stop_car()
{
digitalWrite(reverse,LOW); // turn reverse motor off
digitalWrite(forward,LOW); // turn forward motor off
digitalWrite(front,LOW);
digitalWrite(back,LOW);
}
void go_right()
{
digitalWrite(forward,HIGH); // turn forward motor on
digitalWrite(back,HIGH); // turn back motor on
digitalWrite(reverse,LOW); // turn reverse motor off  
digitalWrite(front,LOW); // turn front motor off
}
// the loop() method runs over and over again,
// as long as the Arduino has power





void loop() {
/* 
The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. 
*/ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 

if (distance > maximumRange || distance < minimumRange){
 /* Send a positive number to computer and Turn MOTORS ON 
 to indicate "out of range" */
 Serial.println("1");
 go_forward();


}
 else {
  go_rvrs();
  delay(250);
  go_right();
 delay(500);
 
 
 }
 //Delay 50ms before next reading.
 delay(50);

Credits

62f274a202a2ab04dffeae41bca98dbd
VASILEIOS MOUGIOS

I like technology and I enjoy programming at my free time.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Arduinomated Car Parking with Voice Assistance in Smartphone
Intermediate
  • 80
  • 5

Full instructions

Make an assistant which tells you about how much to drive backwards without any collision through your smartphone by Arduino and 1Sheeld.

Sigfox Forest Fire Detector
Intermediate
  • 2,015
  • 13

Full instructions

How to prevent forest from burning? A small Sigfox signal can prevent natural disasters. We will use an Arduino MKRFOX + a flame detector.

Using Common Cathode and Common Anode RGB LED with Arduino
Intermediate
  • 850
  • 15

Protip

Common Cathode and Common Anode RGB LEDs are two different kinds of RGB LEDs whose function is same but working is different.

Analog-XY-Plotter-Output-to-HPGL-Translator
Intermediate
  • 209
  • 2

Translate analog X-Y plotter output to HPGL command stream.

From BT to WiFi: Creating WiFi Controlled Arduino Robot Car
Intermediate
  • 275
  • 3

Protip

NodeMCU ESP8266 in access point mode: the simplest way to make Wi-Fi controlled Robot Car from Bluetooth Arduino Robot Car + Android App.

TV Tee Shirt
Intermediate
  • 3,686
  • 12

One shirt, thirty-six LEDs, endless possibilities.

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog