Hey all I am a beginner and having a blast by the way. I am currently trying to make music and or test equipment and have fun along the way. I was curious if anyone had good code on a freq sweep or an explanation of the fun anomalies my know nothing code generates. I don’t know where to begin so I changed some variables of a apple led fade code I found and like what happened but is not what I expected. I know the “tone” command makes single tones analogous to the number indicated but when my desired results were not achieved I changed what I had until IT was cool. I was looking for a 20Hz to 20 kHz. A little aside, I was also looking to find out if I use an old square wave to sine wave circuit to smooth out the grainy texture would that work? Any help would rock.
-Eric
code is as follows…
int i = 0;
void setup ()
{
}
void loop(){
for (i = 0; i < 100; i++) {
tone(13, i);
delay(100);}
for (i = 100; i > 0; i–) {
tone(13, i);
delay(10);}
}
void setup ()
{
}
void loop(){
int i;
for (i = 60; i <= 20000; i++) {
tone(13, i);
delay(100);}
for (i = 20000; i >= 60; i--) {
tone(13, i);
delay(10);}
}
ok so if I change min to about 55 hz the fun but weird sounds go away
honestly I am still not sure why I even have an arduino. However I do and can't help trying to figure out why. Needless to say using this code below the range produces some musical arpeggio artifacts that I will explore further. Thanks very much! Do you know the upper limit of the tone function code so I might explore that for fun?
@E3po: "using this code below the range produces some musical arpeggio artifacts" - the reason for this is because below the limit for tone() (31 Hz, in this case), the values that are sent to tone() go beyond what the microcontroller is expecting. The microcontroller then changes the value to something it can use, and hence you'll get a seemingly random tone.
The bottom line is that, for the 16 MHz boards, the lowest frequency is 31 Hz.
(for the technical details: e.g. Timer2, below 31 Hz, the prescalar is at ck/1024. The OCR value will be above 255, so the value will be wrapped to freq % 256. e.g. @30 Hz, OCR = 259 % 256 = 3 -> this will produce a 1953 Hz tone, which is far from the 30 Hz expected)
Big thanks to the replies. The initial quest was to generate code to do a frequency sweep, but the anomalies are still as interesting to me. As I am dabbling in this I am overwhelmed with possibilities.