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.