Problem on ultrasonic loop reading distance "Please Help"

Is there in this forum that can help me. I have a problem where I create a project that uses ultrasonic sensors, servo motor buzzer and android app to control the servo with bluetooth connection. Where the ulrasonic read distance of 5cm then buzzer will sound and when on monitor serial there is data A then servo motor will move at position 60 and buzzer will off. But the buzzer is not off.

#include <Servo.h>
#define trigPin 31
#define echoPin 30
#define Buzzer 4
long duration; 
int distance = 0;
char val;
Servo myservo;

void setup() {
pinMode(Buzzer, OUTPUT);
myservo.attach(8);
myservo.write(1);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin,INPUT);
 
}

void loop() {
if(Serial.available() > 0 ){
  val = Serial.read();
  Serial.print(val);
}
if(val=='A'){
  myservo.write(60);
   digitalWrite(Buzzer,LOW);
}

digitalWrite(trigPin, HIGH);
delayMicroseconds(10); 
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH); 
distance= duration*0.034/2;
 if (distance <=5 ) 
  {
   
      digitalWrite(Buzzer,HIGH);
      delay(1000);
      digitalWrite(Buzzer,LOW);
 
  }
Serial.println(distance);
delay(1000);
}

Can anyone fix this code?
I am very grateful for the time you have spent. :slight_smile:

Doesn’t the trigPin need to be an OUTPUT?

if(data=="A"){Oops

/Users/john/Documents/Arduino/sketch_jul09a/sketch_jul09a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_jul09a/sketch_jul09a.ino:22:15: warning: comparison with string literal results in unspecified behaviour [-Waddress]
   if (data == "A") {
               ^
/Users/john/Documents/Arduino/sketch_jul09a/sketch_jul09a.ino:22:15: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]

You meant "if (data == 'A')", not "if (data == "A")".

You should probably check each character that you read, rather than reading all of the characters that have arrived and keeping only the last one.

 while (Serial.available() > 0 ) {
    data = Serial.read();
    if (data == 'A') {
      myservo.write(60);
      digitalWrite(Buzzer, LOW);
    }
  }

Why set pinMode for trigPin and echoPin in loop()? Don’t they only need to be set once (in setup()?

What is the problem with your code?

Please read the “how to use the forum” stickies to see how to format and post code.

Forgive me, I entered the wrong code. But this is the correct code, please look back. How to make a buzzer off when sending A data from android app. please help :frowning:

But this is the correct code, please look back.

Congratulations on making the replies to your original post irrelevant by changing the code instead of posting a new reply with the correct code.

As to the problem with the buzzer not turning off, what do you see when you print val, does the servo move to 60 and how is he buzzer wired ?

I added a flag to your code that is toggled when a ‘A’ is received. The first ‘A’ turns the buzzer off and the next ‘A’ turns it back on and so on. You might want to put in a bit of delay (20 or so milliseconds) between ranging calls to allow echos to die down. That will help eliminate false ranges. Have a look at the blink without delay example in the IDE examples (File, Examples, Digital) and the several things at a time thread for information on doing timing without delay() (blocking).

#include <Servo.h>
#define trigPin 31
#define echoPin 30
#define Buzzer 4
long duration;
int distance = 0;
char val;
Servo myservo;
boolean alarmOff = false;  // create a flag variable

void setup() {
  pinMode(Buzzer, OUTPUT);
  myservo.attach(8);
  myservo.write(1);
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  if (Serial.available() > 0 )
  {
    val = Serial.read();
    Serial.print(val);

    if (val == 'A')
    {
      myservo.write(60);
      digitalWrite(Buzzer, HIGH);
      alarmOff = !alarmOff;  // toggle the flag variable
    }
  }
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
// if the flag is false, allow alarm 
  if (distance < 6 && alarmOff == false )
  {

    digitalWrite(Buzzer, HIGH);
    delay(1000);
    digitalWrite(Buzzer, LOW);
    delay(1000);

  }
// if the flag is true disable the alarm 
  else
  {
    digitalWrite(Buzzer, LOW);
  }
  Serial.println(distance);
}

groundFungus:
I added a flag to your code that is toggled when a ‘A’ is received. The first ‘A’ turns the buzzer off and the next ‘A’ turns it back on and so on. You might want to put in a bit of delay (20 or so milliseconds) between ranging calls to allow echos to die down. That will help eliminate false ranges. Have a look at the blink without delay example in the IDE examples (File, Examples, Digital) and the several things at a time thread for information on doing timing without delay() (blocking).

#include <Servo.h>

#define trigPin 31
#define echoPin 30
#define Buzzer 4
long duration;
int distance = 0;
char val;
Servo myservo;
boolean alarmOff = false;  // create a flag variable

void setup() {
  pinMode(Buzzer, OUTPUT);
  myservo.attach(8);
  myservo.write(1);
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  if (Serial.available() > 0 )
  {
    val = Serial.read();
    Serial.print(val);

if (val == ‘A’)
    {
      myservo.write(60);
      digitalWrite(Buzzer, HIGH);
      alarmOff = !alarmOff;  // toggle the flag variable
    }
  }
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
// if the flag is false, allow alarm
  if (distance < 6 && alarmOff == false )
  {

digitalWrite(Buzzer, HIGH);
    delay(1000);
    digitalWrite(Buzzer, LOW);
    delay(1000);

}
// if the flag is true disable the alarm
  else
  {
    digitalWrite(Buzzer, LOW);
  }
  Serial.println(distance);
}

Wow I am very grateful to you, this is a very correct code. Sorry to bother you. :slight_smile: :slight_smile:

I would not claim very correct, but maybe correct enough. And no bother, glad to help.