ESP32 - LED - Knipperen zonder delay

Een van de eerste programma's die beginners uitvoeren is het laten knipperen van een LED. De eenvoudigste manier om een LED te laten knipperen is met behulp van de delay() functie. Deze functie blokkeert de ESP32 echter bij het uitvoeren van andere taken. Dit is prima als u slechts één enkele LED wilt laten knipperen. Maar als u meerdere LED's wilt knipperen of parallel andere taken wilt uitvoeren, kunt u de delay() functie niet gebruiken. We hebben een andere oplossing nodig. In deze handleiding leert u hoe u meerdere taken kunt uitvoeren zonder de delay-functie te gebruiken. Meer specifiek leert u hoe u een LED laat knipperen en tegelijkertijd de status van een knop controleert.

We doorlopen hieronder drie voorbeelden en vergelijken de verschillen:

Deze methode kan worden toegepast om de ESP32 meerdere taken tegelijk te laten uitvoeren. Het knipperen van een LED is slechts een voorbeeld.

Over LED en Knop

We hebben specifieke handleidingen over LED en knop. Elke handleiding bevat uitgebreide informatie en stapsgewijze instructies over hardware pinout, werking, bekabeling naar ESP32, ESP32-code... Leer er meer over via onderstaande links:

Bedradingsschema

ESP32 LED knop Bedradingsschema

This image is created using Fritzing. Click to enlarge image

Als u niet weet hoe u ESP32 en andere componenten van stroom moet voorzien, vindt u instructies in de volgende tutorial: Hoe ESP32 van stroom te voorzien.

Laten we de ESP32-code vergelijken die een LED laat knipperen met en zonder gebruik van de delay() functie

ESP32 Code - Met Delay

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-led-blink-without-delay */ #define LED_PIN 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

Snelle Stappen

  • Als u voor het eerst ESP32 gebruikt, zie dan hoe u de omgeving instelt voor ESP32 op Arduino IDE.
  • Maak de bedrading zoals op bovenstaande afbeelding.
  • Verbind de ESP32 module met uw computer via een micro USB-kabel.
  • Open Arduino IDE op uw computer.
  • Selecteer de juiste ESP32 board (bijv. ESP32 Dev Module) en COM-poort.
  • Kopieer de bovenstaande code en plak deze in Arduino IDE.
  • Compileer en upload de code naar het ESP32 board door te klikken op de Upload knop in Arduino IDE
Hoe ESP32 code uploaden in Arduino IDE
  • Open de Serial Monitor in Arduino IDE
Hoe Serial Monitor openen in Arduino IDE
  • Druk 4 keer op de knop
  • Kijk naar de LED: de LED schakelt elke seconde periodiek tussen AAN en UIT
  • Bekijk de output in Serial Monitor
COM6
Send
1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • In Serial Monitor zult u niet vier keer zien dat de status naar 0 verandert (4 drukken). Dat komt omdat tijdens de delay-tijd de ESP32 GEEN veranderingen kan detecteren.

ESP32 Code - Zonder Delay

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-led-blink-without-delay */ #define LED_PIN 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis = 0; // will store last time LED was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // check to see if it's time to blink the LED; that is, if the difference // between the current time and last time you blinked the LED is bigger than // the interval at which you want to blink the LED. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); // save the last time you blinked the LED previousMillis = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

Snelle Stappen

  • Als u voor het eerst ESP32 gebruikt, zie dan hoe u de omgeving instelt voor ESP32 op Arduino IDE.
  • Voer bovenstaande code uit en druk 4 keer op de knop
  • Kijk naar de LED: de LED schakelt elke seconde periodiek tussen AAN en UIT
  • Bekijk de output in Serial Monitor
COM6
Send
1 0 1 0 1 0 1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Alle knopdruk gebeurtenissen werden gedetecteerd.

Regel-voor-regel Code Uitleg

De bovenstaande ESP32 code bevat een uitleg regel-voor-regel. Lees de opmerkingen in de code alstublieft!

Meer Taken Toevoegen

De onderstaande code laat twee LED's knipperen met verschillende intervallen en controleert de status van de knop.

ESP32 LED twee knop Bedradingsschema

This image is created using Fritzing. Click to enlarge image

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-led-blink-without-delay */ #define LED_PIN_1 22 // ESP32 pin GPIO22 connected to LED #define LED_PIN_2 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL_1 1000 // interval at which to blink LED 1 (milliseconds) #define BLINK_INTERVAL_2 500 // interval at which to blink LED 2 (milliseconds) // Variables will change: int ledState_1 = LOW; // ledState used to set the LED 1 int ledState_2 = LOW; // ledState used to set the LED 2 int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis_1 = 0; // will store last time LED 1 was updated unsigned long previousMillis_2 = 0; // will store last time LED 2 was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN_1, OUTPUT); pinMode(LED_PIN_2, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { unsigned long currentMillis = millis(); // check to see if it's time to blink the LED 1 if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: ledState_1 = (ledState_1 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_1, ledState_1); // save the last time you blinked the LED previousMillis_1 = currentMillis; } // check to see if it's time to blink the LED 2 if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: ledState_2 = (ledState_2 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_2, ledState_2); // save the last time you blinked the LED previousMillis_2 = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!