Pages: [1] 2   Go Down
Author Topic: Error with tone library  (Read 1978 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, here is my code:
Code:
#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:
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.
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, here is my code:
Code:
#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:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all, here is my code:
Code:
#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:
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:
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
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1847
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your modified code.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your modified code.
In what context?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
    }
  }
}  
« Last Edit: September 22, 2013, 01:13:36 am by Strontium » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define enter 48

And given that:

Code:
#define HIGH 0x1

Then:

Code:
  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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define enter 48

And given that:

Code:
#define HIGH 0x1

Then:

Code:
  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*
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (digitalRead (enter) == HIGH) {
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 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:
#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:
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:
#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:
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:
#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.
« Last Edit: September 22, 2013, 12:10:56 pm by Strontium » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, I ran the code (here it is, by the way:
Code:
#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.

* Passcode CIRCUIT.fzz (11.61 KB - downloaded 18 times.)
« Last Edit: September 22, 2013, 12:42:08 pm by Strontium » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How is this all wired up?

http://www.gammon.com.au/switches
Logged


Pages: [1] 2   Go Up
Jump to: