#define LED_PIN 3 // LED connected to GPIO 3
#define BUTTON_PIN 2 // Button connected to GPIO 2
#define DELAY_MIN 200 // Minimum delay in milliseconds
#define DELAY_MAX 1000 // Maximum delay in milliseconds
int ledState = LOW; // Initial LED state
unsigned long previousMillis = 0; // Variable for time tracking
int delayTime = DELAY_MIN; // Initial delay time
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
// If the button is pressed, change the delay time
if (digitalRead(BUTTON_PIN) == LOW) {
delayTime = DELAY_MAX; // Set delay to maximum when button is pressed
} else {
delayTime = DELAY_MIN; // Set delay to minimum when button is not pressed
}
// Blink the LED at the set delay time
if (currentMillis - previousMillis >= delayTime) {
previousMillis = currentMillis; // Save the current time
ledState = !ledState; // Toggle LED state
digitalWrite(LED_PIN, ledState); // Set LED state
}
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1