Professional Documents
Culture Documents
Sensorforblind
Sensorforblind
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(motorPin, OUTPUT);
}
void loop() {
long duration, distance;
// Calculate distance in cm
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If an object is detected within a range of (30cm), activate the buzzer and vibration motor
if (distance > 0 && distance < 30) {
tone(buzzerPin, 100); // atart the buzzer tone at 1000Hz
digitalWrite(motorPin, HIGH); // activate the vibration motor
} else {
noTone(buzzerPin); // stop the buzzer tone
digitalWrite(motorPin, LOW); // deactivate the vibration motor
}
delay(0); // duration time before taking another measurement, i put Zero to make it no delay
}