1
#include <LiquidCrystal.h>

LiquidCrystal lcd(7,6,5,4,3,2); // lcd(RS,E,D4,D5,D6,D7)

int buzzer = 1;
int sen1 = 10;
int sen2 = 11;
unsigned long t1;
unsigned long t2;
float t;
float speed;
int i = 1000;

void setup()
{
  pinMode(sen1, OUTPUT);
  pinMode(sen2, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print(" Speed Detector ");
  noTone(buzzer);
  
}

void loop()
{ 
  digitalWrite(sen1,HIGH);
  t1=millis();
  digitalWrite(sen1,LOW);
  Serial.println(t1);
  delay(i);
  digitalWrite(sen2,HIGH);
  t2=millis();
  digitalWrite(sen2,LOW);
  Serial.println(t2);
  t=t2-t1;
  t = t /1000;//convert millisecond to second
  speed =(10.0/ t);//v=d/t
  speed = (speed * 18/5);
  
  if(speed > 100){
    tone(buzzer,300,2000);
  }
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Object speed = ");
  lcd.setCursor(3,1);
  lcd.print(speed);
  lcd.print(" Km/hr   ");
  delay(1000);
  lcd.setCursor(3,1);
  lcd.print("            ");
  delay(1000);
  i=i-100;
}
New contributor
Idinesh kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • i getiing a continues sound from my buzzer when program starts – Idinesh kumar Jul 21 at 13:50
  • 1
    And? Maybe if you were to describe what your program is supposed to do, and what hardware it controls we may be able to suggest something. Until then we're as much in the dark as you. – Majenko Jul 21 at 14:05
  • 2
    Please provide a schematic how everything is connected. And I don't quite understand, what you are doing in your program. What should all the millis() code mean? Doesn't make much sense to me. And how should the code behave? – chrisl Jul 21 at 14:05
  • Your code starts out with i = 1000, which should cause speed to be around 36, which should not immediately turn on the buzzer. What speed value is on your display and why didn't you add this information to the question? – StarCat Jul 21 at 14:39
  • 1
    you have the buzzer on pin 1? that is Serial TX on esp – Juraj Jul 21 at 15:12

Your Answer

Idinesh kumar is a new contributor. Be nice, and check out our Code of Conduct.

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.