#include <Arduino.h>
const int TOMBOL_PIN = 14;
const int OUTPUT_PIN = 12;
const int POT_PIN = 34;
const int PWM_PIN = 25;
const int PWM_RES = 9;
const int DUTY_50 = 256;
int currentFrequency = 0;
void setup() {
Serial.begin(115200);
pinMode(POT_PIN, INPUT);
pinMode(TOMBOL_PIN, INPUT);
pinMode(OUTPUT_PIN, OUTPUT);
if (!ledcAttach(PWM_PIN, 10, PWM_RES)) {
Serial.println("Gagal attach PWM");
while (1);
}
ledcWrite(PWM_PIN, DUTY_50);
}
void loop() {
int potValue = analogRead(POT_PIN);
int freq = map(potValue, 0, 4095, 10, 100000);
if (freq != currentFrequency) {
if (ledcChangeFrequency(PWM_PIN, freq, PWM_RES)) {
Serial.print("Frekuensi PWM: ");
Serial.print(freq);
Serial.println(" Hz");
currentFrequency = freq;
} else {
Serial.println("Gagal ubah frekuensi");
}
}
// === Kontrol output pin 12 dari tombol di pin 14 ===
int tombolState = digitalRead(TOMBOL_PIN);
digitalWrite(OUTPUT_PIN, tombolState);
delay(100);
}