0

It's 32-bit ARM core processor is SAM3X8E ARM Cortex-M3 CPU.

Error:

sketch_nov16b\sketch_nov16b.ino: In function 'void setup()': sketch_nov16b:49:43: error: 'tone' was not declared in this scope tone(8, melody[thisNote], noteDuration); 
^ sketch_nov16b:61:13: error: 'noTone' was not declared in this scope noTone(8); 
^ exit status 1 'tone' was not declared in this scope 

Code:

/*

  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.

  https://www.arduino.cc/en/Tutorial/Tone

*/

#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() {

  // 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(8, 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(8);

  }
}

void loop() {

  // no need to repeat the melody.
}
4
  • What core are you using? Nov 16, 2023 at 17:14
  • It's 32-bit ARM core processor is SAM3X8E ARM Cortex-M3 CPU
    – JohnB
    Nov 16, 2023 at 17:52
  • Does this meet jsotola's request?sketch_nov16b\sketch_nov16b.ino: In function 'void setup()': sketch_nov16b:49:43: error: 'tone' was not declared in this scope tone(8, melody[thisNote], noteDuration); ^ sketch_nov16b:61:13: error: 'noTone' was not declared in this scope noTone(8); ^ exit status 1 'tone' was not declared in this scope
    – JohnB
    Nov 16, 2023 at 18:07
  • @JohnB I said do not paste into a comment and you pasted into a comment ... this site is not a forum ... it is a question and answer site ... all info must be contained in the question
    – jsotola
    Nov 16, 2023 at 20:52

1 Answer 1

3

In short, they just never implemented tone() in the Due/SAM core. So you're not going to get the normal tone() function working.

  • The tone.h header is empty.
    They probably should done any number of things that would produced a somewhat clearer warning about this.

  • The counterpart .cpp file has been renamed to .cpp.disabled so that the no attempt is made to compile it and is full of AVR-based code that they'd copied into the project for reference with the intention of replacing it but never did.

There a github issue tracking this. But it hasn't been updated since 2020. That issue references the Arduino forum for apparently working alternative code for producing tones. So maybe try their code.

I don't see any popular alternative packaged as an Arduino library for producing simple tones. However, the chip has a DAC and there is an Audio Library. It's a bit overkill for producing basic tones, but you could modify the example there to fill the buffer with a tone's waveform rather than loading it from an sdcard file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.