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.