Volume control of buzzer - electret microphone input

Hello! I have a problem in a project and I need some help if it is possible.

The purpose is to control the volume of a buzzer that is connected to an Arduino Leonardo. The sound is read from an electret microphone. If the sound exceeds a threshold then, I want to instantly lower the volume of the buzzer (for example something like Skype calls, when someone calls you and you listen to a song , and imidiatelly the volume drops).

I have connected the buzzer and the mic as the following pictures.

The PROBLEM is that the command ToneAC needs to be first implemented, to continue to the analogRead. I want these commands to happen at the same time, in order to control the volume from the values that i get from the AnalogRead.

Below is the code:

#include <toneAC.h>
int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
}



//void setup() {} // Nothing to setup, just start playing!

void loop() {

  toneAC(700,5,8000); // Play the frequency (150 Hz to 15 kHz).
  sensorValue = analogRead(sensorPin);
  Serial.print("sensor = " );                       
  Serial.println(sensorValue);


  toneAC(0); // Turn off toneAC, can also use noToneAC().

  while(1); // Stop.
}

Thanks in advance!

fritzing.png

Good luck reading that electret mic with the default analogReference.
You would be lucky to read 200 out of 1023.
You need to invoke analogReference(INTERNAL) to change your analog input voltage scale to 0v to 1.1V
This will increase the readable values from the mic but you should still add an amplifier
Here's a one transistor amp:

Here's a two stage op amp amplifier (much better)

I've never used TONEAC so I'll take a look at your code.

The PROBLEM is that the command ToneAC needs to be first implemented, to continue to the analogRead. I want these commands to happen at the same time, in order to control the volume from the values that i get from the AnalogRead.

If your intention with this sketch is to generate a tone with toneAC and then measure the level of that tone while it is playing with analogRead, check the level, then lower the tone if greater than some threshold, then you can forget about that if you only have one UNO. The CPU can play the tone OR it can read the analog input. It CANNOT , however, do BOTH at EXACTLY the same
instant because it is not a multicore processor. It can only run ONE instruction at a time. Find some other way to generate the tone that does NOT involve the processor that needs to be monitoring the sound level of that tone. The mic amplifier info of the last post only addresses the mic input issue and has nothing at all to do with your toneAC issue. Think about what I just said and read it twice if it doesn't sink in the first time.

Firstly thank you for your response and advise :slight_smile:
The sequence that you mentioned is exactly what I want to do. I understood what you said. Someone told me that maybe I could do this with an interrupt.
It isn't possible even with this way, right?

Do you understand the concept of ONE INSTRUCTION AT A TIME ?
If you can figure out how to use an interrupt to measure the amplitude of a signal generated by OTHER INSTRUCTIONS WITHOUT
INTERRUPTING those instructions (you don't know what the word "INTERRUPT" means right ?) they more power to you.
I should have prefaced my first reply with the question" This is a TRICK question, right ?"

Attached is an Excel of the analog input of an UNO sampling the toneAC signal running the toneAC demo at 40,000 samples per second logging the data to an SD card using the AnalogBinLogger sketch. If you run the toneAC demo in the examples folder, you will see that it plays an ascending tone , followed by the classic "Shave and a haircut, two bits" melody.

toneAC DEMO.csv (1.34 MB)

toneAC DEMO-2.CSV (1.6 MB)