Frequency sweep code

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);}
}

I don't know if it matters but am using Arduino 22 and a Duemilanove. -Eric

I believe tone does not work correctly for frequencies below about 33 Hz.

ok so do you have an idea on how to sweep from max to min. Or is that your explanation of the audio anomalies when I load the code starting at zero?

ok so if I change min to about 55 hz the fun but weird sounds go away

Is this what you’re trying to do…

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

I suspect so.

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?

The parameter is "unsigned int" which limits the frequency to 65535 Hz.

Needless to say using this code below the range produces some musical arpeggio artifacts that I will explore further.

Probably the millis / timer 0 interrupt running. Unfortunately, disabling timer 0 will also stop delay from working.

your sweep is ok... ... can it go faster with the tone function? or is it easier at this point to incorporate a pot/ sensor?

by the way thanks for your help!!

E3po: ... can it go faster with the tone function?

What do you mean by "go faster"?

or is it easier at this point to incorporate a pot/ sensor?

Can't say. This is the first you've mentioned a pot / sensor. You'll have to provide some context.

by the way thanks for your help!!

You are welcome.

@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)

b

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.

Oh to be goal oriented...