#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int POT_PIN = A0;
const int BUTTON_PIN = 2;
const int LED_PIN = 3;
const int BUZZER_PIN = 9;
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool ligado = false;
bool lastButtonState = HIGH;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(115200);
}
void loop() {
bool currentButton = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && currentButton == LOW) {
ligado = !ligado;
delay(50);
}
lastButtonState = currentButton;
int potValue = analogRead(POT_PIN);
int freq = map(potValue, 0, 1023, 100, 2000);
if (ligado) {
tone(BUZZER_PIN, freq);
digitalWrite(LED_PIN, HIGH);
} else {
noTone(BUZZER_PIN);
digitalWrite(LED_PIN, LOW);
}
lcd.setCursor(0, 0);
lcd.print("Pot: ");
lcd.print(potValue);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Freq: ");
lcd.print(freq);
lcd.print(" Hz ");
Serial.print("Pot: ");
Serial.print(potValue);
Serial.print(" | Freq: ");
Serial.println(freq);
delay(100);
}