#define BLYNK_TEMPLATE_ID "TMPL3vd8VWKcS"
#define BLYNK_TEMPLATE_NAME "musicplayer"
#define BLYNK_AUTH_TOKEN "w0sdA5NOL8e9j169PzTUJl07gFbluJn_" 
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
 
const char* ssid = "Wokwi-GUEST";
const char* password = "";
char auth[] = BLYNK_AUTH_TOKEN;




const int buttonPin1 = 34;  // Replace D7 with the actual pin you connect the button to
const int buttonPin2 = 35;  // Replace D6 with the actual pin you connect the button to
const int buttonPinplaypause = 32;  // Replace D5 with the actual pin you connect the button to

const int BUZZER_PIN = 18;  // Replace D1 with the actual pin you connect the buzzer to



bool musicState = false;
int currenttoneindex = 0;
int tones[3] = {1000, 2000, 3000};
int button1State = 0;
int button2State = 0;
int button3State = 0;

int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  
  Serial.begin(115200);
  Blynk.begin(auth, ssid, password);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPinplaypause, INPUT);

 
}
BLYNK_WRITE(V0) {
  int pinValue = param.asInt();  // Get the value from the Blynk app button widget
  if (pinValue == 1) {
    // Your logic when Blynk app button is pressed
    int reading = digitalRead(buttonPin1);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != button1State) {
      buttonState = reading;
 
      if (button1State == HIGH) {
        // Button pressed, play different sequences based on the currenttoneindex
        if (currenttoneindex == 0) {
          // Sequence 1
          for (int i = 0; i < 2; ++i) {
            tone(BUZZER_PIN, 1500); 
            usleep(200000);       
            noTone(BUZZER_PIN);
            usleep(100000);        

            tone(BUZZER_PIN, 2000);  
            usleep(200000);       
            noTone(BUZZER_PIN);
            usleep(100000);
          }
        } else if (currenttoneindex == 1) {
          // Sequence 2
          for (int i = 0; i < 2; ++i) {
            tone(BUZZER_PIN, tones[i]);
            delay(500);  // Adjust the delay between tones as needed
            noTone(BUZZER_PIN);
             delay(250);
          }
        } else if (currenttoneindex == 2) {
          // Sequence 3
          for (int i = 0; i < 3; ++i) {
            tone(BUZZER_PIN, 3000);
            delay(500);
            noTone(BUZZER_PIN);
            delay(250);
          }
        }
        currenttoneindex = (currenttoneindex + 1) % 3;
      }
    }
  }
 
  lastButtonState = reading;
}
}
BLYNK_WRITE(V1) {
  int pinValue = param.asInt();  // Get the value from the Blynk app button widget
  if (pinValue == 1) {
    // Your logic when Blynk app button is pressed
    tone(BUZZER_PIN, tones[currenttoneindex]);
    currenttoneindex = (currenttoneindex + 1 + sizeof(tones) / sizeof(tones[0])) % (sizeof(tones) / sizeof(tones[0]));
 
    delay(1000); // Adjust delay as needed
    noTone(BUZZER_PIN);
  }
}
BLYNK_WRITE(V2) {
  int pinValue = param.asInt();  // Get the value from the Blynk app button widget
  if (pinValue == 1) {
    // Your logic when Blynk app button is pressed
    tone(BUZZER_PIN, tones[currenttoneindex]);
    currenttoneindex = (currenttoneindex - 1 + sizeof(tones) / sizeof(tones[0])) % (sizeof(tones) / sizeof(tones[0]));
 
    delay(1000); // Adjust delay as needed
    noTone(BUZZER_PIN);
  }
}
void loop(){
  int newButton1State = digitalRead(buttonPin1);
  int newButton2State = digitalRead(buttonPin2);
  int newButton3State = digitalRead(buttonPinplaypause);
  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,1);
}
  // Your non-Blynk related logic here...
 
  delay(10); 
  int reading = digitalRead(buttonPin1);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }
 
 

  // Update button states for the next iteration
  button1State = newButton1State;
  button2State = newButton2State;
  button3State = newButton3State;

}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
bz1:1
bz1:2