Error with tone library

First of all, here is my 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):

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.

Strontium: First of all, here is my code:

#include 

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:

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.

MichaelMeissner:

Strontium:
First of all, here is my 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:

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:

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

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/

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.

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/

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.

Please post your modified code.

[quote author=Nick Gammon link=topic=189322.msg1400630#msg1400630 date=1379829541] Please post your modified code. [/quote] In what context?

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

[quote author=Nick Gammon link=topic=189322.msg1400637#msg1400637 date=1379830137] 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). [/quote] 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:

#include 

#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);
    }
  }
}
#define enter 48

And given that:

#define HIGH 0x1

Then:

  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.

[quote author=Nick Gammon link=topic=189322.msg1400647#msg1400647 date=1379831350]

#define enter 48

And given that:

#define HIGH 0x1

Then:

  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. [/quote] 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

  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:

#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:

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):

#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:

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:

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

So, I ran the code (here it is, by the way:

#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.6 KB)

How is this all wired up?

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

[quote author=Nick Gammon link=topic=189322.msg1401552#msg1401552 date=1379886151] How is this all wired up?

http://www.gammon.com.au/switches [/quote] I'll start wiring that all up. I never knew that can happen.

Alright, so I have added pull down resistors to all my switches (including the button). Now I am getting some very strange behavior.

When I first start it up, it plays a very high pitched noise until I press my "enter" button. Then, it plays the correct note for the correct duration.

My LED is staying on the WHOLE time, and I don't really know why.

When I configure my switches (where the passcode is entered), then press the "enter" button, it does nothing, whether the passcode is wrong or not.

I'll post the new version once I get the Fritzing file edited.

Alright, new Fritzing file with the pull-down resistors. (In attachments)

Passcode CIRCUIT.fzz (12.7 KB)

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

How about some meaningful constants here, like LED for the LED, and so on? This is just guesswork looking at this.

#define enter 48
 ...
  if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
   ...
      digitalWrite(48, HIGH);

If pin 48, which is an input, is HIGH, write HIGH to it. ?

This is why you need symbols and not sprinkle numbers all through your code.