Using tone function on my Due

Hi All,

This problem currently has me stumped!

On my Uno, it works fine but on my Uni supplied Due, I get a compile error “exit status 1, ‘tone’ was not declared in this scope”. This Due uses the ARM 32bit processor.

Any thoughts pls?

The code (which, as I said, works fine on my Uno is:


#include <Arduino.h>

const int buzzer = 6; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}

void loop(){
  ToneOnOff();
  ToneTune();
  TonePWM();
}

void ToneOnOff() {
tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec
  tone(buzzer, 1000);
  delay(1000);
  noTone(buzzer);
  delay(1000);
}

void ToneTune() {
  int notei[] = {262, 294, 330, 349, 392, 440, 494, 523};
  int durationi[] = {500, 500, 500, 500, 500, 500, 500, 500};

  for (int i = 0; i < 8; i++) {
    int note = notei[i];
    int duration = durationi[i];
    tone(buzzer, note, duration);
    delay(duration);
  }
  noTone(buzzer);
  delay(1000);
}

void TonePWM() {
  for(int i; i <= 255; i++) {
  tone(buzzer, 500 + i);
  delay(100);
  }
  delay(1000);
}

Hey Gerard,

It might be worth having a specification at the beginning so that the ToneAC function is definitely included as it may need specification with the DUe, Its also worth noting that you will need to replace tone() functions with specific ToneAC Functions. Give the below code a go and see if it works for you:

> #include <Arduino.h>
> #include <ToneAC.h>
> 
> const int buzzer = 6; //buzzer to arduino pin 9
> 
> void setup(){
>   pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
> }
> 
> void loop(){
>   ToneOnOff();
>   ToneTune();
>   TonePWM();
> }
> 
> void ToneOnOff() {
>   toneAC(buzzer, 1000); // Send 1KHz sound signal...
>   delay(1000);          // ...for 1 sec
>   noToneAC(buzzer);     // Stop sound...
>   delay(1000);          // ...for 1sec
>   toneAC(buzzer, 1000);
>   delay(1000);
>   noToneAC(buzzer);
>   delay(1000);
> }
> 
> void ToneTune() {
>   int notei[] = {262, 294, 330, 349, 392, 440, 494, 523};
>   int durationi[] = {500, 500, 500, 500, 500, 500, 500, 500};
> 
>   for (int i = 0; i < 8; i++) {
>     int note = notei[i];
>     int duration = durationi[i];
>     toneAC(buzzer, note, duration);
>     delay(duration);
>   }
>   noToneAC(buzzer);
>   delay(1000);
> }
> 
> void TonePWM() {
>   for(int i; i <= 255; i++) {
>     toneAC(buzzer, 500 + i);
>     delay(100);
>   }
>   delay(1000);
> }

Cheers,
Blayden

1 Like

Thanks Blayden,

Apparently ‘tone’ doesn’t work at all with Due. Which explains why it works fine on Uno but not on Due!

Cheers and thanks,

Gerard