// LED Brightness Control + Buzzer sound + Push Button with Arduino Uno
// Connect a potentiometer to analog pin A0
const int potPin = A0;
const int ledPin = 9;
const int pushButtonPin = 2;
const int buzzerPin = 8;
// Set time to 1s
const int timer = 1000;
void setup() {
pinMode(potPin, INPUT);
pinMode(pushButtonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read button state
int buttonState = digitalRead(pushButtonPin);
// Read potentiometer value (0-1023)
int potValue = analogRead(potPin);
// Map potentiometer value to LED brightness (0-255)
int brightness = map(potValue, 0, 1023, 0, 255);
// Map potentiometer value to Buzzer loudness (0-500)
int loudness = map(potValue, 0, 1023, 0, 500);
// If the button state is HIGH (pressed)
if (buttonState == HIGH) {
// Set LED brightness
analogWrite(ledPin, brightness);
// Set Buzzer loudness
tone(buzzerPin, loudness, timer);
} else {
// If the button is not pressed, turn off LED and buzzer
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
}
}