Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor
- const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
- const int ledPin = ; // LED Pin
- const int buzzPin = ; // Buzzer Pin
- void setup() {
- Serial.begin(9600); // Starting Serial Terminal
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- pinMode(ledPin, OUTPUT); //Sets the LED pin as O/P
- pinMode(buzzPin, OUTPUT); //Sets the buzzer pin as O/P
- }
- void loop() {
- long duration,cm;
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(pingPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- cm = microsecondsToCentimeters(duration);
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- if ( cm < 20.0 )
- {
- // TURN ON LED
- digitalWrite(ledPin, HIGH);
- if ( cm < 10.0 )
- {
- // TURN ON Buzzer
- tone(buzzPin, 1000);
- delay(250)
- }
- else
- {
- // TURN OFF Buzzer
- noTone(buzzPin);
- delay(250);
- }
- }
- else
- {
- // TURN OFF
- digitalWrite(ledPin, LOW);
- }
- delay(100);
- }
- long microsecondsToCentimeters(long microseconds) {
- return microseconds / 29 / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement