Go Down

Topic: 9 DAYS TO DEADLINE - Tone.h + PWM = not working (Read 1 time) previous topic - next topic

TonyStark

I have 9 days before this project has to work, so any help would be greatly appreciated.

I'm pretty sure I know what the problem is, but if there is any way around it, I'd love to know.

I have 2 piezo speakers attached to my Duemilanove, one to pin 12, one to pin 5.
I have 3 transistors controlling external power to LEDs on pins 3, 9 and 10. (all of which are PWM)
I've included the Tone.h library for the speakers, and produce two separate tones simultaneously with them.  This works fine.

The LEDs work fine if I use digitalWrite, but don't light at all when I use analogWrite.  I know this is due to some interference between the Tone library and the PWM pins, but I would prefer the LEDs to fade on and off, instead of just turn on and off.

If there's any way around this, please let me know.

Thank you,

RTS

AlphaBeta

http://www.arduino.cc/en/Reference/Tone
Quote
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

TonyStark

Yeah, I read that, but why does it interfere with the PWM output on 9 and 10?

Big Oil

#3
Mar 08, 2011, 09:10 pm Last Edit: Mar 08, 2011, 09:58 pm by Big Oil Reason: 1
My Christmas tone library project used 1 speaker pin and two pwm pins for LED fading with no problem.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291875201/0#0

LEDs on pins 9 and 10
speaker on pin 11

edit: It used a duemilanove


TonyStark

Quote
My Christmas tone library project used 1 speaker pin and two pwm pins for LED fading with no problem.


Thanks Big Oil, I tried that, but still no success.  Is it because I'm running two speakers with two separate tones that's causing the problem then? 

I'm actually using this Tone library, not the one included with the Arduino: http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation#Projects/Examples_Using_Tone_Library_and_Core_tone()

I have a feeling multiple tones are using the CTC timers on the Duemilanove, though I thought there were three... 2, 1, and 0.  And, to my knowledge, 0 is the one responsible for PWM output, so I didn't expect there to be any conflict.

I wish I could just go with one tone, but unfortunately I require two, but that would probably allow me to regain the fading of the LEDs.

Thanks for the help though


Coding Badly

Duemilanove ... sorry I missed that in your original post ... has three timers.  Each timer is able to provide PWM on two pins.  With the Tone Library, one timer is needed to output on one pin.  If the Tone Library has taken control of a timer, that timer is no longer able to provide PWM.  To have three PWM pins and two tone pins you need four timers.  To make the situation more complicated, the Arduino clock (millis, micros, delay, delayMicroseconds) is driven by timer 0.

Depending on what else your Sketch is doing, you may able to perform the PWM in software.  Something like this may help...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1268806013

Big Oil

You could light each LED at different times with one PWM.  You would need 3 transistors (and 3 digital pins).  Have the one PWM go to all three positives on the LEDs.  Then each LED ground goes through a separate transistor, then to ground.  Then activate whichever transistor you want with a separate digital pin.

Go Up