Last active
April 26, 2018 15:27
-
-
Save BasOnTech/a5476405f6c6c5f4e8da0d87239b659d to your computer and use it in GitHub Desktop.
Drie om en om fadende LED's op Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Bas on Tech - Twee contra fadende LED's | |
* Deze les is onderdeel van de lessen op https://arduino-lessen.nl | |
* | |
* (c) Copyright 2018 - Bas van Dijk / Bas on Tech | |
* Deze code en inhoud van de lessen mag zonder schriftelijke toestemming | |
* niet voor commerciele doeleinden worden gebruikt | |
* | |
* YouTube: https://www.youtube.com/c/BasOnTechNL | |
* Facebook: https://www.facebook.com/BasOnTechChannel | |
* Instagram: https://www.instagram.com/BasOnTech | |
* Twitter: https://twitter.com/BasOnTech | |
* | |
*/ | |
int ledPins[] = {9, 10, 11}; // Geef aan welke PWM pins er gebruikt worden | |
int currentLedIndex = 0; // positie van huidige led pin in ledPins array | |
const int numberOfLeds = 3; // totaal aantal PWN pins in gebruik | |
const int fadeSpeed = 5; // fade snelheid | |
// Voer uit bij de start van programma | |
void setup() { | |
for(int i = 0; i < numberOfLeds; i++) { | |
pinMode(ledPins[i], OUTPUT); | |
} | |
} | |
// Herhaal oneindig | |
void loop() { | |
// Haal het huidige pin nummer op | |
int currentLedPin = ledPins[currentLedIndex]; | |
// Fade in | |
for(int ledVal = 0; ledVal <= 255; ledVal +=1) { | |
analogWrite(currentLedPin, ledVal); | |
delay(fadeSpeed); | |
} | |
// Fade uit | |
for(int ledVal = 255; ledVal >= 0; ledVal -=1) { | |
analogWrite(currentLedPin, ledVal); | |
delay(fadeSpeed); | |
} | |
// Zolang de huidige index kleiner is dan het totaal aantal leds | |
// -1 is nodig omdat het array begint met 0 ipv 1 | |
if (currentLedIndex < numberOfLeds - 1) { | |
currentLedIndex++; | |
} else { | |
currentLedIndex = 0; | |
} | |
// Pauzeer voor 1 seconde | |
// delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment