Go Down

Topic: Error with tone library (Read 2402 times) previous topic - next topic

Strontium

First of all, here is my code:
Code: [Select]
#include <Tone.h>

#define enter 48

Tone = tone1;

void setup() {
 pinMode(53, INPUT);
 pinMode(51, INPUT);
 pinMode(49, INPUT);
 pinMode(47, INPUT);
 pinMode(45, INPUT);
 pinMode(52, OUTPUT);
 pinMode(50, OUTPUT);
 pinMode(48, OUTPUT);

}

void loop() {
 if (enter == HIGH) {
   if (53 == HIGH && 51 == HIGH && 49 == LOW && 47 == LOW && 45 == LOW) {
     digitalWrite(48, HIGH);
   }
   else {
     tone1.begin(52);
     tone1.play(262, 500);
   }
 }
}


Whats happening is it checks if the passcode is correct, and if it is, it lights up an LED. If it isn't, it makes a buzzing noise.

Heres the problem:
I cannot get the tone library to work correctly. (This specific tone library: https://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation).

Here is my error message (Verbose mode):
Code: [Select]
Arduino: 1.5.3 (Mac OS X), Board: "Arduino Due (Programming Port)"

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=153 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due" -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/libsam -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/CMSIS/Include/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/Device/ATMEL/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x -I/Users/mmforde1/Documents/Arduino/libraries/Tone /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build5746105419377166516.tmp/Passcode.cpp -o /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build5746105419377166516.tmp/Passcode.cpp.o
Passcode:8: error: expected constructor, destructor, or type conversion before '=' token
Passcode.ino: In function 'void loop()':
Passcode:28: error: 'tone1' was not declared in this scope


I am using an Arduino Due, and am uploading via the programming port.

Can someone please explain why this is happening?

Please excuse any silly mistakes I made, because an 11 year old brain doesn't have the mental capacity to create good code.

MichaelMeissner


First of all, here is my code:
Code: [Select]
#include <Tone.h>

#define enter 48

Tone = tone1;


Please excuse any silly mistakes I made, because an 11 year old brain doesn't have the mental capacity to create good code.

Your problem is on the last line I quoted.  You should not have the equal sign for that line.  The correct line should be:

Code: [Select]

Tone tone1;


What this does is create a variable by the name tone1 that is of the class Tone.  You then use tone1 else where in the program.

Strontium



First of all, here is my code:
Code: [Select]
#include <Tone.h>

#define enter 48

Tone = tone1;


Please excuse any silly mistakes I made, because an 11 year old brain doesn't have the mental capacity to create good code.

Your problem is on the last line I quoted.  You should not have the equal sign for that line.  The correct line should be:

Code: [Select]

Tone tone1;


What this does is create a variable by the name tone1 that is of the class Tone.  You then use tone1 else where in the program.


Just tried that; new error:
Code: [Select]
Arduino: 1.5.3 (Mac OS X), Board: "Arduino Due (Programming Port)"

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=153 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due" -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/libsam -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/CMSIS/Include/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/Device/ATMEL/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x -I/Users/mmforde1/Documents/Arduino/libraries/Tone /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build5746105419377166516.tmp/Passcode.cpp -o /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build5746105419377166516.tmp/Passcode.cpp.o
Passcode:8: error: 'Tone' does not name a type
Passcode.ino: In function 'void loop()':
Passcode:28: error: 'tone1' was not declared in this scope

MichaelMeissner

Arduino does not include the more general tone library.  You will have to go here to find the library, and then install it in the correct location on your system (~/sketchbook/libraries/<dir> on Linux, I don't remember where it is on other OSes).  http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

The Arduino base code has a simplified tone library.  It does not have a Tone class, and you can only generate one tone at a time.  That library is documented here: http://arduino.cc/en/Reference/Tone.

Strontium


Arduino does not include the more general tone library.  You will have to go here to find the library, and then install it in the correct location on your system (~/sketchbook/libraries/<dir> on Linux, I don't remember where it is on other OSes).  http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation

The Arduino base code has a simplified tone library.  It does not have a Tone class, and you can only generate one tone at a time.  That library is documented here: http://arduino.cc/en/Reference/Tone.

The library I am using is installed.

I have tried using the other tone library, but I get a similar error to "tone() was not declared in this scope)", so I assume I need to include it, but I do not know the name of it.

Nick Gammon

Please post your modified code.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Strontium


Nick Gammon

Your original post had some code. Presumably you modified it? Can you post the new code please? In the context of this forum (whatever that means).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Strontium

#8
Sep 22, 2013, 08:11 am Last Edit: Sep 22, 2013, 08:13 am by Strontium Reason: 1

Your original post had some code. Presumably you modified it? Can you post the new code please? In the context of this forum (whatever that means).

Alright. It hasn't really changed except for the first error I made (When I typed "Tone = tone1;" instead of "Tone tone1;".
Here it is:
Code: [Select]
#include <Tone.h>

#define enter 48

Tone tone1;

void setup() {
 pinMode(53, INPUT);
 pinMode(51, INPUT);
 pinMode(49, INPUT);
 pinMode(47, INPUT);
 pinMode(45, INPUT);
 pinMode(52, OUTPUT);
 pinMode(50, OUTPUT);
 pinMode(48, OUTPUT);

}

void loop() {
 if (enter == HIGH) {
   if (53 == HIGH && 51 == HIGH && 49 == LOW && 47 == LOW && 45 == LOW) { //Checking the passcode
     digitalWrite(48, HIGH);
   }
   else { //Plays if passcode is incorrect
     tone1.begin(52);
     tone1.play(262, 500);
   }
 }
}  

Nick Gammon

Code: [Select]

#define enter 48


And given that:

Code: [Select]

#define HIGH 0x1


Then:

Code: [Select]

  if (enter == HIGH) {


Is testing if 1 is equal to 48. Why do this?

Anyway, since this is a Due issue I'll move it to the Due part of the forum.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Strontium


Code: [Select]

#define enter 48


And given that:

Code: [Select]

#define HIGH 0x1


Then:

Code: [Select]

  if (enter == HIGH) {


Is testing if 1 is equal to 48. Why do this?

Anyway, since this is a Due issue I'll move it to the Due part of the forum.

I was trying to see if pin 48 was equal to 1/on/high. Since that doesn't work, how would I do it?

*Note to make sure that the tone library is going to be solved too, and not moving over to this new problem*

Nick Gammon

Code: [Select]

  if (digitalRead (enter) == HIGH) {
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Strontium

#12
Sep 22, 2013, 06:56 pm Last Edit: Sep 22, 2013, 07:10 pm by Strontium Reason: 1

Code: [Select]

 if (digitalRead (enter) == HIGH) {


Ah. I am guessing that digital read checks the state of the specified pin?
EDIT:
Here is my new, modified code:
Code: [Select]
#include <Tone.h>

#define enter 48

Tone tone1;

void setup() {
  pinMode(53, INPUT);
  pinMode(51, INPUT);
  pinMode(49, INPUT);
  pinMode(47, INPUT);
  pinMode(45, INPUT);
  pinMode(52, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(48, OUTPUT);

}

void loop() {
  if (enter == HIGH) { //Run check process when "enter" is pressed
    if (digitalRead (53) == HIGH && digitalRead (51) == HIGH && digitalRead (49) == LOW && digitalRead (47) == LOW && digitalRead (45) == LOW) { //Checking the passcode
      digitalWrite(48, HIGH);
    }
    else { //Plays if passcode is incorrect
      tone1.begin(52);
      tone1.play(262, 500);
    }
  }
}   


I still get an error:
Code: [Select]
Arduino: 1.5.3 (Mac OS X), Board: "Arduino Due (Programming Port)"

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=153 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due" -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/libsam -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/CMSIS/Include/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/Device/ATMEL/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x -I/Users/mmforde1/Documents/Arduino/libraries/Tone /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build8539723148094633157.tmp/Passcode.cpp -o /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build8539723148094633157.tmp/Passcode.cpp.o
Passcode:5: error: 'Tone' does not name a type
Passcode.ino: In function 'void loop()':
Passcode:25: error: 'tone1' was not declared in this scope


Now, using Arduino's simplified tone library (I have indeed moved the piezo to a PWM pin):
Code: [Select]
#define enter 48

void setup() {
  pinMode(53, INPUT);
  pinMode(51, INPUT);
  pinMode(49, INPUT);
  pinMode(47, INPUT);
  pinMode(45, INPUT);
  pinMode(2, OUTPUT); //*NOTE PIEZO
  pinMode(50, OUTPUT);
  pinMode(48, OUTPUT);

}

void loop() {
  if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
    if (digitalRead (53) == HIGH && digitalRead (51) == HIGH && digitalRead (49) == LOW && digitalRead (47) == LOW && digitalRead (45) == LOW) { //Checking the passcode
      digitalWrite(48, HIGH);
    }
    else { //Plays if passcode is incorrect
      tone(2, 262, 500);
    }
  }
}   

Error:
Code: [Select]
Arduino: 1.5.3 (Mac OS X), Board: "Arduino Due (Programming Port)"

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/g++_arm_none_eabi/bin/arm-none-eabi-g++ -c -g -Os -w -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -mcpu=cortex-m3 -DF_CPU=84000000L -DARDUINO=153 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON -DUSB_MANUFACTURER="Unknown" -DUSB_PRODUCT="Arduino Due" -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/libsam -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/CMSIS/Include/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/system/CMSIS/Device/ATMEL/ -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/sam/variants/arduino_due_x /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build8539723148094633157.tmp/Passcode.cpp -o /var/folders/yz/cqtyzg8n7w1c0bf40mv6w8dw0000gn/T/build8539723148094633157.tmp/Passcode.cpp.o
Passcode.ino: In function 'void loop()':
Passcode:21: error: 'tone' was not declared in this scope


So, I tried using a snippet of code made by Calum (for his Song Of Storms Sketch) and it works! But its a little uglier:
Code: [Select]
#define enter 48
#define NOTE_C4  262
int speakerPin = 52;

void setup() {
  pinMode(53, INPUT);
  pinMode(51, INPUT);
  pinMode(49, INPUT);
  pinMode(47, INPUT);
  pinMode(45, INPUT);
  pinMode(52, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(48, OUTPUT);

}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)  //code for working out the rate at which each note plays and the frequency.
{
  int x;     
  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  for (x=0;x<loopTime;x++)   
  {   
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayAmount);
  }   
  delay(20);
}       

void loop() {
  if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
    if (digitalRead (53) == HIGH && digitalRead (51) == HIGH && digitalRead (49) == LOW && digitalRead (47) == LOW && digitalRead (45) == LOW) { //Checking the passcode
      digitalWrite(48, HIGH);
    }
    else { //Plays if passcode is incorrect
      beep(speakerPin, NOTE_C4, 500);
    }
  }
}   


So now, I shall upload it to my arduino and see if I did anything wrong.

Strontium

#13
Sep 22, 2013, 07:17 pm Last Edit: Sep 22, 2013, 07:42 pm by Strontium Reason: 1
So, I ran the code (here it is, by the way:
Code: [Select]
#define enter 48
#define NOTE_C4  262
int speakerPin = 52;

void setup() {
 pinMode(53, INPUT);
 pinMode(51, INPUT);
 pinMode(49, INPUT);
 pinMode(47, INPUT);
 pinMode(45, INPUT);
 pinMode(52, OUTPUT);
 pinMode(50, OUTPUT);
 pinMode(48, INPUT);

}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)  //code for working out the rate at which each note plays and the frequency.
{
 int x;      
 long delayAmount = (long)(1000000/frequencyInHertz);
 long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
 for (x=0;x<loopTime;x++)    
 {    
   digitalWrite(speakerPin,HIGH);
   delayMicroseconds(delayAmount);
   digitalWrite(speakerPin,LOW);
   delayMicroseconds(delayAmount);
 }    
 delay(20);
}        

void loop() {
 if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
   if (digitalRead (53) == HIGH && digitalRead (51) == HIGH && digitalRead (49) == LOW && digitalRead (47) == LOW && digitalRead (45) == LOW) { //Checking the passcode
     digitalWrite(48, HIGH);
   }
   else { //Plays if passcode is incorrect
     beep(speakerPin, NOTE_C4, 500);
   }
 }
}    
)
I fixed one of my type declarations (48 is an input, not output).

It did NOT work as expected. What instead happened is my LED stayed on, and my piezo kept on buzzing (frequency: 262). I have no idea if its the code or my circuitry, so I have created a file in Fritzing so someone could try to help me. (Fritzing file in the attachments). Also, you may notice I have a resistor coming from the 3.3v pin into the DIP switch. That was because I was worried about short circuitry. I don't know if I will need it or not.

Nick Gammon

How is this all wired up?

http://www.gammon.com.au/switches
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up