//#include <LiquidCrystal.h>
//#include <Wire.h>
// #include "tone32.h"
 
/*const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPin3 = 26;
const int buttonPin4 = 27;
const int BUZZER_PIN = 14;
const int lcdAddr = 0x27;
 
 LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
 
void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
 
  // lcd.init();
  // lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("ESP32 LCD Demo");
}
 
void loop() {
  while(1){
  if (digitalRead(buttonPin1) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 1 pressed");
    tone(BUZZER_PIN, 1000, 100); // Buzzer sound for Button 1
  }
 
  if (digitalRead(buttonPin2) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 2 pressed");
    tone(BUZZER_PIN, 1500, 100); // Buzzer sound for Button 2
  }
 
  if (digitalRead(buttonPin3) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 3 pressed");
    tone(BUZZER_PIN, 2000, 100); // Buzzer sound for Button 3
  }
 
  if (digitalRead(buttonPin4) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 4 pressed");
    tone(BUZZER_PIN, 2500, 100); // Buzzer sound for Button 4
  }
}
  
  // Add any other tasks or code you need
}*/
#define BLYNK_TEMPLATE_ID "TMPL3vd8VWKcS"
#define BLYNK_TEMPLATE_NAME "musicplayer"
#define BLYNK_AUTH_TOKEN "w0sdA5NOL8e9j169PzTUJl07gFbluJn_"

#include <LiquidCrystal.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
 
const char* ssid = "Wokwi-GUEST";
const char* password = "";
char auth[] = BLYNK_AUTH_TOKEN;
 
const int buttonPin1 = 13;
const int buttonPin2 = 12;
const int buttonPinplaypause = 26;
// const int buttonPin4 = 27;
const int BUZZER_PIN = 14;
const int lcdAddr = 0x27;
bool musicState = false;
int currenttoneindex=0;
int tones[]={1000,2000,3000};
int button1State=0;
int button2State=0;
int button3State=0;
// int button4State=0;
 
LiquidCrystal lcd(0x27, 19, 23, 18, 17, 16, 15);
 
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, password);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPinplaypause, INPUT);
  // pinMode(buttonPin4, INPUT);
  lcd.begin(16,2);
  lcd.setCursor(0, 0);
  lcd.print("ESP32 LCD Demo");
}
  BLYNK_WRITE(V0){
  int val=param.asInt();   
  if(val==1){
//  if (digitalRead(buttonPin1) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 1 pressed");
    tone(BUZZER_PIN, tones[currenttoneindex]);
    currenttoneindex=(currenttoneindex+1)%(sizeof(tones)/(sizeof(tones[0])));
    delay(1000);
    noTone(BUZZER_PIN);
  //   tone(BUZZER_PIN, 1000);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);            // Add a short pause between tones

  // tone(BUZZER_PIN, 1500);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);            // Add a short pause between tones

  // tone(BUZZER_PIN, 2000);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);
  //    tone(BUZZER_PIN, 1000);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);            // Add a short pause between tones

  // tone(BUZZER_PIN, 1500);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);            // Add a short pause between tones

  // tone(BUZZER_PIN, 2000);  // Adjust frequency for the siren sound
  // delay(200);            // Adjust duration of the tone
  // noTone(BUZZER_PIN);
  // delay(100);
  }
  }
  BLYNK_WRITE(V1){
  int val=param.asInt();
  if(val==1){
//  if (digitalRead(buttonPin1) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("Button 2 pressed");
   for (int i = 0; i < 5; ++i) {
    tone(BUZZER_PIN, 1500);  // Adjust frequency for the siren sound
    usleep(200000);        // Adjust duration of the tone (in microseconds)
    noTone(BUZZER_PIN);
    usleep(100000);        // Add a short pause between tones

    tone(BUZZER_PIN, 2000);  // Adjust frequency for the siren sound
    usleep(200000);        // Adjust duration of the tone
    noTone(BUZZER_PIN);
    usleep(100000);
     // Buzzer sound for Button 1
  // }
  }
  }
  }
  //buttonPinplaypause
  BLYNK_WRITE(V2){
  int val=param.asInt();
  if(val==1){
   for (int i = 0; i < 5; ++i) {
    tone(BUZZER_PIN, 2500);  // Adjust frequency for the emergency alarm sound
    usleep(300000);         // Adjust duration of the tone (in microseconds)
    noTone(BUZZER_PIN);
    usleep(300000);         // Add a short pause between tones

    tone(BUZZER_PIN, 3000);  // Adjust frequency for the emergency alarm sound
    usleep(300000);         // Adjust duration of the tone
    noTone(BUZZER_PIN);
    usleep(300000);         // Add a short pause between tones
  }
// //  if (digitalRead(buttonPin1) == HIGH) {
//     lcd.setCursor(0, 1);
//     musicState = !musicState;
//     if(musicState){
//      // tone(BUZZER_PIN, 2000, 100); 
//       lcd.print("music on");
//     }else{
//       noTone(BUZZER_PIN);
//       lcd.print("music of");
//     }
//     // lcd.print("Button 3 pressed");
   
//     // Buzzer sound for Button 1
  // }
  }
  }
  
//   BLYNK_WRITE(V3){
//   int val=param.asInt();
//   if(val==1){
// //  if (digitalRead(buttonPin1) == HIGH) {
//     lcd.setCursor(0, 1);
//     lcd.print("Button 4 pressed");
//     tone(BUZZER_PIN, 2500, 100); 
//     // Buzzer sound for Button 1
//   // }
//   } 
//   }
void loop() {
  Blynk.run();
 button1State = digitalRead(buttonPin1);
if(button1State == HIGH){
  Blynk.virtualWrite(V0,1);
}
 button2State = digitalRead(buttonPin2);
if(button2State == HIGH){
  Blynk.virtualWrite(V1,1);
}
 button3State = digitalRead(buttonPinplaypause);
if(button3State == HIGH){
  Blynk.virtualWrite(V2,musicState ? 1 : 0);
}
//  button4State = digitalRead(buttonPin4);
// if(button4State == HIGH){
//   Blynk.virtualWrite(V3,1);
// }

 }
 
 
  // if (digitalRead(buttonPin2) == HIGH) {
  //   lcd.setCursor(0, 1);
  //   lcd.print("Button 2 pressed");
  //   tone(BUZZER_PIN, 1500, 100); // Buzzer sound for Button 2
  // }
 
  // if (digitalRead(buttonPin3) == HIGH) {
  //   lcd.setCursor(0, 1);
  //   lcd.print("Button 3 pressed");
  //   tone(BUZZER_PIN, 2000, 100); // Buzzer sound for Button 3
  // }
 
  // if (digitalRead(buttonPin4) == HIGH) {
  //   lcd.setCursor(0, 1);
  //   lcd.print("Button 4 pressed");
  //   tone(BUZZER_PIN, 2500, 100); // Buzzer sound for Button 4
  // }