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() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPinplaypause, INPUT);
}
void loop(){
int newButton1State = digitalRead(buttonPin1);
int newButton2State = digitalRead(buttonPin2);
int newButton3State = digitalRead(buttonPinplaypause);
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;
// ... (your existing code)
if (newButton2State == HIGH && button2State == LOW) {
// Button 2 logic - Play a sequence
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);
}
if (newButton3State == HIGH && button3State == LOW) {
// Button 3 logic - Decrease tone
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);
}
// Update button states for the next iteration
button1State = newButton1State;
button2State = newButton2State;
button3State = newButton3State;
}