0
#define trigPin 13
#define echoPin 14
#define e1 10  // Enable Pin for motor 1
#define t1 8   // Control pin 1 for motor 1
#define t2 9   // Control pin 2 for motor 1
#define led 3
#define led2 5
#define buzzer 4

float duration,distance;
#include<SoftwareSerial.h>

void setup() {
 Serial.begin(9600);
 pinMode(trigPin,OUTPUT);
 pinMode(echoPin,INPUT);
 pinMode(e1, OUTPUT);
 pinMode(t1, OUTPUT);
 pinMode(t2, OUTPUT);

}

void loop() {
  digitalWrite(e1, HIGH);
  digitalWrite(t1, HIGH);
  digitalWrite(t2, LOW);

  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);


duration=pulseIn(echoPin,HIGH);
distance=(duration/2)*0.0343;
Serial.print("Distance= ");
if(distance>=400||distance<=2)
{ 
  Serial.println("out of range");
}
else if(distance<=100&&distance<=50)

{ 
  digitalWrite(led,HIGH);
  delay(1000);
  analogWrite(e1,157);
  digitalWrite(t1,HIGH);
  digitalWrite(t2,LOW);
  delay(500);
}
else if(distance<=50&&distance>=25)
{
  digitalWrite(buzzer,HIGH);
  delay(100);
  analogWrite(e1,56);
  digitalWrite(t1,HIGH);
  digitalWrite(t2,LOW);
}
else if(distance<=25&&distance>=10)
{
  digitalWrite(led2,HIGH);
  delay(100);
  analogWrite(e1,0);

}
else if(distance<=390&&distance>=100)
{  digitalWrite(e1, HIGH);
  digitalWrite(t1, HIGH);
  digitalWrite(t2, LOW);


}

delay(500);
}
  • 1
    Have you tested the code? How does it behave different, than you expect? And you should really add a complete description, what the desired behavior is. Just the title is not enough. Please put some effort into writing your question with all the information needed – chrisl Nov 13 at 19:37
  • 1
    you do not use a sensor to brake a motor ... you use a motor driver that is controlled by the program ... the sensor is used only for input ... decisions about motor braking can be made as a result of the input .... since you said nothing about what parameter affects braking, your question is not answerable – jsotola Nov 13 at 19:38
  • Please take the tour, learn how to How to Ask, and then edit your question with additional informations. You will get much better answers then. ;-) – the busybee Nov 14 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.