How to a melody after a combination of keys is played?

Hello, I am struggling with my arduino project because I find it difficult to understand code. I want my piezo to play a melody after a certain combination of keys have been played, however I don’t know how to go about that. I’m afraid right now my code is a terrible mess, but at least it is playing the tones when the buttons are pressed. I took the code from the FSR Keyboard example, and am using the melody code from someone else as a place holder. How should I fix this up?

#include “pitches.h” //library
const int threshold = 10; // minimum reading of the sensors that generates a note

// notes to play, corresponding to the 5 sensors:
int notes = {
NOTE_D5, NOTE_B4,NOTE_A4,NOTE_F4, NOTE_D4 };
int button1 = 0;
int button2 = 1;
int button3 = 2;
int button4 = 3;
int button5 = 4;
int ledPin = 13;
int speakerPin = 8;

void setup() {

pinMode(ledPin, OUTPUT); // Setting both pins to outputs
pinMode(speakerPin, OUTPUT);

}

void loop() {
for (int thisSensor = 0; thisSensor < 5; thisSensor++) {
// get a sensor reading:
int sensorReading = analogRead(thisSensor);

// if the sensor is pressed hard enough:
if (sensorReading > threshold) {
// play the note corresponding to this sensor:
tone(8, notes[thisSensor], 20);
}
}

}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) //code for working out the rate at which each note plays and the frequency.
{
digitalWrite(ledPin, HIGH);
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds1000)/(delayAmount2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
digitalWrite(ledPin, LOW);
delay(20);
}

void song() //here is where all the notes for the song are played.
{
beep(speakerPin, NOTE_D5, 100); // beep( -PIN OF SPEAKER-, -THE NOTE WANTING TO BE PLAYED-, -DURATION OF THE NOTE IN MILISECONDS- )
delay(40);
beep(speakerPin, NOTE_F5, 100);
delay(30);
beep(speakerPin, NOTE_D6, 300);
delay(400);

beep(speakerPin, NOTE_D5, 100);
delay(40);
beep(speakerPin, NOTE_F5, 100);
delay(30);
beep(speakerPin, NOTE_D6, 300);
delay(400);

beep(speakerPin, NOTE_E6, 300);
delay(200);
beep(speakerPin, NOTE_F6, 100);
delay(100);
beep(speakerPin, NOTE_E6, 80);
delay(50);
beep(speakerPin, NOTE_F6, 100);
delay(50);
beep(speakerPin, NOTE_E6, 100);
delay(50);
beep(speakerPin, NOTE_C6, 100);
delay(80);
beep(speakerPin, NOTE_A5, 300);
delay(400);

beep(speakerPin, NOTE_A5, 200);
delay(80);
beep(speakerPin, NOTE_D5, 200);
delay(100);
beep(speakerPin, NOTE_F5, 100);
delay(80);
beep(speakerPin, NOTE_G5, 100);
delay(80);
beep(speakerPin, NOTE_A5, 300);
delay(500);

beep(speakerPin, NOTE_A5, 200);
delay(80);
beep(speakerPin, NOTE_D5, 200);
delay(100);
beep(speakerPin, NOTE_F5, 100);
delay(80);
beep(speakerPin, NOTE_G5, 100);
delay(80);
beep(speakerPin, NOTE_E5, 300);
delay(500);

beep(speakerPin, NOTE_D5, 80);
delay(50);
beep(speakerPin, NOTE_F5, 80);
delay(40);
beep(speakerPin, NOTE_D6, 400);
delay(350);

beep(speakerPin, NOTE_D5, 80);
delay(50);
beep(speakerPin, NOTE_F5, 80);
delay(40);
beep(speakerPin, NOTE_D6, 400);
delay(350);

beep(speakerPin, NOTE_E6, 300);
delay(200);
beep(speakerPin, NOTE_F6, 100);
delay(100);
beep(speakerPin, NOTE_E6, 80);
delay(80);
beep(speakerPin, NOTE_F6, 100);
delay(40);
beep(speakerPin, NOTE_E6, 100);
delay(50);
beep(speakerPin, NOTE_C6, 100);
delay(80);
beep(speakerPin, NOTE_A5, 300);
delay(300);

beep(speakerPin, NOTE_A5, 200);
delay(100);
beep(speakerPin, NOTE_D5, 200);
delay(80);
beep(speakerPin, NOTE_F5, 100);
delay(100);
beep(speakerPin, NOTE_G5, 100);
delay(80);
beep(speakerPin, NOTE_A5, 300);
delay(200);
beep(speakerPin, NOTE_A5, 300);
delay(50);
beep(speakerPin, NOTE_D5, 500);
delay(2000);

}

Please post your code using the # button when you enter text. You can edit the previous post to make it more readable.

 for (int thisSensor = 0; thisSensor < 5; thisSensor++) {
    // get a sensor reading:
    int sensorReading = analogRead(thisSensor);

    // if the sensor is pressed hard enough:
    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:
      tone(8, notes[thisSensor], 20);
    }

This code is for reading an analog sensor and you are doing it for digital pins 0 thru 4. The fact it works for your purpose at the moment is a big co-incidence. Digitals can be read as analog values but that is not the point of your requested help.

  1. To read a digital, you need to use digitalRead() and also need to make sure that the digital switches are configured in pull-up input mode (in setup()) if they do not have a pull-up resistor in the physical circuit. You will find plenty of examples for this.
  2. You also need to be able to detect the transition of a switch from deactivated to activated (ON to OFF or OFF to ON, depending on how it is wired up). The transition is important, not the final state. The reason for this is that the processor will detect a switch ON many times before it is OFF again. Again, lots of examples for this code.
  3. To detect a pattern of switches you need to specify the pattern in the code. This can be hard coded into the way the program works or can be driven by data in an array. The second method is more flexible for changing patterns.
  4. Assuming we are using an array, you look for a transition in the first ‘pin’ defined in the array (index 0 of the array). If that transition occurs, you the look for a transition in the second pin defined in the array (index 1 of the array). If the next transition is not with that pin, reset the array index to 0 and start again. If it is, increment the array index to look for the next array pin. Hopefully you see a pattern here. At each pass you need to scan all the digital pins to decide if they have changed state.
  5. Once you have recognised your combination, trigger whatever you want in the code.

The way the music is being played has a lot of delay() statements. This may be a problem for you while the music is playing as you will not be able to do anything else with the processor (respond to inputs, implement a ‘Cancel’ function, etc). However, at this stage I would focus on getting to the point of playing the tune and worry about making that part of the code more efficient later.