ATTiny85 & Tone function

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've been trying to use the Tone function to make a fire engine siren sound effect for a small toy for my son.

 

A simple nee naa nee naa type sound works fine with the Tone function however when I try to make a rising and falling sound effect it works but sounds very rough and not smooth - not nice to listen to at all and given we'll be subjected to this sound a lot I want to make it less annoying :-)

 

I've been using the 8mhz internal clock.  I want to keep the component count down as I don't have much space inside the toy to place this device.

 

Does anyone have any advice on how to make this sound better on an ATTiny85?   

 

The approach i've been using is this:

for(i=350;i<500;i++) {
        tone(speakerPin,i,50);
        delay(15);
      }
for(i=500;i>350;i--) {
        tone(speakerPin,i,50);
        delay(15);
      }

Last Edited: Mon. Jun 29, 2020 - 01:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

8bitMark wrote:
the Tone function

what "Tone function" is that?

 

Are we talking Arduino?

 

https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/

 

Please see Tip #1 (in my signature, below) for how to properly post source code.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

8bitMark wrote:
A simple nee naa nee naa type sound
Someone the other day was looking to make a "fire engine" sound for his very discerning 4 year old. Is this related to that somehow? If not then see if you can search it out. The solution is to play some kind of PCM WAV sample using PWM but, because the sample itself may take considerable space (could easily be 8K per second) you may need to pick a micro with more flash.

 

EDIT: oh wait a minute it was YOU:

 

https://www.avrfreaks.net/forum/attiny85-elm-chan-wav-file-audio-player-no-sound

 

so why are you cross posting??

 

PS tone() is surely a feature of "Arduino" so are you using some "core" to do Arduino stuff on a tiny85 ?

Last Edited: Mon. Jun 29, 2020 - 10:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the tip about posting code, sorry I'm new here.

Wasn't my intention to cross post. Yes it's related to same problem of making a fire engine sound but was trying to use a different solution. So first post was about wav files and this was about tone.

Yes I'm trying to use the Arduino tone function and attinycore for programming the attiny via Arduino IDE.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I doubt that with tones alone (unless fire engines really use pure sine/square/triangle waves??) that you are going to get anything sounding much like the "real thing". You really need sound samples. But, like I say, even at low sampling rates (like 8 bit, 8kHz mono) you are still looking at 8K per second. So that's why in your other thread I suggest trading up to a "big" AVR that has a reasonable amount of flash to hold the sample data - to my mind (for a dedicated one sound player) this is a whole lot simler than trying to inteface to SD or whatever simply to extend flash storage.

 

A "half way house" between the two might be to use small AVR and external memory but not SD but perhaps something with a simpler interface like AT45 Dataflash (used to be an Atmel product but they sold the rights to a 3rd party). Or maybe just any kind of I2C/SPI connected flash/EEPROM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note how a little whitespace helps legibility - rather than cramming everything together:

for( i=350; i<500; i++ ) {
        tone( speakerPin, i, 50 );   // Tone    for 50ms
        delay(15);                   // Silence for 15ms
}
for( i=500; i>350; i-- ) {
        tone( speakerPin, i, 50 );
        delay(15);
}

8bitMark wrote:
sounds very rough and not smooth

probably due to the delays?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Add an RC filter to the output to smooth the rough edges!

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Make it with PWM code.

 

Run the tiny85 @ 16MHz from  internal clk and timer 64MHz. (perhaps you need to calibrate)

 

With 2-5 tones at the same time (add the tones) I will guess that it can sound ok, and for sure way better than a simple IO

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you get the free "Audacity" sound editing tool and a WAV file of a fire engine it can do various types of analysis like fourier transform to tell you what the dominant frequencies are and other things like that. So as Sparrow says if you can work out the fundamental waves, their shape and frequency you might be able to do some kind of additive playback to try and recreate it without needing complete samples. But it sure would be easier just to take a whole sample and play that I guess (though it costs in storage space).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But not the same fun ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW I found this:

 

https://www.soundsnap.com/tags/firetruck

 

but of those I'm not really sure what a "normal" US fire engine sounds like!

 

UK ones sound like this: https://youtu.be/ZhYSuAjuRzs?t=3

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here are a bunch of audio sound samples from Whelen, a major siren manufacturer.

 

If you go with the option of just playing back a pre-recorded .WAV file they might serve you well.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW, how's your parts bin supply?

 

If you Google Siren Circuit and select images there are only a couple hundred simple siren audio signal generator circuits.

 

Many of them have one audio oscillator, with a second oscillator set up to modulate the audio frequency of the first oscillator.

 

The modulation signal can be a triangle, Ramp, Hi/Low, etc.

 

The specifics of the audio signal, (waveform, harmonics, base pitch, etc.), determine the "sound" of the siren, (as well as how it is modulated).

 

The Whelen sounds demonstrate some complex sound mixing, which is easily done when the signal is generated digitally.

The low frequency "rumble" helps to get the driver's attention to the higher pitched classic warbling sounds.

It makes people feel uncomfortable, and they pay more attention to the siren.

You'll probably not wish to add that feature to your son's toy!

 

JC

 

Edit:  This SA-400 page includes some more pre-recorded siren sound samples.

 

Last Edited: Mon. Jun 29, 2020 - 05:41 PM