Pages: [1]   Go Down
Author Topic: 9 DAYS TO DEADLINE - Tone.h + PWM = not working  (Read 766 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

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

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

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 310
Posts: 40382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 08, 2011, 03:58:18 pm by Big Oil » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Board?
Logged

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

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

Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 310
Posts: 40382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: