Help Me : My Buzzer Keep Buzzing!

Hello guys,

Please help me solving with my coding! :cry:

I am doing my project with two Arduino Uno which is A and B using wireless (APC220 Radio Communication Modules).

and my idea is, my buzzer is buzzing whenever my sensor detects in a certain distance.

The A is connected with a Ultrasonic Sensor (SEN136B5B).
The B is connected with a piezo buzzer.

The A is working perfectly, my sensor detected which in result, serial monitor can read the distance in Inches and Centimeters.

But the B is keep buzzing even though i disconnected the sensor.

The Code for A is:

const int pingPin = 7;

void setup() 
{
  Serial.begin(9600);
}

void loop()
{
  long duration, inches, cm;
  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
  
}

long microsecondsToInches(long microseconds)

{
  
  return microseconds / 74 / 2;
  
}

long microsecondsToCentimeters(long microseconds)

{
  
  return microseconds / 29 / 2;
  
}

The code for B:

const int BUZZ = 5;

void setup()

{
  
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  
}

void loop()

{
  while (Serial.available() > 0)
  
  {
    int cm = Serial.read();
  }
  
  long cm;
  
  
 if(cm< 100)
 
 {
  
  analogWrite(BUZZ, (100 - cm) * 10);
 }

else

{
  
  digitalWrite(BUZZ, LOW);
}

}

I would read this as a byte instead of an int, as only 1 byte of data actually comes in.
int cm = Serial.read();

You then create a new instance of cm here
long cm;
which will likely = 0 and wipe out the read you just did. serial.println and see.
I would take it out.

Then cm = 0 is <100,
you analogWrite it (and analogWrite only needs a byte, 0 to 255)
(100 - 0) *10 = 1000
so I don't know what that will actually do for an output.

The line
long cm
creates a new variable called cm and sets it to zero.
Therefore when you do the if statement next it will always be less than 100 and so the buzzer will always sound.

EDIT CrossRoads beat me to it.

Also, how is B getting its serial data?
From the serial monitor?
That data will come in as type char, you will need to convert it to an byte
www.ascitable.com

Simple way is just to subtract '0' (or 48, or 0x30);

byte cm = Serial.read() - '0';

If you are receiving multiple characters, 125
then you have to read three times, and convert those:

if (Serial.available()>2){ // at least 3 bytes received?
byte upper = Serial.read() - '0';
byte middle = Serial.read() - '0';
byte lower = Serial.read() - '0;
byte total = upper * 100 + middle * 10 + lower; // don' enter more than 255 for analogWrite!

I just make all the variables global, declare them all before setup(), and skip all this constant declaration nonsense.

CrossRoad,

Could you please rewrite my codes and edit which incorrect?
I so confusing and hair-wire.. Sorry, i am soo new to arduino actually.

Doesn't work that way. Give it a try yourself first.

Grumpy_Mike:
The line
long cm
creates a new variable called cm and sets it to zero.

. . . only if the variable "cm" has global scope.
Which it doesn't appear to, in which case, it adopts a random value.

CrossRoads,

I've tried your coding..
I doesnt work. :frowning:

But, i got a sensor reading from other arduino using serial monitor. It was perfect. That's mean the data from A to B is worked.

On the other hand, it still doesn't achieve my objective. I just want my piezo buzzer trigger a sound when my sensor detect in a certain distance.

This is an updated code:

const int BUZZ = 5;
String inputString = "";
boolean stringComplete = false;
byte cm;


void setup()

{
  
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  inputString.reserve(200);
  
}

void loop()

{
  
  if (stringComplete)
  
  {
    
    Serial.println(inputString);
    inputString = "";
    stringComplete = false;
    
    if(cm< 100)
 
 {
  
  analogWrite(BUZZ, (200 - cm) );
 }

else

{
  
  digitalWrite(BUZZ, LOW);
}

  }
}
 
 void serialEvent()
{
 char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n')
{
  stringComplete = true;
}
}

I think it has something to do with the cm code. Is there other code aside from cm ? I received a data from A by reading from serial monitor. But for some reason, my buzzer keep sounding in every way.

But for some reason, my buzzer keep sounding in every way.

Because you are never changing the value of the variable cm.

Grumpy_Mike

How to change the value of the variable cm ?

zarifrobot:
Grumpy_Mike

How to change the value of the variable cm ?

Assign it a new value.
Maybe a new reading?