I'm making a project with motion sensor, but without motion detection, the buzzer keeps beeping
Invert the output.
please use code tags to post, screenshots are worse than useless.
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int const trigPin = 4;
int const echoPin = 5;
int const buzzPin = 12;
const int LED1 = 13;
void setup()
{
Serial.begin(115200);
configTime(0, 0, "pool.ntp.org");
client.setTrustAnchors(&cert);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int a = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
a++;
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzPin, OUTPUT);
pinMode(LED1 , OUTPUT);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Wifi Connected!", "");
bot.sendMessage(CHAT_ID, "System has Started!!", "");
}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10 && distance >= 0) {
digitalWrite(buzzPin, HIGH);
digitalWrite(LED1, HIGH);
bot.sendMessage(CHAT_ID, "UYARI! HAREKET ALGILANDI!!", "");
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED1, LOW);
}
}
Code tags. please reformat with </> button ...
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int const trigPin = 4;
int const echoPin = 5;
int const buzzPin = 12;
const int LED1 = 13;
void setup()
{
Serial.begin(115200);
configTime(0, 0, "pool.ntp.org");
client.setTrustAnchors(&cert);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int a = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
a++;
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzPin, OUTPUT);
pinMode(LED1 , OUTPUT);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Wifi Connected!", "");
bot.sendMessage(CHAT_ID, "System has Started!!", "");
}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10 && distance >= 0) {
digitalWrite(buzzPin, HIGH);
digitalWrite(LED1, HIGH);
bot.sendMessage(CHAT_ID, "UYARI! HAREKET ALGILANDI!!", "");
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED1, LOW);
}
}
check again
hi.
make the distance value in float rather than int.
If not, the division distance = (duration/2) / 29.1;
won't give you a correct result.
nothing has changed
It has changed something I promess.
My advise is to export your distance values on the serial monitor (eventually comment the line digitalWrite(buzzPin, HIGH);
during test).
this way you can visualize the result and compare it to your if statment.
By the way, distance will be positive so if (distance <= 10)
should be enough.
EDIT: your loop is focused on the pulsIn function as it is a blocking function. Every other lines will be read by your board VERY quickly. Insert a "human compatible" delay in your loop so US measurement are not made continuously, like delay(100);
EDIT EDIT: other examples in the web are slighlty different than yours:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration *0.034/2;
there the result is given in centimeters.
yes i tried but still the same
ok. did you try:
- put a delay;
- correct the timing for triggering distance measurement
- display output value to serial monitor
- check if the results are consistency with actual distance measured
are you now able to share the value you get? or at least understand if your code is running properly (the different if statment are coherent and can be entered at a moment) ?
Also, did you try posting what you tried that had the same outcome?
yes but still same
"yes but still same" is a terrible message to people trying to help. It does not give any useful information.
Post your latest code here (in code tags).
Add a print before your if statement. What gets printed?
Serial.print("Distance = ");
Serial.println(distance);
if (distance <= 10 && distance >= 0) {
digitalWrite(buzzPin, HIGH);
digitalWrite(LED1, HIGH);
bot.sendMessage(CHAT_ID, "UYARI! HAREKET ALGILANDI!!", "");
} else {
digitalWrite(buzzPin, LOW);
digitalWrite(LED1, LOW);
}
I think the third killed my patience.
Hope you will get better results than me.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.