Pages: [1]   Go Down
Author Topic: ***4 days to deadline*** (PWM & tone()function interference problem???)  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,

I'm working on a rather simple project for school and it's due in 4 days. Your help would be greatly appreaciated.

So basically I'm controlling two LED's via PWM lines from 2 Potentiometer inputs. The output of the potentiometers are map() from 0 - 1023 to 0-255.

Then, I have a potentiometer controlling an 8 ohm speaker via the tone() function. The potentiometer just cycles through different notes on a simple musical scale depending on the position of the pot.

The problem is that after turning the pot for the speaker fully right then fully left a few times the speaker emits a high pitched sound and the musical scale is not controllobale by the pot until arduino is reset(by pressing the button on the board.)
I think it is due to PWM interference from the LED's because when I remove the code for PWMed Led's, the time until the problem occurs is greatly extended(up to around 60 fully left and fully right turned on the potentiometer).






Here is my code(there is also a seperate pitches.h page that just lists the frequencies for each note e.g. NOTE_C3 etc.):
Code:
/*
 2 PWM controlled LED's
 1 PWM controlled 8 Ohm speaker(stereo)
 
 LED's controlled by 2 potentiometers
 Speaker controlled by 1 potentiometer
 
 Variables for pin numbers, brightness, and values
 */
 
 
 #include "pitches.h"
 //just different notes on a scale
int melody[] = {0,NOTE_C3, NOTE_CS3,NOTE_D3 ,NOTE_DS3,NOTE_E3 ,NOTE_F3, NOTE_FS3,NOTE_G3, NOTE_GS3,NOTE_A3 ,NOTE_AS3,NOTE_B3, NOTE_C4 };

int led1 = 5;
int led2 = 6;

int brightnessled1 = 0;
int brightnessled2 = 0;

int potval1 = 0;
int potval2 = 0;
int potvalforspeaker;

void setup() {
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
}

void loop() {
  //for speakers
  potvalforspeaker = analogRead(5);
  int thisPitch = map(potvalforspeaker, 0, 1023, 0, 13);
  tone(8, melody[thisPitch], 15);

 //for LED's
  potval1 = analogRead(1);
  potval2 = analogRead(2);
  brightnessled1 = map(potval1, 0, 1023, 0, 255);
  brightnessled2 = map(potval2, 0, 1023, 0, 255);
  analogWrite(led1, brightnessled1);
  analogWrite(led2, brightnessled2);
  
}

« Last Edit: March 31, 2011, 10:16:07 am by Numex106 » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a thought, have you checked to see that the PWM pins you are using and your pitch.h/tone code are using conflicting hardware timers?

Lefty

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a thought, have you checked to see that the PWM pins you are using and your pitch.h/tone code are using conflicting hardware timers?

No I haven't, where I can I find which pwm pins are for which timers??

Thanks
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a thought, have you checked to see that the PWM pins you are using and your pitch.h/tone code are using conflicting hardware timers?

No I haven't, where I can I find which pwm pins are for which timers??

Thanks

http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

You will have to check the tone code to see which timers it uses.

Lefty
Logged

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

Quote
controlling an 8 ohm speaker

Connected directly to an Arduino pin?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I researched the "pitches.h" file and it doesn't specify any timers.  In fact, all "pitches.h" contains is a list of defined constants that are linked to note names.  So the "pitches.h" doesn't deal with the use of timers directly.  That is still handles by the Tone() function.
====================================================
To make the pitches.h file, click on the "new Tab" button in the upper right hand corner of the window. It looks like this:

The paste in the following code:
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Lefty, you said "check the tone code to see which timers it uses" ... I would like to be able to do that but I don't have any idea just HOW I could do that.  Where/how do I find a listing of that code?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well it doesn't appear to be a timer conflict. From the Arduino reference for tone:

Quote
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).


http://arduino.cc/en/Reference/Tone


Lefty
Logged

Toronto, ON
Offline Offline
Full Member
***
Karma: 10
Posts: 233
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hiya!

Strange this hasn't been asked yet, but what board are you using?  It will be helpful in tracking things down.  As well, what version of the IDE are you using?

Next, if I understand correctly, you're saying that the code goes wonky when the pot controlling the tone() values is moved to its extents repeatedly?  And you say that regardless of whether the PWM (analogWrite()) code is in place, that the code goes wonky - just the length of time to failure is different?

If the board you are using is a Duemilanove or an Uno, then retrolefty is correct, that there will be no "interference" between tone() and analogWrite().

I've tested the code on a rEDI board, and the sketch works fine.

As I think CodingBadly has asked - are you connecting the speaker directly to a pin?  If so, this can be seriously dangerous to the controller.  If you do not put a resistor (and preferably a flyback diode - although not strictly necessary as long as you use a high enough series resistor) in series with the speaker, you will have large voltages being sent back into the controller potentially causing latchup - i.e. causing the controller to stop working.

More references:
http://www.wiring.org.co/learning/basics/tonemelody.html
http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Strange this hasn't been asked yet, but what board are you using?  It will be helpful in tracking things down.  As well, what version of the IDE are you using?

Duemilanove + latest IDE 0022

Quote
Next, if I understand correctly, you're saying that the code goes wonky when the pot controlling the tone() values is moved to its extents repeatedly?  And you say that regardless of whether the PWM (analogWrite()) code is in place, that the code goes wonky - just the length of time to failure is different?

Correct, when i remove the analogWrite() code for the LED's time until failure is increased from around 3 extents to 20.

Quote
are you connecting the speaker directly to a pin?

yes, I'm using a resistor as stated by the tone() example diagram on arduino.cc>learning>tone()


A slight update: If you look at my code you will see I have an array with the frequencies in order. the first thing(forgot what it's called) in the array is stated 0 frequency it seems that if I just this to 1 then it seems to be working except every second a pulse is generated on the speaker which is about as annoying as the high pitched squeel it emits when not working. Also, if I do not turn the pot fully left so it doesn't reach that 0Hz frequency it works. (so full right works, unless you turn it fully left)

Thanks for all the help so far smiley
Logged

Toronto, ON
Offline Offline
Full Member
***
Karma: 10
Posts: 233
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Oh right, I forgot we're using Arduino. smiley-wink

Try this in your code:

Code:
  int thisPitch = map(potvalforspeaker, 0, 1023, 0, 13);
  if (thisPitch > 0)
    tone(8, melody[thisPitch], 15);
  else
    noTone(8);


b
Logged


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

Quote
are you connecting the speaker directly to a pin?

yes, I'm using a resistor as stated by the tone() example diagram on arduino.cc>learning>tone()

http://arduino.cc/en/Tutorial/Tone
http://arduino.cc/en/Tutorial/Tone4

5 volts / 108 ohms = 0.0463 amps = 46.3 milliamps

From the datasheet...

Quote
28.1 Absolute Maximum Ratings*
...
DC Current per I/O Pin ............................................... 40.0 mA
...

*NOTICE: Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device. This is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. Exposure to absolute maximum rating conditions for extended periods may affect device reliability.

Quote
Correct, when i remove the analogWrite() code for the LED's time until failure is increased from around 3 extents to 20.

You are running WAY TOO MUCH CURRENT through the processor.  My suspicion is that the processor is overheating and then stops functioning correctly.  Even if I'm wrong, YOU NEED TO REDUCE THE CURRENT.

Logged

Toronto, ON
Offline Offline
Full Member
***
Karma: 10
Posts: 233
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh sheesh...  I didn't know you were using only 100 Ohms.  Definitely needs to be more like 1K Ohm.

This is what I specify in the Tone library.

http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation#Hardware_Connections/Requirements

The diagram I use on the wiki page is from Tom Igoe's examples, and, ugh, is it wrong, for so many reasons.  I'm going to change it now.


Further to what CodingBadly has written - When you have the tone() set @ 0 Hz (i.e. the pin is either ON or OFF solid) - the MAXIMUM current will flow through the pin if it is set HIGH, so it will definite exceed the specifications and likely cause a latchup.


Finally - the code change I posted MUST be used, because you shouldn't send a frequency of 0 to tone() in Arduino 0022.


b
« Last Edit: April 02, 2011, 12:50:38 pm by bhagman » Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, I'm using a resistor as stated by the tone() example diagram on arduino.cc>learning>tone()

http://arduino.cc/en/Tutorial/Tone
http://arduino.cc/en/Tutorial/Tone4

5 volts / 108 ohms = 0.0463 amps = 46.3 milliamps

Even if I'm wrong, YOU NEED TO REDUCE THE CURRENT.

So should the tutorial be changed?
Logged

Pages: [1]   Go Up
Jump to: