Advertisement
kartonman

random basic coding Randomly blinks one of 8 LEDs

Nov 3rd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. //random basic coding Randomly blinks one of 8 LEDs
  2. /*Code by Gary Granai steamtraininfo.com .
  3. Complete documentation and live demonstration at https://steamtraininfo.com/arduino-projects/random-blink-led
  4. Complete arduino for model railroaders library at https://steamtraininfo.com/arduino-projects
  5. You may freely use and change this sketch as long as all the information above is included unchanged.
  6. */
  7.  
  8.  
  9. void setup() {
  10. /* This sets 8 leds on pins D2 to D9 to output
  11. */
  12. for (int i = 2; i <= 9; i++) {
  13. pinMode(i, OUTPUT); // sets pins to output
  14. }
  15. randomSeed(analogRead(A0)); /*sets the pin to create "static so the the initial LED to light is different
  16. eacg time through the loop */
  17. }
  18.  
  19. //***************************************************
  20. void loop() {
  21. BlinkRandomly();
  22.  
  23. }
  24.  
  25. //**************************************************
  26. void BlinkRandomly() {
  27. int LightLED = random(2, 10); //randomly selects LED to light. Must use +1 over the last pin number
  28. //(ie: 9 + 1 = 10)
  29. int dlay = 250; // sets the blink rate in milliseconds
  30. digitalWrite(LightLED, HIGH);
  31. delay(dlay);
  32. digitalWrite(LightLED, LOW);
  33. delay(dlay);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement