Buzzer buzzing because of rx code optimization

hello,i'm trying to send 5 push button signals from a 433mhz tx to rx .In rx i want to use a buzzer remotely .The thing is the buzzer buzzes default when i connect...i used pulseIn to divide freq among the buzzer to sound differently.I checked wires etc & didn't find any flaws there.The problem is there as it buzzes always ...Thank you. Here's the code

unsigned long Ton = 0;
int rxPin = 7;
int buzzerPin = 9;
int btn = 0, pbtn=0;
int freq = 0;

void setup()
{
pinMode(rxPin, INPUT);
}

void loop()
{ 

Ton = pulseIn(rxPin, HIGH);
btn=0;

if (Ton < 94) { btn = 5; freq = 392; }
else if (Ton < 188) { btn = 4; freq = 350; }
else if (Ton < 375) { btn = 3; freq = 330; }
else if (Ton < 750) { btn = 2; freq = 294; }
else if (Ton < 1500) { btn = 1; freq = 262; } 
if (btn != pbtn) {
pbtn = btn;
if (btn == 0) noTone(buzzerPin);
else tone(buzzerPin, freq);
delay(50);
}
}
`

It sounds an awful lot like you're using a powered buzzer rather than a piezo transducer. Can you share a link to the exact buzzer that you're using?

https://www.techshopbd.com/product-categories/audio/1007/speaker-techshop-bangladesh
hi i'm using this speaker/buzzer also,i'm using a 20mm one too.I don't know what's the issue though.....want to stop that buzzing sound

That speaker looks like it should work. Have you tried the ToneMelody example to check that your hardware is ok?

Maybe I'm missing something.

But the link shows an 8 ohm speaker. I doubt you can drive that directly from an Arduino.

that tonemelody func works i tried....it randomy starts sound after connecting .....just don't know :frowning:

sterretje:
Maybe I'm missing something.

But the link shows an 8 ohm speaker. I doubt you can drive that directly from an Arduino.

well,i used a normal active buzzer it sounds so poorly....it just keeps buzzing.......

sterretje:
Maybe I'm missing something.

But the link shows an 8 ohm speaker. I doubt you can drive that directly from an Arduino.

It is possible- there are a few projects out there that do it, like this SD-based one from Elm-chan or this one from Technoblogy.

XessX:
that tonemelody func works i tried....it randomy starts sound after connecting

Can you please clarify this? Does the ToneMelody example work, or does it randomly start making sounds?

BJHenry:
It is possible- there are a few projects out there that do it, like this SD-based one from Elm-chan or this one from Technoblogy.
Can you please clarify this? Does the ToneMelody example work, or does it randomly start making sounds?

no,it's not like those...it's through transmitter to receiver signaling the buzzer.And yes it normally works with the toneMelody example ,i checked with the arduino button too and again it does the melody.The thing is when i connect the nano with this code it starts to sound randomly....which i can't stop & when i push button on transmitter it works with the frequency given but that sound never stops ....

XessX:
no,it's not like those...it's through transmitter to receiver signaling the buzzer.

That's fine, I was just replying to sterretje's comment about being able to power a speaker directly from the Arduino pins.

XessX:
And yes it normally works with the toneMelody example ,i checked with the arduino button too and again it does the melody.The thing is when i connect the nano with this code it starts to sound randomly....which i can't stop & when i push button on transmitter it works with the frequency given but that sound never stops ....

It sounds like you need to start with some basic troubleshooting. The easiest way would be to put some serial prints in there that show the value of Ton on every loop through.

BJHenry:
That's fine, I was just replying to sterretje's comment about being able to power a speaker directly from the Arduino pins.
It sounds like you need to start with some basic troubleshooting. The easiest way would be to put some serial prints in there that show the value of Ton on every loop through.

ok give me some min ..i'll be checking it out in abit.

https://streamable.com/9zl4i

the problem is on pulseIn & Ton place....i don't know how to turn off the buzzer there as it default starts up....
when i push button it works but again the problem persists .....Please need help on this .

That's a start, although a clear explanation of exactly what is happening will be more helpful that posting a video. Were you pressing any of the buttons on the transmitter while you were taking that video?
I would remove the whole if/else tree and just directly print the 'Ton' value. I think you'll see it fluctuating all over the place.

BJHenry:
That's a start, although a clear explanation of exactly what is happening will be more helpful that posting a video. Were you pressing any of the buttons on the transmitter while you were taking that video?
I would remove the whole if/else tree and just directly print the 'Ton' value. I think you'll see it fluctuating all over the place.

uh hope that let you understand faster .
yeah on those cases where i was pushing the buttons the serial showed a constant of that print. Like pushing button 5 lets me show that "it works5" thingy.
Um,i actually can't get the picture on how to do that directly,i mean the Ton function.....Can you show me the code? will it fluctuate even after the direct value??

XessX:
yeah on those cases where i was pushing the buttons the serial showed a constant of that print. Like pushing button 5 lets me show that "it works5" thingy.

See, it would have been really helpful if you said that earlier. Details matter.
So based on what you've tried, is it fair to say that the receiver is correctly picking up on the button that is being pressed on the transmitter, and that there are no spurious results that show up when you aren't pressing any button?

BJHenry:
See, it would have been really helpful if you said that earlier. Details matter.
So based on what you've tried, is it fair to say that the receiver is correctly picking up on the button that is being pressed on the transmitter, and that there are no spurious results that show up when you aren't pressing any button?

yes when i'm not pushing the button...it seems like all the signals get messed up & starts that unanimous sound all at once from the buzzer :frowning: . So,what can be the solution to this???

Please try out this code:

unsigned long Ton = 0;
int rxPin = 7;
int buzzerPin = 9;
int btn = 0, pbtn = 0;
int freq = 0;
int pTon = 0;

void setup()
{
  pinMode(rxPin, INPUT);
}

void loop()
{

  Ton = pulseIn(rxPin, HIGH);
  if (Ton != pTon)
  {
    Serial.println(Ton);
    pTon = Ton;
  }
}

and post the results. Don't take a video, just copy the test straight from the Serial Monitor window. Please do it once with no buttons being pressed, and once where it shows the results from pressing a single button on the transmitter.

BJHenry:
Please try out this code:

unsigned long Ton = 0;

int rxPin = 7;
int buzzerPin = 9;
int btn = 0, pbtn = 0;
int freq = 0;
int pTon = 0;

void setup()
{
  pinMode(rxPin, INPUT);
}

void loop()
{

Ton = pulseIn(rxPin, HIGH);
  if (Ton != pTon)
  {
    Serial.println(Ton);
    pTon = Ton;
  }
}



and post the results. Don't take a video, just copy the test straight from the Serial Monitor window. Please do it once with no buttons being pressed, and once where it shows the results from pressing a single button on the transmitter.

tried it ...weird i'm not getting a single Ton result in serial....But no avail from it's usage... :confused:
And by using with the old code...and the Ton serial....
i get results like

it works3
241
it works4
122
it works5
67
it works2
511
it works3
242
it works5
40
it works3
270
it works5
36
it works3
301
it works5
17
it works4
116
it works3
261
it works3
it works5
72
it works4
139
............go on

XessX:
tried it ...weird i'm not getting a single Ton result in serial....But no avail from it's usage... :confused:
And by using with the old code...and the Ton serial....
i get results like

it works3
241
it works4
122
it works5
67
it works2
511
it works3
242
it works5
40
it works3
270
it works5
36
it works3
301
it works5
17
it works4
116
it works3
261
it works3
it works5
72
it works4
139
............go on

when i code in serial 9600 in setup ..i'm getting this result in com---

74
59
71
109
92
34
62
54
381
89
60
123
70
107
112
58
68
37
131
121
32
60
33
62
96
16
93
95
94
73
228
76
58
17
50
101
26
171
80.... & so on
it was same like this in the loop code without button push....

Ok, can you post a circuit diagram, and the details of the wireless module that you're using?