Audio Tones with TVOut

Hi all, first post!

I'm really enjoying all that I can do with the Arduino itself and the TVOut Library. But I have a few problems and questions for those with more experience than I have.

I know that you can output sound through the use of "tone" in TVOut, but I'm not certain how to go about it. Whenever I try to do it, only small sounds come out of the speaker and for brief periods of time. They're actually more like small pops that are audible only when a tone starts. Sometimes nothing comes out at all. Everything compiles fine and I'm using the usual format for tone:

tone(pin,frequency,time)

Another question I have is, how can you write the code in a way that tones will play (music or sound effects, etc.) while something else is happening on the screen? I see videos of games developed for arduino with music playing fine along with many different things happening on the screen at once. It makes me wonder what I'm missing. Any help is appreciated!

Foxden

It makes me wonder what I'm missing.

Me, too. Without seeing your code, though, we'll never know, will we?

Well then,
I've taken everything else out of the sketch except for the very basics. Everything to get the TVout running is there, plus a simple tone section in void loop:

#include <TVout.h>

TVout TV;

void setup()
{
  TV.begin(NTSC,127,97);
}

void loop()
{
  tone(11,500,500);
  delay(1000);
}

My last post still holds true even with this simple sketch.

1 Like

The TVout class has a tone() method. Have you tried that method?

I thought so but I'm not sure that I know exactly how it works. All I've seen is that it's coded in the exact same way as the normal tones are. How can the compiler/arduino tell the difference? Can you walk me through the use of the TVout method?

Can you walk me through the use of the TVout method?

TV.tone(freq, duration);

:blush: Duh. That makes me feel stupid. lol Thanks so much! Still a beginner obviously :cold_sweat:

That makes me feel stupid.

Don't be. I don't think that class is all that well documented. I presume, from your response, that that tone() method worked, then.

Yes. Thank you very much! XD

Well I sort of take back my last statement. The sound outputs, but the timing is a bit sluggish and the sound quality is bad. It pops and crackles during the time that it's playing a tone. It may have something to do with the way it's coded. The image on screen also flickers while the tone is played as well. What I'm going for is a motorcycle-like sound that increases in frequency up to a certain number and then resets the frequency (revsound) to a lower value and then it starts a similar loop below it. Is this too much for the Arduino to handle? I wouldn't think so but I still could use some help I guess... :sweat_smile:
Here's the code:

#include <TVout.h>
#include "LightCycle.h"
#include <stdio.h>

TVout TV;

void setup()
{
  TV.begin(NTSC,127,97);
  TV.bitmap(0,0,LightCycle);
 
  int revsound = 100;
  while (revsound < 300)
  {
    TV.tone(revsound,65);
    delay(65);
    revsound ++;
  }
  
  revsound = 75;
  
  while (revsound < 275)
  {
    TV.tone(revsound,60);
    delay(60);
    revsound ++;
  }
  
  revsound = 50;
  
  while (revsound < 250)
  {
    TV.tone(revsound,60);
    delay(60);
    revsound ++;
  }
    noTone;
}

void loop()
{
  
}

Doesn't the TVout library have its own version of delay()? Have you tried that instead of the standard delay()? It's less likely to mess with the timing, I would think.

It's a little better now, but the timing is still a bit sluggish and the popping isn't as audible, but it's still there.

Have a go at the Blink Without Delay example to learn some basics, especially with time.

Then you start and stop things like sound while in between do other things.

Hi there,

I'm sorry for digging up this old topic but I have the same problem as foxden313002.

The TVOut library plays singles tones just fine. But when it comes to melodies, it sounds choppy.

I am pretty sure that this is because the Uno only has 3 hardware timers and the TVOut library uses all of them to create the video signal using interrupts.
When the Arduino generates a video signal like that, the accuracy of the timing functions is equal to the frame time.

Which results in an accuracy for the "delay()" and "millis()" methods of 16ms(NTSC) or 20ms(PAL).
Even when using the methods that come with the library.

Could this be the cause for the choppy music playback?
And if so, is there any way around that except using a Mega(because it has more timers) or two Unos where one would act as a sound chip?