Code not doing what I want it to do

I am new to Arduino so forgive me if this is simple or my code is unnecessarily long. I am trying to make a sketch in which the keypad can select a song if I type in a certain key, but no matter what key I press, the song plays. if you know any reason why this would occur, help would be very much appreciated!

[#include <Keypad.h>
// LiquidCrystal - Version: Latest 
#include <LiquidCrystal.h>
char songnum[2];


int currentlength = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, A5, A4, A2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A1, A0, A3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(7,9,10,11,12,13); // the pins we use on the LCD
int P = 8;
void setup() {
 #define e 329.63
 #define d 293.66
 #define c 261.63
 #define g 392.00
 lcd.begin(16,2);
 lcd.print("choose a song:");
 lcd.setCursor(0, 1);
 lcd.cursor();
 Serial.begin(9600);
songnum[1]=0;
songnum[2]=0;
 while(currentlength<2){
  char key = keypad.getKey();
 key == NO_KEY;
 if (key != NO_KEY)
 {
   if ((key != '*')&&(key != '#'))
   { 
   lcd.print(key);
   songnum[currentlength] = key;
   currentlength++;
  
}
}
}
}
void loop() {


 
   
  
  
 
  
  

   
   
 
 
   
 
  
     
   
if ((currentlength==2)and(songnum[1]==1));{
 tone(P, e, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, c, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, e, 250);
 delay(500);
 tone(P, e, 250);
 delay(500);
 tone(P, e, 500);
 delay(1000);
 tone(P, d, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, d, 500);
 delay(1000);
 tone(P, e, 250);
 delay(500);
 tone(P, g, 250);
 delay(500);
 tone(P, g, 500);
 delay(1000);
 tone(P, e, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, c, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, e, 250);
 delay(500);
 tone(P, e, 250);
 delay(500);
 tone(P, e, 500);
 delay(1000);
 tone(P, d, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, e, 250);
 delay(500);
 tone(P, d, 250);
 delay(500);
 tone(P, c, 1000);
 delay(1500);
lcd.print(songnum[1]);
currentlength++;
}

 }
  ]

if ((currentlength==2)and(songnum[1]==1));

Surely you didn't mean to have the ;

Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

I erased the semicolon but now the music wont play at all.

Please edit your post to add code tags ("</>" editor button).

I erased the semicolon but now the music wont play at all.

Then something is not evaluating the way you think.

Show us your new version with the ; removed

Use CTRL T to format your new version.

Use code tags.

#include <Keypad.h>
// LiquidCrystal - Version: Latest 
#include <LiquidCrystal.h>
char songnum[2];


int currentlength = 0;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, A5, A4, A2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A1, A0, A3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(7,9,10,11,12,13); // the pins we use on the LCD
int P = 8;
void setup() {
    #define e 329.63
    #define d 293.66
    #define c 261.63
    #define g 392.00
    lcd.begin(16,2);
    lcd.print("choose a song:");
    lcd.setCursor(0, 1);
    lcd.cursor();
    Serial.begin(9600);
   songnum[1]=0;
   songnum[2]=0;
    while(currentlength<2){
     char key = keypad.getKey();
    key == NO_KEY;
    if (key != NO_KEY)
    {
      if ((key != '*')&&(key != '#'))
      { 
      lcd.print(key);
      songnum[currentlength] = key;
      currentlength++;
     
  }
}
}
}
void loop() {


    
      
     
     
    
     
     
  
      
      
    
    
      
    
     
        
      
  if ((currentlength==2)&&(songnum[1]==1)){
    tone(P, e, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, c, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, e, 250);
    delay(500);
    tone(P, e, 250);
    delay(500);
    tone(P, e, 500);
    delay(1000);
    tone(P, d, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, d, 500);
    delay(1000);
    tone(P, e, 250);
    delay(500);
    tone(P, g, 250);
    delay(500);
    tone(P, g, 500);
    delay(1000);
    tone(P, e, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, c, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, e, 250);
    delay(500);
    tone(P, e, 250);
    delay(500);
    tone(P, e, 500);
    delay(1000);
    tone(P, d, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, e, 250);
    delay(500);
    tone(P, d, 250);
    delay(500);
    tone(P, c, 1000);
    delay(1500);
   lcd.print(songnum[1]);
   currentlength++;
  }
  
    }

Try

if ((currentlength == 2) && (songnum[1] == '1'))

It still won't make a sound.

This line is writing out of bounds and trashing memory:

   songnum[2]=0;

songnum[] is a two element array and arrays are indexed starting from 0.

Delete this line. It does nothing useful.

    key == NO_KEY;

By the time loop() is executed, the variable "currentlength" is always 2. Did you fix the ==1 as suggested above? If so, the tune will be played ONLY if the last valid key entered is '1'.

  if ((currentlength==2)&&(songnum[1]==1)){

For one thing you only set key in setup(). This only runs once

In loop() you never look for other keys, so unless your play test is satisfied in setup() your tune will never play.

Allan

THANK YOU SO MUCH!!!! I stayed up to 12:00 last night trying to get this to work and because of you it has! My problem was I was thinking it was starting at songnum[1], but it was starting at songnum[0], so my second digit has to be 1.

And remember,
1 is one,
'1' is ASCII 1 (49) as defined in your keypad array

Luke21206:
THANK YOU SO MUCH!!!! I stayed up to 12:00 last night trying to get this to work and because of you it has! My problem was I was thinking it was starting at songnum[1], but it was starting at songnum[0], so my second digit has to be 1.

I am not criticizing you or your program. But this is a perfect example of why it is important to write your program in steps and to be sure that the step is producing what you want. Use the Serial.print() at each step to display the variable you are using in each instruction and the result you are getting. Once that is ok, write the next set of logic instructions, remove the old Serial.print() and add them to the new set of instructions.

Paul