Stap 4: Blink LED in- en uitschakelen en gebruik Potentiometer de LED dimmen
Nu, hechten de potentiometer aan de LED zoals vóór, maar niet het verbinden met de seriële Monitor. Dezelfde code als alvorens te gebruiken.
const int ledPin = 8; // Tell Arduino that button is pin 2 const int buttonPin = 2; // Tell Arduino that LED is pin 8// Initialize button, LEDs, timer, etc...int ledState = LOW; int buttonState = 0; long previousMillis = 0; long interval = 1000; void setup() { pinMode(ledPin, OUTPUT); // Declare LED an output pinMode(buttonPin, INPUT); //Decalare button an input }void loop() { buttonState = digitalRead(buttonPin); // Read if button is on or off if(buttonState == HIGH) { // If button is on...// Timer: unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; // Decide whether LED should be on or off if (ledState == LOW) ledState = HIGH; else ledState = LOW; digitalWrite(ledPin, ledState); // Turn LED on or off } } else { // If button isn't on... digitalWrite(ledPin, LOW); // Turn LED off }}