arduino-volume1
arduino-volume1 copied to clipboard
Precise hertz
Allow floating point frequencies for tone method
Hi Connor,
The volume library is awesome! I forgot to include a code example of using the overloaded tone method. Here is an example that demonstrates use of both the integer and floating point tone methods:
#include "Volume.h" // Include the Volume library
Volume vol; // Audio output is plugged into pin 5 and Gnd
void setup()
{
vol.begin(); // After calling this, delay() and delayMicroseconds will no longer work
// correctly! Instead, use vol.delay() and vol.delayMicroseconds() for
// the correct timing
vol.setMasterVolume(1.00);
vol.delay(500);
}
bool run_song=true;
float notes[107] = {
130.81, //C3
138.59, //C#3/Db3
146.83, //D3
155.56, //D#3/Eb3
164.81, //E3
174.61, //F3
185.00, //F#3/Gb3
196.00, //G3
207.65, //G#3/Ab3
220.00, //A3
233.08, //A#3/Bb3
246.94, //B3
};
void play(float frequency){
//This function demonstrates that vol.tone accepts
//floating point frequencies variables
int v = 255;
while (v>0){
vol.tone(frequency, v);
vol.delay(3);
v-=2;
}
}
void play(int frequency){
//This function demonstrates that vol.tone accepts
//integer based frequencies
int v = 255;
while (v>0){
vol.tone(frequency, v);
vol.delay(3);
v-=2;
}
}
void loop() {
if (run_song){
// Play integer based freqneucies
for (int i=0;i<=12;i++){
play(int(notes[i]));
delay(500);
}
//play floating point frequencies
for (int i=0;i<=12;i++){
play(notes[i]);
delay(500);
}
run_song=false;
}
}