Loading...
Pages: [1]   Go Down
Author Topic: NewTone Library - Plug-in replacement for Tone library - Better, smaller, faster  (Read 2042 times)
0 Members and 1 Guest are viewing this topic.
Toledo, OH
Offline Offline
Sr. Member
****
Karma: 16
Posts: 410
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why another tone library?
I'd already written a highly optimized toneAC library because I needed higher volume, volume control, higher frequency, and better quality.  However, toneAC uses fixed timer 1 PWM pins so it's not as flexible.  Making toneAC work like tone was simple and there would be several advantages over the tone library, so I spent an hour and made NewTone from the toneAC library.

What does it do better/differently?
  • About 1,200 bytes smaller code size than the Tone library.
  • Faster execution time.
  • Exclusive use of port registers for fastest and smallest code.
  • Higher quality sound output than tone library.
  • Uses timer 1 which may free up conflicts with the tone library.

How do I use it?
It's a plug-in replacement for the standard tone library.  Add the include, use NewTone() instead of tone() and noNewTone() instead of noTone() to enjoy the benefits.  If you're running out of program space or have a timer conflict with the tone library, this is the library for you.  See the sketch below for an example.

Download: NewTone v1.0

If you're looking to save even more space and more features like almost twice as loud output and volume control, check out my toneAC library as well.

Example sketch
Code:
#include <NewTone.h>

#define TONE_PIN 2 // Pin you have speaker/piezo connected to (be sure to include a 100 ohm resistor).

// Melody (liberated from the toneMelody Arduino example sketch by Tom Igoe).
int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };

void setup() {} // Nothing to setup, just start playing!

void loop() {
  for (unsigned long freq = 125; freq <= 15000; freq += 10) {  
    NewTone(TONE_PIN, freq); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps).
    delay(1); // Wait 1 ms so you can hear it.
  }
  noNewTone(TONE_PIN); // Turn off the tone.

  delay(1000); // Wait a second.

  for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array.
    int noteDuration = 1000/noteDurations[thisNote];
    NewTone(TONE_PIN, melody[thisNote], noteDuration); // Play thisNote for noteDuration.
    delay(noteDuration * 4 / 3); // Wait while the tone plays in the background, plus another 33% delay between notes.
  }

  while(1); // Stop (so it doesn't repeat forever driving you crazy--you're welcome).
}

Tim
« Last Edit: January 21, 2013, 01:55:54 am by teckel » Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

SE USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 3651
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does it work with mega?
Logged


Toledo, OH
Offline Offline
Sr. Member
****
Karma: 16
Posts: 410
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

does it work with mega?

Yup, should work with all ATmega microcontrollers.  Tested on my Uno and Teensy 2.0, which are totally different.  Doesn't hard code port register values so it should work on all platforms.  I use the same technique with NewPing and compatibility is universal for any ATmega microcontroller.  Will be simple to confirm it works.

Tim
« Last Edit: January 21, 2013, 01:57:15 am by teckel » Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi have been trying to adapt to use it on Timer "4" on the mega
As i can not use it on Timer 1 as it then conflicts width my PWM signals on the MEGA 2560  ;-(
I am trying to drive a speedometer
as you see i have just tryed to change the 1 to 4 not sure i know what i am doing
Any reply greatly appriciatet ;-)

I have changed here

NewTone.h

#define TIMSK4 TIMSK //#define TIMSK1 TIMSK

NewTone.cpp

ICR4    = top;                     // Set the top.
  if (TCNT4 > top) TCNT4 = top;      // Counter over the top, put within range.
  TCCR4B  = _BV(WGM40)  | prescaler; // Set PWM, phase and frequency corrected (ICR1) and prescaler.
  TCCR4A  = _BV(COM4B0);
  TIMSK4 |= _BV(OCIE4A);             // Activate the timer interrupt.

TIMSK4 &= ~_BV(OCIE4A);   // Remove the timer interrupt.
  TCCR4B  = _BV(CS40);      // Default clock prescaler of 8.
  TCCR4A  = _BV(WGM40);     // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).

ISR(TIMER4_COMPA_vect) { // Timer interrupt vector.
Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 16
Posts: 410
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi have been trying to adapt to use it on Timer "4" on the mega
As i can not use it on Timer 1 as it then conflicts width my PWM signals on the MEGA 2560  ;-(
I am trying to drive a speedometer
as you see i have just tryed to change the 1 to 4 not sure i know what i am doing
Any reply greatly appriciatet ;-)

I have changed here

NewTone.h

#define TIMSK4 TIMSK //#define TIMSK1 TIMSK

NewTone.cpp

ICR4    = top;                     // Set the top.
  if (TCNT4 > top) TCNT4 = top;      // Counter over the top, put within range.
  TCCR4B  = _BV(WGM40)  | prescaler; // Set PWM, phase and frequency corrected (ICR1) and prescaler.
  TCCR4A  = _BV(COM4B0);
  TIMSK4 |= _BV(OCIE4A);             // Activate the timer interrupt.

TIMSK4 &= ~_BV(OCIE4A);   // Remove the timer interrupt.
  TCCR4B  = _BV(CS40);      // Default clock prescaler of 8.
  TCCR4A  = _BV(WGM40);     // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).

ISR(TIMER4_COMPA_vect) { // Timer interrupt vector.


It doesn't just conflict with the PWM signals, it replaces them.  It's by design.  Instead, why not just use PWM for timer 4 for whatever else you're doing?

One of the main purposes of NewTone is to use a different timer than the standard tone library.

In any case, I don't really know the ATmega2560 to know off the top of my head if there's any differences between timer 1 and timer 4.  Are they identical?  For example, if timer 4 doesn't have all the features of timer 1, it won't work.  It doesn't appear it's identical as it has 3 pins tied to timer 4 while timer 1 only has 2.

Without diving into the ATmega2560 technical documentation, I'll be hard-pressed to help.

Instead, I would suggest using my ToneAC library (there's an alternate version which uses timer 2).  It's superior to NewTone anyway and is already written to use timer 2 and can be assigned to any pins.

Tim
« Last Edit: April 11, 2013, 05:02:39 pm by teckel » Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Thanks for you reply
but i have solved it

Width great help from this Forum :-)

http://arduino.cc/forum/index.php/topic,157602.0.html

Thanks
Ole
Logged

US
Offline Offline
Full Member
***
Karma: 3
Posts: 133
Electronics are the new Legos
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

this could be awesome if the library was also Due compatible
Logged


Toledo, OH
Offline Offline
Sr. Member
****
Karma: 16
Posts: 410
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

this could be awesome if the library was also Due compatible

I don't find the Due relevant, so I don't consider support for it.  Also, as NewTone uses port registers and direct timer calls, it doesn't really work well for the Due which is totally different than the ATmega.

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

US
Offline Offline
Full Member
***
Karma: 3
Posts: 133
Electronics are the new Legos
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't find the Due relevant, so I don't consider support for it. 

Supposedly it is the future as they did not stick with a compatible processor.  Many have written suitable timer control libraries so the work would not be from datasheet scratch.
Logged


Pages: [1]   Go Up
Print
 
Jump to: