Arduino - Knop LED Schakelen

In een vorige tutorial hebben we geleerd hoe we de LED kunnen inschakelen wanneer de knop wordt ingedrukt en de LED uitzetten als de knop wordt losgelaten. In deze tutorial gaan we leren hoe u de LED kunt schakelen (toggle) telkens wanneer de knop wordt ingedrukt.

De tutorial bestaat uit twee hoofdonderdelen:

Over LED en Knop

Als u nog niet bekend bent met LED en knop (pinout, hoe het werkt, hoe te programmeren ...), leer hier meer over in de volgende tutorials:

Bedradingsschema

Arduino Knop LED Bedradingsschema

This image is created using Fritzing. Click to enlarge image

Arduino Code - Knop schakelt LED zonder Debouncing

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-button-toggle-led */ // constants won't change const int BUTTON_PIN = 7; // Arduino pin connected to button's pin const int LED_PIN = 3; // Arduino pin connected to LED's pin // variables will change: int ledState = LOW; // the current state of LED int lastButtonState; // the previous state of button int currentButtonState; // the current state of button void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode currentButtonState = digitalRead(BUTTON_PIN); } void loop() { lastButtonState = currentButtonState; // save the last state currentButtonState = digitalRead(BUTTON_PIN); // read new state if(lastButtonState == HIGH && currentButtonState == LOW) { Serial.println("The button is pressed"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(LED_PIN, ledState); } }

Snelle Stappen

  • Verbind Arduino met uw pc via USB-kabel
  • Open de Arduino IDE, selecteer de juiste board en poort
  • Kopieer de bovenstaande code en open deze in Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de Arduino te uploaden
  • Houd de knop meerdere seconden ingedrukt en laat dan los
  • Zie de verandering van de toestand van de LED

Code Uitleg

Lees de regel-voor-regel uitleg in de commentaarregels van de code!

In de code is ledState = !ledState gelijk aan de volgende code:

if(ledState == LOW) ledState = HIGH; else ledState = LOW;

※ Notiz:

In de praktijk werkt bovenstaande code soms niet correct. Om het altijd correct te laten werken, moeten we debouncing toepassen op de knop. Debouncing voor de knop is niet makkelijk voor beginners. Gelukkig kunnen we dankzij de ezButton bibliotheek dit eenvoudig doen.

Arduino Code - Knop schakelt LED met Debouncing

Waarom is debouncing nodig? ⇒ zie Arduino - Knop Debounce tutorial

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-button-toggle-led */ #include <ezButton.h> /// constants won't change const int BUTTON_PIN = 7; // the number of the pushbutton pin const int LED_PIN = 3; // the number of the LED pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7; // variables will change: int ledState = LOW; // the current state of LED void setup() { Serial.begin(9600); // initialize serial pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggleed sate digitalWrite(LED_PIN, ledState); } }

Snelle Stappen

  • Installeer de ezButton bibliotheek. Zie Hoe Te Doen
  • Kopieer de bovenstaande code en open deze in Arduino IDE
  • Klik op de Upload knop in Arduino IDE om de code naar de Arduino te uploaden
  • Druk meerdere keren op de knop
  • Zie de verandering van de toestand van de LED

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.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten in de implementatie en helpt u stap voor stap.

※ 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!