Project 6 - Tone Not Declared in This Scope

HI.
I am working on Project 6 - Light Theremin

Once I created the code, and try to upload I get the following error

"Tone Not Declared in This Scope" and won't let me upload the program. The exact code is below:

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  while (millis() < 5000) {
    sensorValue = analogRead(A0);
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    if (sensorValue < sensorLow){
      sensorLow = sensorValue;
    }
  }
  digitalWrite(ledPin, LOW);
}
void loop() { 
  sensorValue = analogRead(A0);
  int pitch = map(sensorValue,sensorLow,sensorHigh, 50, 4000);
  
  tone(8,pitch,20);
  delay(10);
}

The error information is as follows - Im using the online editor and using Arduino Due Native USB

/usr/local/bin/arduino-cli compile --fqbn arduino:sam:arduino_due_x --libraries /home/builder/opt/libraries/latest --build-cache-path /tmp --output-dir /tmp/069014772/build --build-path /tmp/arduino-build-F18E16C7BE23C2CA9C35509222C4B25D /tmp/069014772/sketch_may28a

Compiling sketch...

/home/builder/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++ -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -mcpu=cortex-m3 -mthumb -DF_CPU=84000000L -DARDUINO=10607 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Due\"" -I/home/builder/.arduino15/packages/arduino/hardware/sam/1.6.12/system/libsam -I/home/builder/.arduino15/packages/arduino/hardware/sam/1.6.12/system/CMSIS/CMSIS/Include/ -I/home/builder/.arduino15/packages/arduino/hardware/sam/1.6.12/system/CMSIS/Device/ATMEL/ -I/home/builder/.arduino15/packages/arduino/hardware/sam/1.6.12/cores/arduino -I/home/builder/.arduino15/packages/arduino/hardware/sam/1.6.12/variants/arduino_due_x /tmp/arduino-build-F18E16C7BE23C2CA9C35509222C4B25D/sketch/sketch_may28a.ino.cpp -o /tmp/arduino-build-F18E16C7BE23C2CA9C35509222C4B25D/sketch/sketch_may28a.ino.cpp.o

/tmp/069014772/sketch_may28a/sketch_may28a.ino: In function 'void loop()':

/tmp/069014772/sketch_may28a/sketch_may28a.ino:23:18: error: 'tone' was not declared in this scope

tone(8,pitch,20);

^

Error during build: exit status 1

Any suggestions or thoughts on how to rectify this issue or errors that you can see. I have triple checked it so not sure how to fix.

Thanks

Check out this post:

Please edit your post and put your code into code tags, they look like this </>

I compiled your code just fine. Perhaps there is something wrong with your installation. Are you using the IDE? Which version?

Compiles for me as well. The error message that you posted does not go with the code that you posted.

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

There is the built in tone() function and there is an external library called Tone library. The difference is the case of the 'T'. The Tone library does not come with the IDE and would have to be installed. Install the library using the IDE library manager.

Thanks. Updated my post. Never had an issue before with the first 5 projects

I updated my post and add the error information from the online code editor.

THanks!

Try it in the IDE, it work just fine. There must be an issue with the online editor.

Tone is not supported on the Due.
https://github.com/arduino/ArduinoCore-sam/issues/24

There are probably work arounds using a Timer or PWM library for the Due but I think it will be a rather complicated solution.

Can you get an Arduino with an AT328 processor like the Uno or the Nano? The Due is not a good choice for a beginner.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.