Go Down

Topic: toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc. (Read 17 times) previous topic - next topic

countofangels

#35
Jan 24, 2013, 09:20 pm Last Edit: Jan 24, 2013, 09:30 pm by countofangels Reason: 1
First of all this library is awesome, and I'm very pleased that I found it. Registered just for this.

Secondly though, I'm running into trouble because of the 9 & 10 pin requirements. (Arduino Uno)

Is there a way someone could point in the right direction to modify the library to use pins 5 & 6? (or other PWM pins in general?)  Or perhaps I misunderstand and the relevant timer is ONLY accessible on 9 & 10.

Any reason we couldn't use Timer 0 or Timer 2, per: http://playground.arduino.cc/Main/TimerPWMCheatsheet


Alternately, I think it'd be an excellent addition to the library to be able to set options to switch between all possible PWM pins, just to give a little bit more flexibility.

teckel


First of all this library is awesome, and I'm very pleased that I found it. Registered just for this.

Secondly though, I'm running into trouble because of the 9 & 10 pin requirements. (Arduino Uno)

Is there a way someone could point in the right direction to modify the library to use pins 5 & 6? (or other PWM pins in general?)  Or perhaps I misunderstand and the relevant timer is ONLY accessible on 9 & 10.

Any reason we couldn't use Timer 0 or Timer 2, per: http://playground.arduino.cc/Main/TimerPWMCheatsheet

Alternately, I think it'd be an excellent addition to the library to be able to set options to switch between all possible PWM pins, just to give a little bit more flexibility.


The library uses timer 1, which are hard-coded to the PWM pins 9 & 10 on the Arduino Uno.  It's not that my library hard-codes this, this is how the ATmega microcontroller is designed.  While it's possible to have the timer interrupt run a small routine that would allow you to use different pins, the speed and accuracy would suffer.  This isn't a big deal if you're doing human-audible frequencies as the Arduino would be plenty fast to do this.  But, toneAC is designed to do frequencies well beyond even typically ultrasonic range (like 1 MHz) and do so with precise accuracy.

If it was just about flexibility, I would have written the library that way from the beginning (I basically already did when I wrote the NewTone library, which is based on toneAC but allows you to specify a pin).  There's no way to keep the speed and accuracy while allowing you to select the pins.

Further, typically the only reason why you wouldn't want to use the timer 1 PWM pins is because you have a conflict with timer 1, not because you can't just move pins around.  So, the real issue isn't that you use other pins, it's that you need it to use a different timer.  The problem with doing that is that you'll get just as many people complain about using a different timer as it conflicts with a different set of libraries.  Timer 0 is basically off-limits because that would mess with the ATmega's internal timers (millis, micros).  Timer 2 would be possible, but more complicated as not all ATmega's have a timer 2 (Leonardo, Teensy 2.0) and they would use timer 4 instead.

So, switching over to use timer 2&4 instead of timer 1 would solve a problem for 10%, but create a problem for 10%, in other words a wash.  Adding both timer 1 and timer 2/4 support would make the library more than twice the size, which would cause problems for some as it's small enough to be used on microcontrollers like the ATmega8 while the tone library won't fit.

In other words, we wouldn't really be solving anything by adding timer 2/4 support.  We would for some, but we'd be creating problems for others.

I would think the only real option would be to rework toneAC and create a new library, say toneAC2 for example.  It would simply be a timer 2/4 version of toneAC that would be fixed to the timer 2/4 PWM pins.

Now that I say this, and think about it for a minute, maybe it's not possible.  Timer 2 doesn't have a Phase and Frequency Corrected PWM mode, so I don't think you can do it exactly like toneAC.  It would need to be a slower and less accurate way.

It's not really that big of a project to do, but I don't have the need for it, therefore not the desire either.  If someone is versed in timer interrupts, it would be really easy to take my toneAC and NewTone libraries and make a toneAC2 library that would use timer 2/4 as well as allow you to specify the pins.  If I get bored one night maybe I'll do it (probably a 2 hour job).  I guess as a bonus I wouldn't need to create a new development page for it.  I could just piggy-back it on the toneAC site as an alternative version that uses timer 2/4 and has flexible pin assignments.  Ah crap, maybe I will just do it.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

countofangels


Ah crap, maybe I will just do it.

Tim


Ah my name is Tim as well.  :P

So from one Tim to another: Thanks for the detailed information involved as to the concerns with the timers. It's helpful for me to learn more about this as a beginner to the Arduino world.

At the moment, I'm mostly trying to figure out how to use your library without conflicting with the SD library's use of the 10 pin as a selector (per: http://arduino.cc/en/Reference/SDCardNotes)  Based on what I've read there, though it looks like I may be able to just use SD.begin() to select a different pin for card initiation, and as long as 10 remains an Output that shouldn't actually cause problems.

So actually, with a little reading, I think the library will work for my particular application as it is.


Though I still think it would be awesome to have versions of the library for each of the available timers, just for that extra flexibility. From the pretty robust response you've received on the library in only a couple weeks, I think it's clear that people are finding it useful.

Anyway! Thanks for your efforts.

teckel



Ah crap, maybe I will just do it.

Tim


Ah my name is Tim as well.  :P

So from one Tim to another: Thanks for the detailed information involved as to the concerns with the timers. It's helpful for me to learn more about this as a beginner to the Arduino world.

At the moment, I'm mostly trying to figure out how to use your library without conflicting with the SD library's use of the 10 pin as a selector (per: http://arduino.cc/en/Reference/SDCardNotes)  Based on what I've read there, though it looks like I may be able to just use SD.begin() to select a different pin for card initiation, and as long as 10 remains an Output that shouldn't actually cause problems.

So actually, with a little reading, I think the library will work for my particular application as it is.


Though I still think it would be awesome to have versions of the library for each of the available timers, just for that extra flexibility. From the pretty robust response you've received on the library in only a couple weeks, I think it's clear that people are finding it useful.

Anyway! Thanks for your efforts.


If the conflict is with the SD library, it's easy to get around it as it doesn't need to use pin 10.  toneAC sets pins 9 and 10 (on the Uno) to output the first time you run it.  It also never changes them back to input either.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

teckel


Though I still think it would be awesome to have versions of the library for each of the available timers, just for that extra flexibility. From the pretty robust response you've received on the library in only a couple weeks, I think it's clear that people are finding it useful.


I had some extra time so I spent an hour and built the toneAC2 library that uses timer 2 and allows you to use any two pins.  It doesn't have the level of accuracy as toneAC nor the range.  Also, we lose the volume control because timer 2 doesn't have all the features of timer 1.  You're basically trading accuracy, range, and volume for flexible pin assignments.

What's kind of funny is that I wrote toneAC2 at work and I didn't have any Arduino's with me to test.  So, I couldn't test it at all, didn't even compile it as I was just using note++ to edit the library.  When I got home it had one little bug and then it compiled (needed to declare a variable outside a "for" loop).  It compiled the second time and worked perfectly.  I guess I've been doing low level port registers and timers too long.

I need to add Teensy and Leonardo support (ATmega32U4 needs to use timer 4 instead of timer 2) and test before I release it.  But, that's simple so it should be uploaded this weekend.  I'll post when it's available.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Go Up