Author Topic: tone() support on Oak?  (Read 4854 times)

crambo

  • Jr. Member
  • **
  • Posts: 52
tone() support on Oak?
« on: December 16, 2016, 07:13:37 pm »
I have a sketch that works on a traditional arduino, but doesn't seem to work on the oak. I see that it is supported on the Arduino Zero, which I thought was an ESP82666 derivative as well...

I can't determine conclusively that tone() WON'T work on the Oak.

Has anyone else tried?

crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #1 on: December 16, 2016, 07:34:29 pm »
More info....

Ok: So tone() does seem to work, but it only gets part way through this example:
Code: [Select]
#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {

}

void loop() {
  // no need to repeat the melody.
    // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(3, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(3);
  }
}

It only plays first 5 tones. After "0" pause, it freezes and Oak becomes locked up and must be P1 ->GND rebooted.

crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #2 on: December 16, 2016, 07:41:07 pm »
Yup. The "zero" pause in the notes array is the issue.  If you remove it, it will play in loop. If you leave it, even define it as a const, it will crash.

:) Smarter people than me will understand what is happening!


PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: tone() support on Oak?
« Reply #3 on: December 16, 2016, 09:38:11 pm »
Well, that was exciting... did I mention I absolutely HATE pizeo buzzers... nasty chirpy things!

It appears there is a glitch in the tone library for the ESP8266... where they haven't taken into consideration what happens if you specify a 'zero' tone or didn't expect it to crash. If you replace the

Code: [Select]
      tone(3, melody[thisNote], noteDuration);
line from the example with the below, it will guard against that situation, and do a noTone() with corresponding delay.

Code: [Select]
    if (melody[thisNote] == 0)
    {
      noTone(3);
      delay(noteDuration);
    }
    else
    {
      tone(3, melody[thisNote], noteDuration);
    }

I will (probably) get around to testing it on the ESP8266 core to make sure it's an issue on their end and post something over there about it.

crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #4 on: December 19, 2016, 12:59:01 pm »
Hah! Yes! I couldn't agree more! Imagine a classroom of students all making various hideous sounds on chirpy little things! I only need them in this particular little project, as teachers need an audible for the last five minutes of class... Mine is "The Imperial March". "Ok kids, five minutes until Vader arrives!"

Thanks for looking into this issue as well! For the time being, this is a good work around. Now to figure out how to appropriately amplify. 3.3v is not enough. Will prob use the 5v supply off of a transistor.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: tone() support on Oak?
« Reply #5 on: December 19, 2016, 07:15:20 pm »
Oh no... not a whole classroom of them!  :'(

lol... nice idea though... Imperial March is always a good one... reminds me of the The Floppotron (video of it on YouTube). Yeah, transistor or a mosfet should do the trick. btw, I found the root of the issue, and it is from the ESP8266 core, so I've added a comment to an already open issue there and proposed a fix to the library. Hopefully they'll like it and commit it at some point. Thanks for spotting it!  ;D


crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #6 on: December 20, 2016, 09:37:11 am »
Peter...as always, thanks for all your support!

crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #7 on: December 20, 2016, 10:14:00 am »
A possible final question....

Why is it I can only use pin3 for this ... I have tried a bunch of other spots on the board, using the mapping according to the Wiki pinout... p5 ---> gpio 4 (reserving P0124 ...and such), but I can only get it work on P3 (the only pin with a natural 3-3 mapping...

Am I missing a document that would spell this out better?

crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #8 on: December 20, 2016, 10:16:12 am »
This tutorial: https://digistump.com/wiki/_media/oak/tutorials/buzzer.png

Shows using pin2, but I would have thought that was a no-no, as you'd be holding pin2 to gnd at boot!

Arrgghh!

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: tone() support on Oak?
« Reply #9 on: December 21, 2016, 01:54:04 am »
btw, thanks for the wiki contrib... more the merrier!  :)

I think the writer of that sketch didn't think of that when they wrote it, or they had a buzzer that didn't do that (power the Oak up with the buzzer connected). I did have that exact problem when I tried it, so will need to do something about that. I can't tell you why P3 appears to be the only one that works, but I can replicate your results. And you shouldn't need to use the ESP8266 GPIO mapping as the esp8266_pinToGpio array is used in the in the tone library to remap Oak pins to esp8266 GPIOs.

Ok...  ??? The esp8266_pinToGpio that Erik added when importing the Tone library is itself the issue? That ... ok.. I got no words for that. If you're willing to go find the Tone.cpp file in your Oak core, and replace it with the attached one, I have patched the issue that caused tone(tone_pin, 0) crash, and also commented out the esp8266_pinToGpio usage which now appears to allow any digital pin (so P0 to P10) to drive the pizeo!  ;D Silly Erik!  :o  :P

(on my Windows 10 box, this is where to find and replace the Tone.cpp file: %appdata%\..\Local\Arduino15\packages\digistump\hardware\oak\1.0.6\cores\oak)


crambo

  • Jr. Member
  • **
  • Posts: 52
Re: tone() support on Oak?
« Reply #10 on: December 21, 2016, 09:42:54 am »
Peter! Thanks for this! Huge help!