// Pin definitions
const int SWITCH_PIN = 14;
const int LED_SWITCH_PIN = 15;
const int BUTTON_PIN = 12;
const int LED_BUTTON_PIN = 13;
const int POT_PIN = A0; // GPIO 26 (ADC0)
const int LED_POT_PIN = 11;
// Constants
const unsigned long DEBOUNCE_MS = 50; // Debounce time for button
const int ADC_MAX = 1023; // 10-bit ADC resolution in Arduino (Wokwi)
const int PWM_MAX = 255; // PWM range for LED brightness
// Variables for button debouncing
unsigned long last_button_time = 0;
bool last_button_reading = HIGH; // Default HIGH due to INPUT_PULLUP
bool button_state = HIGH;
bool led_button_state = LOW; // Tracks button LED state
void setup() {
// Initialize serial for debugging
Serial.begin(115200);
// Initialize pins
pinMode(SWITCH_PIN, INPUT_PULLUP); // Switch with internal pull-up
pinMode(LED_SWITCH_PIN, OUTPUT); // Switch LED
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button with internal pull-up
pinMode(LED_BUTTON_PIN, OUTPUT); // Button LED
pinMode(POT_PIN, INPUT); // Potentiometer (ADC)
pinMode(LED_POT_PIN, OUTPUT); // Potentiometer LED (PWM)
}
void loop() {
// Read switch state and control its LED
int switch_state = digitalRead(SWITCH_PIN);
digitalWrite(LED_SWITCH_PIN, switch_state); // LED ON when switch OFF (HIGH)
// Read button state (with non-blocking debouncing)
bool current_button = digitalRead(BUTTON_PIN);
if (current_button != last_button_reading && (millis() - last_button_time) >= DEBOUNCE_MS) {
if (current_button == LOW) { // Button pressed (LOW due to INPUT_PULLUP)
led_button_state = !led_button_state; // Toggle LED state
digitalWrite(LED_BUTTON_PIN, led_button_state);
last_button_time = millis();
}
}
last_button_reading = current_button;
// Read potentiometer and control LED brightness
int pot_value = analogRead(POT_PIN); // Read ADC (0 to 1023)
int brightness = pot_value / (ADC_MAX / PWM_MAX); // Scale to PWM range (0-255)
analogWrite(LED_POT_PIN, brightness);
// Debug output
Serial.print("Pot Value: ");
Serial.print(pot_value);
Serial.print(" | Brightness: ");
Serial.println(brightness);
delay(100); // Slow down serial output for readability
}