How to control the volume of a buzzer while using the tone() function?

Hello guys, this is my code:

#define A0  27.5
#define AS0 29.1
#define B0  30.8
#define C1  32.7
#define CS1 34.6
#define D1  36.7
#define DS1 38.8
#define E1  41.2
#define F1  43.6
#define FS1 46.2
#define G1  49
#define GS1 51.9
#define A1  55
#define AS1 58.3
#define B1  61.7
#define C2  65.4
#define CS2 69.2
#define D2  73.4
#define DS2 77.8
#define E2  82.4
#define F2  87.3
#define FS2 92.5
#define G2  98
#define GS2 103.8
#define A2  110
#define AS2 116.5
#define B2  123.5
#define C3  130.8
#define CS3 138.6
#define D3  146.8
#define DS3 155.6
#define E3  164.8
#define F3  174.6
#define FS3 185
#define G3  196
#define GS3 207.7
#define A3  220
#define AS3 233.1
#define B3  246.9
#define C4  261.6
#define CS4 277.2
#define D4  293.7
#define DS4 311.1
#define E4  329.6
#define F4  349.2
#define FS4 370
#define G4  392
#define GS4 415.3
#define A4  440
#define AS4 466.2
#define B4  493.9
#define C5  523.3
#define CS5 554.4
#define D5  587.3
#define DS5 622.3
#define E5  659.3
#define F5  698.5
#define FS5 740
#define G5  784
#define GS5 830.6
#define A5  880
#define AS5 932.3
#define B5  987.8
#define C6  1046.5
#define CS6 1108.7
#define D6  1174.7
#define DS6 1244.5
#define E6  1318.5
#define F6  1396.9
#define FS6 1480
#define G6  1568
#define GS6 1661.2
#define A6  1760
#define AS6 1864.7
#define B6  1975.5
#define C7  2093
#define CS7 2217.5
#define D7  2349.3
#define DS7 2489
#define E7  2637
#define F7  2793.8
#define FS7 2960
#define G7  3136
#define GS7 3322.4
#define A7  3520
#define AS7 3729.3
#define B7  3951.1
#define C8  4186
const int buzzerPin = 9;
const int buttonPin = 10;
const int melody[] = {F6, A6, E6, D6, AS4, F5, A5, E5, D5, 0, D7};
const int noteDurations[] = {2, 2, 4, 8, 8, 2, 2, 4, 4, 4, 7};
const int tempo = 150;
const int volume = 256;
void setup(){
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
boolean buttonState = digitalRead(buttonPin);
if(buttonState == LOW){
for(int thisNote = 0; thisNote <= 10; thisNote++){ 
int noteDuration = noteDurations[thisNote] * 60000/tempo;
int i;
if(melody[i] != 0){
for(i = 0; i <= melody[i]*noteDuration/1000; i++){
analogWrite(buzzerPin, volume);
delayMicroseconds(500000/melody[i]);
analogWrite(buzzerPin, 0);
delayMicroseconds(500000/melody[i]);
}
}
else{
noTone(buzzerPin); 
delay(noteDuration);
}
noTone(buzzerPin);
}
}
else{
noTone(buzzerPin);
}
}

When I press the button, the buzzer makes a cracking sound. I understand it’s because of the delayMicroseconds() for loop, but I used that as a replacement for the tone() function. I wanted to know how to control the volume while using the tone() function in PWM pins. I appreciate any solutions to this :slight_smile:
And I know I could’ve just used a resistor, but if I need to modify this code, I would like some notes louder than others.

Your duplicate post has been deleted

UKHeliBob:
Your duplicate post has been deleted

I'm sorry, I sort of had a connection error so it didn't save. So I copied it and posted it again and I didn't think it was duplicate

look at the top or bottom of the thread. There is a box "NO ALERTS OR EMAILS". Click that and set for the options you wish.

Paul

Don’t the “A0”, “B0”, “A1” etc definitions cause compilation problems?

It compiled just fine for me. But when I uploaded it, it didn't work as what I wished.

int i;
if(melody[i] != 0){

Hmmm

TheMemberFormerlyKnownAsAWOL:

int i;

if(melody[i] != 0){



Hmmm

Okay, that means the 0 symbolizes for NO sound. So if it's not equal to 0, it plays the sound. If it is, that's noTone();

I wasn’t interested in the zero.
I was interested in i

TheMemberFormerlyKnownAsAWOL:
I wasn't interested in the zero.
I was interested in i

i indicates one of the entries in melody.

And what is its value?

TheMemberFormerlyKnownAsAWOL:
And what is its value?

Oh, I get it! I'm supposed to use a different variable for the for() loop so it doesn't infere. However my problem isn't fixed. It still doesn't work

pon00009:
. However my problem isn’t fixed. It still doesn’t work

Thoughts and prayers.

#define A0  27.5
#define AS0 29.1
#define B0  30.8
#define C1  32.7
#define CS1 34.6
#define D1  36.7
#define DS1 38.8
#define E1  41.2
#define F1  43.6
#define FS1 46.2
#define G1  49
#define GS1 51.9
#define A1  55
#define AS1 58.3
#define B1  61.7
#define C2  65.4
#define CS2 69.2
#define D2  73.4
#define DS2 77.8
#define E2  82.4
#define F2  87.3
#define FS2 92.5
#define G2  98
#define GS2 103.8
#define A2  110
#define AS2 116.5
#define B2  123.5
#define C3  130.8
#define CS3 138.6
#define D3  146.8
#define DS3 155.6
#define E3  164.8
#define F3  174.6
#define FS3 185
#define G3  196
#define GS3 207.7
#define A3  220
#define AS3 233.1
#define B3  246.9
#define C4  261.6
#define CS4 277.2
#define D4  293.7
#define DS4 311.1
#define E4  329.6
#define F4  349.2
#define FS4 370
#define G4  392
#define GS4 415.3
#define A4  440
#define AS4 466.2
#define B4  493.9
#define C5  523.3
#define CS5 554.4
#define D5  587.3
#define DS5 622.3
#define E5  659.3
#define F5  698.5
#define FS5 740
#define G5  784
#define GS5 830.6
#define A5  880
#define AS5 932.3
#define B5  987.8
#define C6  1046.5
#define CS6 1108.7
#define D6  1174.7
#define DS6 1244.5
#define E6  1318.5
#define F6  1396.9
#define FS6 1480
#define G6  1568
#define GS6 1661.2
#define A6  1760
#define AS6 1864.7
#define B6  1975.5
#define C7  2093
#define CS7 2217.5
#define D7  2349.3
#define DS7 2489
#define E7  2637
#define F7  2793.8
#define FS7 2960
#define G7  3136
#define GS7 3322.4
#define A7  3520
#define AS7 3729.3
#define B7  3951.1
#define C8  4186
const int buzzerPin = 9;
const int buttonPin = 10;
const int melody[] = {F6, A6, E6, D6, AS4, F5, A5, E5, D5, 0, D7};
const int noteDurations[] = {2, 2, 4, 8, 8, 2, 2, 4, 4, 4, 7};
const int tempo = 150;
const int volume = 500;
void setup(){
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
boolean buttonState = digitalRead(buttonPin);
if(buttonState == LOW){
for(int thisNote = 0; thisNote <= 10; thisNote++){ 
int noteDuration = noteDurations[thisNote] * 60000/tempo;
int i;
if(melody[i] != 0){
for(int x = 0; x <= melody[i]*noteDuration/1000; x++){
analogWrite(buzzerPin, volume);
delayMicroseconds(500000/melody[i]);
analogWrite(buzzerPin, 0);
delayMicroseconds(500000/melody[i]);
}
}
else{
noTone(buzzerPin); 
delay(noteDuration);
}
noTone(buzzerPin);
}
}
else{
noTone(buzzerPin);
}
}

This is my new code. (Which still fails).

int i;
if(melody[i] != 0){

We’ve been here before.

Okay, I wanted to just ask how to use the tone() function while controlling the volume of the piezo buzzer on a PWM analog output pin.

To your question, read this

As far as i is concerned in

 int i;
if(melody[i] != 0){...

the question remains. You declare a local variable i without assigning any value to it and you then use it. What index do you think will be used in the comparison?

J-M-L:
To your question, read this

As far as i is concerned in

 int i;

if(melody[i] != 0){...



the question remains. You declare a local variable i without assigning any value to it and you then use it. What index do you think will be used in the comparison?

I used "thisNote" instead. I got it, but still not working :frowning: Also, I didn't say I wanted to adjust the volume of the entire thing, just on specific notes. To do that, I will need to modify the code. Does anyone know how?

pon00009:
I used “thisNote” instead. I got it, but still not working

No, still not understanding this.

Code please.

TheMemberFormerlyKnownAsAWOL:
No, still not understanding this.

Code please.

Okay…

#define A0  27.5
#define AS0 29.1
#define B0  30.8
#define C1  32.7
#define CS1 34.6
#define D1  36.7
#define DS1 38.8
#define E1  41.2
#define F1  43.6
#define FS1 46.2
#define G1  49
#define GS1 51.9
#define A1  55
#define AS1 58.3
#define B1  61.7
#define C2  65.4
#define CS2 69.2
#define D2  73.4
#define DS2 77.8
#define E2  82.4
#define F2  87.3
#define FS2 92.5
#define G2  98
#define GS2 103.8
#define A2  110
#define AS2 116.5
#define B2  123.5
#define C3  130.8
#define CS3 138.6
#define D3  146.8
#define DS3 155.6
#define E3  164.8
#define F3  174.6
#define FS3 185
#define G3  196
#define GS3 207.7
#define A3  220
#define AS3 233.1
#define B3  246.9
#define C4  261.6
#define CS4 277.2
#define D4  293.7
#define DS4 311.1
#define E4  329.6
#define F4  349.2
#define FS4 370
#define G4  392
#define GS4 415.3
#define A4  440
#define AS4 466.2
#define B4  493.9
#define C5  523.3
#define CS5 554.4
#define D5  587.3
#define DS5 622.3
#define E5  659.3
#define F5  698.5
#define FS5 740
#define G5  784
#define GS5 830.6
#define A5  880
#define AS5 932.3
#define B5  987.8
#define C6  1046.5
#define CS6 1108.7
#define D6  1174.7
#define DS6 1244.5
#define E6  1318.5
#define F6  1396.9
#define FS6 1480
#define G6  1568
#define GS6 1661.2
#define A6  1760
#define AS6 1864.7
#define B6  1975.5
#define C7  2093
#define CS7 2217.5
#define D7  2349.3
#define DS7 2489
#define E7  2637
#define F7  2793.8
#define FS7 2960
#define G7  3136
#define GS7 3322.4
#define A7  3520
#define AS7 3729.3
#define B7  3951.1
#define C8  4186
const int buzzerPin = 9;
const int buttonPin = 10;
const int melody[] = {F6, A6, E6, D6, AS4, F5, A5, E5, D5, 0, D7};
const int noteDurations[] = {2, 2, 4, 8, 8, 2, 2, 4, 4, 4, 7};
const int tempo = 150;
const int volume = 500;
void setup(){
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
boolean buttonState = digitalRead(buttonPin);
if(buttonState == LOW){
for(int thisNote = 0; thisNote <= 10; thisNote++){ 
int noteDuration = noteDurations[thisNote] * 60000/tempo;
if(melody[thisNote] != 0){
tone(buzzerPin, melody[thisNote], noteDuration);
}
}
else{
noTone(buzzerPin); 
}
noTone(buzzerPin);
}
}
else{
noTone(buzzerPin);
}
}