#include <Servo.h>
const int blu = 2;
const int grn = 4;
const int red = 6;
const int buzz = 8;
const int but = 18;
const int sw = 19;
Servo servo;
int pos = 0;
// Example Melody 1
int melody2[] = {300};
int noteDurations2[] = {2};
int melodyLength2 = sizeof(melody2) / sizeof(melody2[0]);
// Function to play a customizable tone melody
void playMelody(int melody2[], int noteDurations2[], int melodyLength2) {
for (int i = 0; i < melodyLength2; i++) {
int noteDuration = 1000 / noteDurations2[i];
tone(buzz, melody2[i], noteDuration);
delay(noteDuration + 50); // add a small delay between notes for clarity
noTone(buzz); // stop the tone playing
}
}
void setup() {
Serial.begin(9600);
pinMode(blu, OUTPUT);
pinMode(grn, OUTPUT);
pinMode(red, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(but, INPUT_PULLUP);
pinMode(sw, INPUT_PULLUP);
servo.attach(7);
}
void loop() {
int butVal = digitalRead(but);
int swVal = digitalRead(sw);
Serial.print("Button Value: ");
Serial.println(butVal);
Serial.print("Switch Value: ");
Serial.println(swVal);
delay(500);
if (swVal == LOW) { // Adjusted condition
digitalWrite(blu, HIGH);
if (butVal == LOW) {
servo.write(0);
digitalWrite(grn, HIGH);
digitalWrite(red, LOW);
} else {
digitalWrite(grn, LOW);
digitalWrite(red, HIGH);
playMelody(melody2, noteDurations2, melodyLength2);
//delay(50); // Pause between melodies
noTone(buzz); // Stop the tone immediately when the switch is off
}
} else {
digitalWrite(blu, LOW);
digitalWrite(grn, LOW);
digitalWrite(red, LOW);
noTone(buzz); // Stop the tone immediately when the switch is off
servo.write(180);
}
if (swVal == LOW && butVal == LOW) {
servo.write(90);
}
if (swVal == LOW && butVal == HIGH) {
servo.write(0);
}
}
// Example Melody 1
//int melody1[] = {262, 294, 330, 349, 392, 440, 494, 523};
//int noteDurations1[] = {4, 4, 4, 4, 4, 4, 4, 4};
//int melodyLength1 = sizeof(melody1) / sizeof(melody1[0]);
// Play Melody 1
//playMelody(melody1, noteDurations1, melodyLength1);
//delay(1000); // Pause between melodies
// Example Melody 2
//int melody2[] = {523, 587, 659, 698, 784, 880, 988, 1047};
//int noteDurations2[] = {8, 8, 8, 8, 8, 8, 8, 8};
//int melodyLength2 = sizeof(melody2) / sizeof(melody2[0]);
// Play Melody 2
//playMelody(melody2, noteDurations2, melodyLength2);
//delay(1000); // Pause between melodies