/*
Arduino | general
Verthru — 10/23/24 at 11:30 PM
my arduino sensor is not sensoring
*/
const int BUZZ_PIN = 11;
const int ECHO_PIN = 8;
const int TRIG_PIN = 9;
int oldDistance = 0;
void setup() {
Serial.begin(9600);
pinMode (ECHO_PIN, INPUT);
pinMode (TRIG_PIN, OUTPUT);
pinMode (BUZZ_PIN, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = ((duration / 2) / 29.1) + 0.5;
if (distance != oldDistance) {
oldDistance = distance;
/*
if (distance <= 100) {
soundValue = 250;
}
if (distance < 90) {
soundValue = 350;
}
if (distance < 80) {
soundValue = 450;
}
if (distance < 70) {
soundValue = 550;
}
if (distance < 60) {
soundValue = 650;
}
if (distance < 50) {
soundValue = 750;
}
if (distance < 40) {
soundValue = 850;
}
if (distance < 30) {
soundValue = 950;
}
if (distance < 20) {
soundValue = 1050;
}
if (distance < 10) {
soundValue = 1150;
}
if (distance < 5) {
soundValue = 1250;
}
*/
if (distance > 100 || distance <= 0) {
Serial.println("Out of range");
noTone (BUZZ_PIN);
} else {
// shantaram — Today at 12:01 AM
int soundValue = 250 + 100 * round(10 - distance / 10);
Serial.print("Range: ");
Serial.print(distance);
Serial.println(" cm");
tone(BUZZ_PIN, soundValue);
}
}
delay(500);
}