Pages: [1]   Go Down
Author Topic: Tone & TLC5940  (Read 894 times)
0 Members and 1 Guest are viewing this topic.
Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
Writing a game that uses a Mega, 2 TLC5940 chips, 8 RGB LEDs and a speaker.
The TLC5940 chips use pins 9,11,12 and the speaker is on pin 8.
If I use the code below as is, I get tones, but no LEDs lighting up.
If i comment out the line playToneWin(); I get LEDs working, but no tones.
I'm assuming from this that somehow the tones interfere with either pin 9,11 or 12. I'm not sure how to fix this
Any suggestions on how to correct this so I can both play tones and light up the LEDs?

Thanks.

Code:

/**************************************************************************
 * Tone Test
 **************************************************************************/

#include <avr/pgmspace.h>
#include "pitches.h"
#include "Tlc5940.h"
int channel=2;

int melodyCard0[] = {
  NOTE_B2, NOTE_B3, NOTE_B4};
int melodyCard1[] = {
  NOTE_C4};
int melodyCard2[] = {
  NOTE_B5};
int melodyCard3[] = {
  NOTE_A3};
int melodyWin[] = {
  NOTE_C2, NOTE_D3, NOTE_E4,NOTE_C2, NOTE_D3, NOTE_E4,NOTE_C2, NOTE_D3, NOTE_E4,NOTE_E5};
int melodyLose[] = {
  NOTE_B4, NOTE_B2};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurationsCard1[] = {
  2};
int noteDurationsCard0[] = {
  8,8,4};
int noteDurationsCardWin[] = {
  4,4,2,4,4,2,4,4,4,2};
int noteDurationsCardLose[] = {
  4,2};

void setup(void) {
  Serial.begin(9600); 
  Tlc.init();
  Tlc.clear();
   playToneWin();
  setLED();
}

void loop(void) {

}


void setLED()
{
  Serial.println(F("Setup LED"));
  delay(100);
  channel=2;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=5;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=8;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=11;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=19;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=22;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=24;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(300);
  channel=27;
  Tlc.set(channel, 3095);
  Tlc.update();
  delay(900);
  Tlc.clear();
  Tlc.update();
}

void playToneWin()
{
  for (int thisNote =0; thisNote<10; thisNote++)
  {
    int noteDurations = 1000/noteDurationsCardWin[thisNote];
    tone(8, melodyWin[thisNote],noteDurations);
    int pauseBetweenNotes = noteDurations * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }

}

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 508
Posts: 31369
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Tone uses the same timer as the PWM relies on.
So you have to replace one function with an alternative.
Like a software PWM
http://www.elcojacobs.com/shiftpwm/
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Grumpy_Mike. 
I think I have some shift registers lying around.
I will give that a go.
Logged

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


A small modification to the core will move tone to a different timer.  The relevant section is here...

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Tone.cpp#L88

Change the appropriate tone_pin_to_timer_PGM.
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure I want to switch to shift registers.
If I was able to change the core, what would I change here?
Code:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

#define AVAILABLE_TONE_PINS 1
#define USE_TIMER2

const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };

#elif defined(__AVR_ATmega8__)

#define AVAILABLE_TONE_PINS 1
#define USE_TIMER2

const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };

#elif defined(__AVR_ATmega32U4__)
 
#define AVAILABLE_TONE_PINS 1
#define USE_TIMER3
 
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
 
#else

#define AVAILABLE_TONE_PINS 1
#define USE_TIMER2

// Leave timer 0 to last.
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };

#endif

As another alternative, could I change the TLC5940 library to use a different timer?
http://code.google.com/p/tlc5940arduino/
Logged

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


The code is divided by processor.  The first section...

Code:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
...
#elif ...

...is for the ATmega1280 and ATmega2560 processors.  Find the section for your processor.  If your processor is not listed, the last section is used...

Code:
#else
...
#endif


Within each section is a definition for tone_pin_to_timer_PGM like this one...

const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };

Change the number to the timer you want used for tone.  Using the line above as an example, the following moves tone from timer 3 to timer 2...

const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, but for some reason I can't get this to work.
I am using a Mega 2560 editing the line below in a text editor.
Code:
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
If it is set to anything but 2, I get no tones. I have tried them all.
What am I missing?
Logged

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


I missed a bit.  Sorry about that.  You will also have to change...

Code:
#define USE_TIMER2

...found in the same section...

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Tone.cpp#L89


These changes should get tone working on timer 1...

#define USE_TIMER1
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 1 /*, 1 */ };
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No problem, I'll give that a try.
Thanks for your help.
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 574
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That did it, works now.
Thanks again.
Logged

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

I wonder if anyone get this working with UNO? I tried changed the code accordingly but it doesn't seem to work for me....
Logged

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


The TLC5940 driver uses two timers.  millis / micros use a third.  The Uno has three timers.  There are none left for tone.
Logged

Pages: [1]   Go Up
Jump to: