Arduino Sketch Code is as below:
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int sig = 7;
int led = 3;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
Serial.begin(9600);
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
pinMode(sig, OUTPUT);
digitalWrite(sig, LOW); // turn the LED on (HIGH is the voltage level)
delayMicroseconds(2); // wait for a second
digitalWrite(sig, HIGH); // turn the LED off by making the voltage LOW
delayMicroseconds(5); // wait for a second
digitalWrite(sig, LOW);
pinMode(sig, INPUT);
long duration = pulseIn(sig, HIGH);
int distance = duration / 74 / 2;
Serial.print("distance ");
Serial.println(distance);
if (distance <50){
tone(8, 1000, 500);
digitalWrite(led, HIGH);
delay(100);
}
digitalWrite(led, LOW);
delay(100);
}
Note: I am using circuits.io for simulating the task,
No comments:
Post a Comment