Hello everyone. I was trying to create a project so that when my pet enters the kitchen(ult6), the piezo buzzer buzzes. However, when a person enters(ult7) the buzzer doesn't buzz.
#include <Ultrasonic.h>
Ultrasonic ult7(7);
Ultrasonic ult6(5);
int ButtonHighOrLow;
void setup() {
pinMode(4, INPUT);
pinMode(3, OUTPUT);
}
void loop() {
long RangeInCentimeters7;
long RangeInCentimeters6;
RangeInCentimeters7 = ult7.MeasureInCentimeters();
RangeInCentimeters6 = ult6.MeasureInCentimeters();
if (RangeInCentimeters6 < 150 && RangeInCentimeters7 >= 150) {
for (int i = 0; i < 10000; i++) {
ButtonHighOrLow = digitalRead(4);
if (ButtonHighOrLow == HIGH) {
noTone(3);
break;
}
else {
tone(3, 1250);
delay(1);
}
}
noTone(3);
}
else if (RangeInCentimeters7 < 150) {
noTone(3);
delay(10);
}
else if (RangeInCentimeters7 >= 150 && RangeInCentimeters6 >= 150) {
noTone(3);
delay(10);
}
delay(10);
}
Yet, whenever I try it the project is completely random. I tried researching it, and I found out that the range of the Ultrasonic sensor is not straight, but rather conical. Any ideas for it to work?
[iurl=https://www.tinkercad.com/things/iEpMAOzxZKR-ultrasonic/editel?sharecode=85NTGKPyu1TeNnTtjpoV6eBEfRwUGN79LYoFX5rwC84]Tinkercad link[/iurl]
MarvelShooCopy.ino (867 Bytes)