Is there a command for playing a tone with volume control?

I have a speaker and a LED and I want the LED to dim according to the volume of the tone but I cant find any tone command that has volume control. So is there a command or library to do this or this is not a thing?

There are three different libraries listed here:

https://github.com/connornishijima?tab=repositories&q=volume

Kind of confusing that there are three to pick from, but there is a comparison table here:

https://github.com/connornishijima/arduino-volume1#volume-library-comparison

I have not heard of those libraries that in0 writes about. I only know ToneAC: https://github.com/teckel12/arduino-toneac

Hmm. None works for me. It just errors out
Using toneAC:
Here's what the console says

C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp: In function 'void toneAC(long unsigned int, uint8_t, long unsigned int, uint8_t)':
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:23:5: error: 'TIMSK1' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |     ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:23:19: error: 'OCIE1A' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |                   ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:23:15: error: '_BV' was not declared in this scope
   23 |     TIMSK1 |= _BV(OCIE1A);            // Activate the timer interrupt.
      |               ^~~
In file included from C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:8:
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp: In function 'void toneAC_playNote(long unsigned int, uint8_t)':
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.h:106:23: error: 'DDRB' was not declared in this scope
  106 |     #define PWMT1DREG DDRB
      |                       ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:31:3: note: in expansion of macro 'PWMT1DREG'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |   ^~~~~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.h:104:24: error: 'DDB1' was not declared in this scope; did you mean 'B1'?
  104 |     #define PWMT1AMASK DDB1
      |                        ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:31:20: note: in expansion of macro 'PWMT1AMASK'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                    ^~~~~~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:31:16: error: '_BV' was not declared in this scope
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                ^~~
In file included from C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:8:
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.h:105:24: error: 'DDB2' was not declared in this scope
  105 |     #define PWMT1BMASK DDB2
      |                        ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:31:38: note: in expansion of macro 'PWMT1BMASK'
   31 |   PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too).
      |                                      ^~~~~~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:33:27: error: 'CS10' was not declared in this scope
   33 |   uint8_t prescaler = _BV(CS10);                  // Try using prescaler 1 first.
      |                           ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:36:21: error: 'CS12' was not declared in this scope
   36 |     prescaler = _BV(CS12);                        // Set the 256 prescaler bit.
      |                     ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:40:3: error: 'ICR1' was not declared in this scope
   40 |   ICR1   = top;                                     // Set the top.
      |   ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:41:7: error: 'TCNT1' was not declared in this scope
   41 |   if (TCNT1 > top) TCNT1 = top;                     // Counter over the top, put within range.
      |       ^~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:42:3: error: 'TCCR1B' was not declared in this scope; did you mean 'TCC1'?
   42 |   TCCR1B = _BV(WGM13)  | prescaler;                 // Set PWM, phase and frequency corrected (top=ICR1) and prescaler.
      |   ^~~~~~
      |   TCC1
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:42:16: error: 'WGM13' was not declared in this scope
   42 |   TCCR1B = _BV(WGM13)  | prescaler;                 // Set PWM, phase and frequency corrected (top=ICR1) and prescaler.
      |                ^~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:43:3: error: 'OCR1A' was not declared in this scope
   43 |   OCR1A  = OCR1B = top / _tAC_volume[volume - 1];   // Calculate & set the duty cycle (volume).
      |   ^~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:43:12: error: 'OCR1B' was not declared in this scope
   43 |   OCR1A  = OCR1B = top / _tAC_volume[volume - 1];   // Calculate & set the duty cycle (volume).
      |            ^~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:44:3: error: 'TCCR1A' was not declared in this scope; did you mean 'TCC1'?
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |   ^~~~~~
      |   TCC1
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:44:16: error: 'COM1A1' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:44:30: error: 'COM1B1' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                              ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:44:44: error: 'COM1B0' was not declared in this scope
   44 |   TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC).
      |                                            ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp: In function 'void noToneAC()':
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:48:3: error: 'TIMSK1' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |   ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:48:18: error: 'OCIE1A' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |                  ^~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:48:14: error: '_BV' was not declared in this scope
   48 |   TIMSK1 &= ~_BV(OCIE1A);        // Remove the timer interrupt.
      |              ^~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:49:3: error: 'TCCR1B' was not declared in this scope; did you mean 'TCC1'?
   49 |   TCCR1B  = _BV(CS11);           // Default clock prescaler of 8.
      |   ^~~~~~
      |   TCC1
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:49:17: error: 'CS11' was not declared in this scope
   49 |   TCCR1B  = _BV(CS11);           // Default clock prescaler of 8.
      |                 ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:50:3: error: 'TCCR1A' was not declared in this scope; did you mean 'TCC1'?
   50 |   TCCR1A  = _BV(WGM10);          // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
      |   ^~~~~~
      |   TCC1
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:50:17: error: 'WGM10' was not declared in this scope
   50 |   TCCR1A  = _BV(WGM10);          // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
      |                 ^~~~~
In file included from C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:8:
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.h:104:24: error: 'DDB1' was not declared in this scope; did you mean 'B1'?
  104 |     #define PWMT1AMASK DDB1
      |                        ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:51:21: note: in expansion of macro 'PWMT1AMASK'
   51 |   PWMT1PORT &= ~_BV(PWMT1AMASK); // Set timer 1 PWM pins to LOW.
      |                     ^~~~~~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.h:105:24: error: 'DDB2' was not declared in this scope
  105 |     #define PWMT1BMASK DDB2
      |                        ^~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:52:21: note: in expansion of macro 'PWMT1BMASK'
   52 |   PWMT1PORT &= ~_BV(PWMT1BMASK); // Other timer 1 PWM pin also to LOW.
      |                     ^~~~~~~~~~
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp: At global scope:
C:\Users\chirp\OneDrive\Documents\Arduino\libraries\toneAC\toneAC.cpp:55:4: error: expected constructor, destructor, or type conversion before '(' token
   55 | ISR(TIMER1_COMPA_vect) {                 // Timer interrupt vector.
      |    ^
exit status 1
Error compiling for board Adafruit Metro M0 Express (SAMD21).

Which Arduino board do you use ?

It isn't a Arduino Board. Its a Adafruit Metro M0 Express

Adafruit METRO M0 Express: https://www.adafruit.com/product/3505.

That is the same processor as the Arduino MKR Zero.
They are called the "samd" boards, Arduino calls them "ArduinoCore-samd".
The ArduinoSound library is for those processors: https://www.arduino.cc/en/Reference/ArduinoSound, but then you need a amplifier with I2S.

Some have made a library to play a file with a speaker: https://github.com/hydronics2/SamdAudio. But that library does not have a simple tone() function.

Do you have a speaker or a piezo for sound and do you have a resistor with it ?

I'm very sorry, but I can not find a simple tone() function for the "samd" boards with volume control :cry:

Those libraries are using AVR processor registers so they will only work on AVR processors (UNO, Nano, and maybe Mega or Leonardo/Micro).

For some reason TimerFreeTone worked.

Where i found it

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.