Author Topic: SOLVED - Having Trouble Getting Tone() To Work  (Read 4954 times)

granzeier

  • Jr. Member
  • **
  • Posts: 73
SOLVED - Having Trouble Getting Tone() To Work
« on: February 02, 2018, 12:27:33 pm »
I'm having troubles with the tone() command.

My circuit is just connecting a speaker to an I/O pin, and then through a resistor to ground:


When I upload the sketch (I hate that term, when I upload the program), I get no sound:
Code: [Select]
//A sketch to demonstrate the tone() function

//Specify digital pin on the Arduino to which the positive lead of the speaker is attached.
 int SpkrPin = 0;

void setup() {
    pinMode(SpkrPin, OUTPUT);
} //close setup

void loop() {

  tone(SpkrPin, 2000);

//  digitalWrite(SpkrPin, HIGH);
//  delay(2);
//  digitalWrite(SpkrPin, LOW);
//  delay(2);

}

I have tried with and without the pinMode() statement, and have also added a digitalWrite() statement to initially set the pin high (and low) - I saw someone somewhere say that you needed that.

When I uncomment the digitalWrite() statements to toggle the I/O pin, and comment out the tone() statement, I get a tone. Changing the delay() statements works as expected.  The scope (connected to the green line on the schematic) shows:


In addition, I have tried all 6 of the I/O pins, none of them give a tone (all show 0V on my scope, except for P5, which shows constant high.)

When I switch to an Arduino Uno, I get the tone from the speaker, using either the digitalWrite, or the tone() statements.

Does anyone have any ideas where I am going wrong?
« Last Edit: March 03, 2018, 06:25:12 pm by granzeier »

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Having Trouble Getting Tone() To Work
« Reply #1 on: February 02, 2018, 11:33:12 pm »
Have a look at this thread... there is mention that there is a conflict with the PWM stuff and tone()... maybe worth investigating further? Not sure if that was updated or fixed.


granzeier

  • Jr. Member
  • **
  • Posts: 73
Re: Having Trouble Getting Tone() To Work
« Reply #2 on: February 03, 2018, 03:36:46 am »
PeterF,

Thanks, I did see that thread while researching my issue, in fact that made me try two different things. First, I updated my libraries (I thought that in the past two-and-a-half years, it may have been fixed.) Secondly it made me try all of the I/O pins on the DigiSpark. Neither of these made any changes.

Looks like it is still elusive. I'm pretty sure that I am just missing something simple - like forgetting to use pinMode() when setting an I/O pin. Can anyone try this exact code on their DigiSpark to verify that it gives the same results?

benalb

  • Newbie
  • *
  • Posts: 9
Re: Having Trouble Getting Tone() To Work
« Reply #3 on: February 04, 2018, 08:34:45 am »
Can anyone try this exact code on their DigiSpark to verify that it gives the same results?

I have tried, and got the same results, no sound with tone function, buzz with digitalwrite.
No idea what the problem is, sorry.

granzeier

  • Jr. Member
  • **
  • Posts: 73
Re: Having Trouble Getting Tone() To Work
« Reply #4 on: February 07, 2018, 01:54:47 pm »
Thanks, at least I know it isn't just me (or mine.)  :(

Perhaps DigiStump could look into this, and see if he can find the trouble.

Does anyone have any idea where I could start (perhaps point me to where the tone function would be hiding.)  I did find a tone.cpp in my /opt/arduino-1.6.12/hardware/arduino/avr/cores/arduino directory (Linux Mint,) and in that file, there are numerous references to timer 2, which does not exist in the Tiny85.  Unfortunately, my C is not good enough to tell if that is what is blocking the tone statement.  Any ideas?

granzeier

  • Jr. Member
  • **
  • Posts: 73
Re: Having Trouble Getting Tone() To Work
« Reply #5 on: February 22, 2018, 03:20:30 am »
Hey DigiStump,

Any thoughts here?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Having Trouble Getting Tone() To Work
« Reply #6 on: February 24, 2018, 08:45:21 pm »
I don't fancy your chances of getting a reply from Erik, he's busy on other projects atm ;)

The problem you're having is because on the Digispark you need to have a delay for the tone to be heard, and then a notone() to stop the tone. It can't run 'in the background' like it does on the Uno, etc. Mainly due to it not having been implemented, although having said that... they don't seem to have implemented it on the Trinket either, which is using the same ATTiny85 MCU. If you really, really had to do it via timer interrupts, then this would be the code to pull apart, as this chap is doing exactly that - http://w8bh.net/avr/TrinketTone.pdf

If you're after the source files, you'll be after something like ~/.arduino/15/packages/hardware/digispark/cores/tiny/Tone.cpp (depending on exactly where you put the digistump board support package files... you're after that 'hardware' folder, which may be in your sketchbook, and the rest of the path should be right.). Here is the file on  Github.

btw, this sketch gives a reasonable simulation of a heartbeat monitor if used with a piezo speaker, if a bit metronomic.  ;)

Code: [Select]
//Specify digital pin on the Arduino to which the positive lead of the speaker is attached.
int SpkrPin = 0;

void setup() {
  pinMode(SpkrPin, OUTPUT);
} //end setup()

void loop() {
  tone(SpkrPin, 2000);
  delay(200);
  noTone(SpkrPin);

  delay(2000);
} //end loop()



granzeier

  • Jr. Member
  • **
  • Posts: 73
Re: Having Trouble Getting Tone() To Work
« Reply #7 on: March 03, 2018, 04:54:22 pm »
...The problem you're having is because on the Digispark you need to have a delay for the tone to be heard, and then a notone() to stop the tone. It can't run 'in the background' like it does on the Uno, etc. ...
Peter,
Thank you very much, that did exactly what I needed.  The only difference being that the delay adds a time limit to the tone - but I was going to add that later; I was just playing with the tone to make sure I knew how it worked.  Also, that time limit, being an unsigned long, is not all that limiting - something just under 50 days!  :P
So, thanks again for this solution.