#include "pitches.h"

#define SPEAKER_PIN 8

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C newLcd(0x27, 20, 4);

boolean flag=true;
int button=0;
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const uint8_t ledPins[] = { 3, 2, 1, 0, 13};

const int buttonTones[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
  NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
int ledOn=3;

void setup() {
  for (uint8_t i = 0; i < numTones; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  for (uint8_t i = 0; i < 5; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  pinMode(SPEAKER_PIN, OUTPUT);
  newLcd.init();
  newLcd.backlight();
  newLcd.setCursor(1, 0);
 
}

void loop() {
  int pitch = 0;
  for (uint8_t i = 0; i < numTones; i++) {
    if (digitalRead(buttonPins[i]) == LOW) {
      pitch = buttonTones[i];
      if(i<5){
        digitalWrite(ledPins[i], HIGH);
        ledOn=ledPins[i];
      }
      button=buttonPins[i];
    }
  }
      
  if (pitch) {
    tone(SPEAKER_PIN, pitch);    
    if(flag){
      newLcd.clear();
      newLcd.print("presed: ");
      newLcd.print(button);
      newLcd.noCursor();
      flag=false;
    }    
  } else {
    digitalWrite(ledOn, LOW);
    newLcd.clear();
     newLcd.noCursor();
     flag=true;
     noTone(SPEAKER_PIN);
  }
}