Things used in this project

Hardware components:
A000066 iso both
Arduino UNO & Genuino UNO
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
09670 01
SparkFun Dual H-Bridge motor drivers L298
×1
11026 02
Jumper wires (generic)
×1
DC motor (generic)
×2
Software apps and online services:
Ide web
Arduino IDE
Hand tools and fabrication machines:
09507 01
Soldering iron (generic)

Code

Arduino codeArduino
int enA = 8;
int in1 = 9;
int in2 = 10;
int tt = 2;
// Motor B
 
int enB = 13;
int in3 = 11;
int in4 = 12;

 int trig=6;
 int echo=5;
 int vreme,dinstanca;
void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  pinMode(tt,INPUT);
}
 
void loop()
{
int x = ultrazvucni();
if(x < 5 ) {
  nazad(1000);
  stop1(500);
  levo(600);
  stop1(300);
}
else napred(100);

}

void napred(int vreme){
   digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 250);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 500);
  delay(vreme);
}
void nazad(int vreme){
   digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 250);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 500);
  delay(vreme);
}
void stop1(int vreme){
   digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(vreme);
  
}
void levo(int vreme){
   digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 250);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  analogWrite(enB, 500);
  delay(vreme);
  
}void desno(int vreme){
   digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 250);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 500);
  delay(vreme);
}
int ultrazvucni(){
digitalWrite(trig,LOW);
delayMicroseconds(5);
digitalWrite(trig,HIGH);
delayMicroseconds(5);
digitalWrite(trig,LOW);
vreme=pulseIn(echo,HIGH);
dinstanca=vreme/2/29;
return dinstanca;
}

Credits

6098c3738136b594919dce817cdfccbe
Botti
0 projects • 2 followers
Contact
Logokocka lzxtgjyylt
MakerRobotics
6 projects • 75 followers
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

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog