chipKIT® Development Platform

Inspired by Arduino™

[Beginner] Tone function not working

Created Fri, 02 Sep 2011 21:24:51 +0000 by jromang


jromang

Fri, 02 Sep 2011 21:24:51 +0000

Hello, I played with the tone tutorial (http://arduino.cc/en/Tutorial/Tone) on my arduino mega 2560 board and it works perfectly. I tried to run the same progam on my chiptkit max32 and it does not play any sound. Is there something to change to the tutorial's code or do I misss something ? Thanks


Darth Maker

Fri, 02 Sep 2011 23:24:13 +0000

Just one of the many ways which their 100% compatibility claim is an outright lie. It's a decent board, but it's more like 65%-75% percent compatible as far as core functions, and 10%-35% compatible with libraries.

Tone doesn't work.


jromang

Sat, 03 Sep 2011 07:25:05 +0000

But the 'tone' function should already be supported according the supported function list on the wiki ? Is there another way to output sound ?


Darth Maker

Sat, 03 Sep 2011 20:14:57 +0000

You can do it manually. I made this code that I found commented out in the Tone.cpp file into a function. It's a blocking function though, which makes it's usefulness extremely limited.

void ToneWorkaround(byte _pin, int frequency, int duration)
{
    frequency	=	1000000 / frequency; 
    int startTime	=	millis();
    while ((millis() - startTime) < duration)
    {
        digitalWrite(_pin, HIGH);
        delayMicroseconds(frequency);
        digitalWrite(_pin, LOW);
        delayMicroseconds(frequency);
    }
}

jromang

Sat, 03 Sep 2011 21:09:58 +0000

Thanks, I'll try it, it should be fine for my needs ;) I also found the problem is listed here : https://github.com/chipKIT32/chipKIT32-MAX/issues/72 ; maybe it will be solved for the next release ?


GeneApperson

Mon, 12 Sep 2011 19:07:00 +0000

Somehow, the corrected code for the Tone function didn't get into the last release. It is in the 20110907 test release. I downloaded and tested it about an hour ago and it works.

Gene Apperson Digilent


procyon

Wed, 21 Sep 2011 21:34:29 +0000

I tried the 20110907 test release and it indeed works, but it can't handle 0 frequency tone. For example the "toneMelody" example contains:

int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

Currently it plays the first 5 notes and stops. I'm using Uno32 if that matters.


KM6VV

Thu, 22 Sep 2011 19:48:42 +0000

"0 freq tone" would be a rest?

Alan KM6VV


procyon

Fri, 23 Sep 2011 08:11:37 +0000

Well the sample melody sounds nice if there is a pause at that time, so I assume that's how its meant to be. I put 1 instead of 0 as a workaround and it worked that way.


GeneApperson

Fri, 23 Sep 2011 19:54:25 +0000

I looked into this and found a couple of bugs related to this. Arguably, a frequency of 0 is DC and the function should be setting the pin to 0 (or 1), but I think 0 is more appropriate).

One bug is that the code will end up doing a divide by 0 when calculating the value to load into the timer period register when the value 0 is passed in for the frequency.

I think that there is also a bug in doing the note duration when the frequency is 0.

I've compare the chipKIT code to the Arduino code, and it looks to me like their code should have the same problems.

I'll have to dig deeper.

I'll log this as an issue on github and look into fixing it for a future release.

Thanks, Gene Apperson Digilent


drBart

Tue, 15 Nov 2011 09:06:49 +0000

For Arduino, the following lines

//TCCR2A = _BV(WGM21) | _BV(COM2A0); // This mode toggles output once per timer cycle
  //TCCR2B = _BV(CS20);  // Do not scale the clock down - use 16 MHz timer rate.
  //OCR2A = 210; // Divide sys. clock by 210, 1/2 cycle = 76 khz, 1 cycle = 38 khz

are equivalent to tone(11, 38000);

Is there something like that that I can do for chipkit? Off hand, I did not write the above code.


KM6VV

Tue, 15 Nov 2011 19:33:56 +0000

Can't just use

tone(pin, frequency) tone(pin, frequency, duration)

Alan KM6VV


drBart

Mon, 28 Nov 2011 05:58:26 +0000

It doesn't seem to do anything.

I use tone(11, 38000); and noTone(11);

I'll try setting something up using the ocomp.


drBart

Mon, 28 Nov 2011 14:49:35 +0000

Here's what I did. 80Mhz / 2105 ~= 38KHz, and I used that for the timer reset. 1052 ~= 2105/2, so that set the 50% duty cycle.

//set up timer2 on pic32
  CloseTimer2();
  OpenTimer2(T2_ON | T2_PS_1_1, 2105);

  //Set up ocomp1 on pic32
  CloseOC1();
  OpenOC1(OC_OFF | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, \
    1052, 1052);

OC1CONCLR = OC_ON; //disable ocomp1

OC1CONSET = OC_ON; // enable ocomp1


Bypeduredads

Tue, 06 Dec 2011 08:09:56 +0000

Hi there,

Im using a Mac mini Core Solo with the latest online updates of OS X Kernel 10.4.7.

For some days now the function keys F1 to F12 of my keyboard are not working anymore.

Any ideas, how to troubleshoot that problem?

Roger.


fastlater_pro

Sun, 19 Feb 2012 18:36:29 +0000

So, right now, We can use the tone library in chipkit max 32 or not ? There are a tone library for max 32?


HacknMod

Mon, 27 Feb 2012 00:23:14 +0000

The following code works just fine on the Max32:

/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 
#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

void setup() {

}

void loop() 
{
  tone(3, NOTE_C4);
  delay(1000);
  noTone(3);
  delay(500);
  tone(3, NOTE_A3);
  delay(1000);
  noTone(3);
  delay(500);
}

fastlater_pro

Mon, 27 Feb 2012 09:40:18 +0000

I need to set something or just connect the speaker in pin 8