Problems with my Pushbutton Musical Keyboard Safe

Hi everyone,

I am brand new to Arduino, and for a creative project I decided to build a pushbutton musical keyboard safe. Basically what I have are 5 pushbuttons and a Piezo speaker, and when I press the buttons they make a tone. What I want to do is press a 4 note sequence that will turn the Servo motor (acts as my lock).

So far, I have managed to turn my Servo motor with the press of 1 button, and I am unsure on how to code so that if I press 5 buttons, the Servo motor will turn. My sketch is attached and I typed it out below.

Thank you in advance for your help, and sorry for any possible coding or space issues… I’m learning!

//“define” is similar to const int, where a constant variable can be named and either called later in the code and enables me to give a name to these constant values
#define NOTE_C 262 //defines the note “C” with its respective frequency
#define NOTE_D 294 //defines the note “D” with its respective frequency
#define NOTE_E 330 //defines the note “E” with its respective frequency
#define NOTE_F 349 //defines the note “F” with its respective frequency
#define NOTE_G 392 //defines the note “G” with its respective frequency

#define ACTIVATED LOW //this statement makes it easy to deal with button INPUT later in the code. When the button is pressed, the tone becomes activated

const int PIEZO = 11; //the piezo speaker is assigned to digital pin 11

const int BUTTON_C = 10; //The button playing the C note is assigned to digital pin 10
const int BUTTON_D = 9; //The button playing the D note is assigned to digital pin 9
const int BUTTON_E = 8; //The button playing the E note is assigned to digital pin 8
const int BUTTON_F = 7; //The button playing the F note is assigned to digital pin 7
const int BUTTON_G = 6; //The button playing the G note is assigned to digital pin 6

#include <Servo.h> //imports the servo library so it is available to use in the code
int pos = 0; //sets initial position of servo to 0 degrees
Servo myServo; //creates an object so it can be called throuhgout the code

void setup(){
// put your setup code here, to run once:
myServo.attach(12); //tells the Arduino what pin the Servo is attached to

pinMode(BUTTON_C, INPUT); //sets the digital pin to input
digitalWrite(BUTTON_C,HIGH); //let Button C stay on
pinMode(BUTTON_D, INPUT); //sets the digital pin to input
digitalWrite(BUTTON_D,HIGH); //let Button D stay on
pinMode(BUTTON_E, INPUT); //sets the digital pin to input
digitalWrite(BUTTON_E,HIGH); //let Button E stay on
pinMode(BUTTON_F, INPUT); //sets the digital pin to input
digitalWrite(BUTTON_F,HIGH); //let Button F stay on
pinMode(BUTTON_G, INPUT); //sets the digital pin to input
digitalWrite(BUTTON_G,HIGH); //let Button G stay on
}

void loop(){
// put your main code here, to run repeatedly:
if (digitalRead(BUTTON_E) == LOW){
myServo.write(180); //spin the servo 180 degrees
}

while(digitalRead(BUTTON_C) == ACTIVATED) //while Button C is being pressed, play the note C from the speaker
{
tone(PIEZO,NOTE_C);
}

while(digitalRead(BUTTON_D) == ACTIVATED) //while Button D is being pressed, play the note C from the speaker
{
tone(PIEZO,NOTE_D);
}

while(digitalRead(BUTTON_E) == ACTIVATED) //while Button E is being pressed, play the note C from the speaker
{
tone(PIEZO,NOTE_E);
}

while(digitalRead(BUTTON_F) == ACTIVATED) //while Button F is being pressed, play the note C from the speaker
{
tone(PIEZO,NOTE_F);
}
while(digitalRead(BUTTON_G) == ACTIVATED) //while Button G is being pressed, play the note C from the speaker
{
tone(PIEZO,NOTE_G);
}
noTone(PIEZO); //otherwise, don’t play anything.
}

ThirdCircuit.ino (3 KB)

Is is necessary to press the 4 switches (you did NOT sew buttons onto the Arduino, did you?) in some specific order? Or, is it only necessary to have pressed the 4 switches at some point?

If the order matters, how long should one have to press the 4 switches? One a day over 4 days is as good as press the 4th switch within 3 seconds of pressing the first switch?

You need to have well understood requirements before you can begin coding.

I would like for my Servo motor to turn if my 4 buttons are pressed in a specific order, sort of like a combination lock. And, it doesn't matter how long you wait to press the buttons in the order; I was envisioning more like a 2 second wait between pressing buttons.

if my 4 buttons are pressed in a specific order

How do you define "a specific order"? You need an array to record the switch numbers in.

Periodically, like every time a switch is pressed, see if the array contains the correct number of switch numbers, and that the order is correct.

You'll need to consider what happens if you press the switches in the wrong order. How will you reset?