Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Strangeness with Tone

  1. #1
    Senior Member
    Join Date
    Jul 2014
    Posts
    138

    Strangeness with Tone

    Code running on Teensy 3.6:
    https://gist.github.com/macaba/33813...9bfc127f83d7a2

    It's so simple, I'm sure I'm missing something obvious...

    The sketch as-is won't output any tone (but the Serial output shows valid frequency numbers). If you uncomment line 17 and comment out line 16, it'll give a [fixed frequency] tone. Why doesn't the sketch as-is work?

    (I also tried AnalogWriteFrequency and get the same issue)

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    138
    Figured it out - passing a frequency of 0 creates some kind of unrecoverable fault. Coercing to 1 or higher fixes it.

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,153
    That’s quite logical behavior. Setting a frequency means using the bus clock divided by the frequency as a timer mod value proportional to the signal period. And division by 0 is not too amusing for a small microcontroller...

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    138
    Logical; for sure. Good behavior; certainly not.

    Once 0 has been set once, it seems any positive integers after this point don't work. This would be ok if documented but it's not in the GitHub source or the PJRC page.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,266
    I've put this on my list of bugs to investigate...

  6. #6
    Senior Member manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,265
    The avr version of Tone.cpp constrains frequency to within a MIN and MAX (dictated by hardware). teensy3 version of Tone.cpp has no check on frequency, and value of 0 will result in divide by zero. Probably limit minimum frequency to 1

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,266
    Yup, I've "fixed" this by just disallowing zero. Instead you get 1 Hz.

    https://github.com/PaulStoffregen/co...60f484a8f81996

    I briefly considered supporting DC. But that would make all the code much more complex, especially when tone() is used with the 3rd parameter for duration.

    Turns out Arduino Uno clamps the minimum to about 31 Hz, which makes me feel much better about not being able to go all the way down to DC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •