#define LED_PIN 2
#define BUZZER_PIN 4
#define SWITCH_PIN 12
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
Serial.begin(115200);
// LEDC atau LED Control adalah fungsi built-in pada ESP32
// LEDC dapat digunakan untuk membangkitkan sinyal PWM
ledcSetup(0, 2000, 8); // channel, frequency, duty cycle resolution
}
void loop() {
if(digitalRead(SWITCH_PIN) == HIGH)
{
digitalWrite(LED_PIN, HIGH);
myTone(BUZZER_PIN);
Serial.println("Switch ON");
} else {
digitalWrite(LED_PIN, LOW);
myNoTone(BUZZER_PIN);
Serial.println("Switch OFF");
}
}
void myTone( int pin)
{
ledcAttachPin(pin, 0); // pin, channel
ledcWriteNote(0, NOTE_C, 4); // channel, note, octave 1 i
delay(800);
ledcWriteNote(0, NOTE_D, 4); //2 bu
delay(300);
ledcWriteNote(0, NOTE_E, 4); //3 ki
delay(500);
ledcWriteNote(0, NOTE_F, 4); //4 ta
delay(500);
ledcWriteNote(0, NOTE_G, 4); //5 kar
delay(700);
ledcWriteNote(0, NOTE_E, 4); //ti
delay(300);
ledcWriteNote(0, NOTE_C, 4); //ni
delay(1000);
ledcWriteNote(0, NOTE_A, 4); //pu
delay(800);
ledcWriteNote(0, NOTE_C, 5); //tri
delay(300);
ledcWriteNote(0, NOTE_B, 4); //se
delay(500);
ledcWriteNote(0, NOTE_A, 4); //ja
delay(500);
ledcWriteNote(0, NOTE_G, 4); //ti
delay(1100);
ledcWriteNote(0, NOTE_F, 4); //pu
delay(900);
ledcWriteNote(0, NOTE_A, 4); //tri
delay(300);
ledcWriteNote(0, NOTE_G, 4); //in
delay(500);
ledcWriteNote(0, NOTE_F, 4); //do
delay(500);
ledcWriteNote(0, NOTE_E, 4); //ne
delay(1100);
ledcWriteNote(0, NOTE_C, 4); //sia
delay(1000);
ledcWriteNote(0, NOTE_D, 4); //ha
delay(800);
ledcWriteNote(0, NOTE_F, 4); //rum
delay(300);
ledcWriteNote(0, NOTE_E, 4); //na
delay(500);
ledcWriteNote(0, NOTE_D, 4); //ma
delay(600);
ledcWriteNote(0, NOTE_C, 4); //nya
delay(1000);
}
void myNoTone( int pin)
{
ledcDetachPin(pin);
}