bulrush

Firefly10

May 20th, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. /*
  2. Modified May 18, 2017. [email protected], v1.0 firefly blinking green LEDs
  3. For Digispark ATTiny85 and clones.
  4. Fade in and out LEDs for random on and off times. This uses PWM and analogwrite().
  5. Attach 120ohm resistor to ground/GND. Put positive leg of LEDs on different holes
  6. like P0 as labeled on the board.
  7. */
  8. /* pin names and C code values
  9. * onboard LED=1
  10. * p0=0 (PWM)
  11. * p1=1 (PWM)
  12. * p2=2
  13. * p3=3
  14. * p4=4 (PWM)
  15. * p5=do not use
  16. */
  17. // Do includes here
  18.  
  19.  
  20. //=================================================================
  21. //=================================================================
  22. //=================================================================
  23.  
  24. // Begin global vars.
  25. //int p0=1; //onboard LED
  26.  
  27. byte p0=0; //Pin labeled p0 on actual board.
  28. byte LEDONBOARD=1;
  29. byte p1=1; //Same as LEDONBOARD.
  30. byte p2=2;
  31. byte p3=3;
  32. byte p4=4;
  33. byte mypauseup=30; //milliseconds between steps to light up an LED. Increase to make LED light up slower.
  34. byte mypausedn=mypauseup/2; //ms between steps to dim an LED.
  35. byte randon; //random number to light LED in milliseconds.
  36. byte randoff; //random ms to turn off led.
  37. byte lightlevel; //light level when LED on. 10-255
  38. byte randpos=0;
  39. const byte NUMPINS=3;
  40. byte myPins[NUMPINS] = {p0, p1, p4};
  41. byte usepin;
  42. int randwait;
  43.  
  44. //Declare subs here.
  45. //=================================================================
  46. void allpinsoff()
  47. {
  48. byte k;
  49. for (k=0; k<NUMPINS; k++)
  50. {
  51. analogWrite(myPins[k],0); // Turn off pin.
  52. }
  53. return;
  54. }
  55. // the setup function runs once when you press reset or power the board
  56. void setup() {
  57. // initialize 4 pins as an output.
  58. byte k=0;
  59. for (k=0; k<NUMPINS; k++)
  60. {
  61. pinMode(myPins[k], OUTPUT);
  62. analogWrite(myPins[k], 0); //Turn off LED.
  63. }
  64. allpinsoff();
  65. }
  66.  
  67. // the loop function runs over and over again forever
  68. void loop() {
  69.  
  70. randon=random(500,800); //Time to leave led on. Range 50-1000ms.
  71. //randoff=random(500,1000);
  72. lightlevel=random(200,255); //Light intensity of LED.
  73.  
  74. randpos=random(0,(NUMPINS-1));
  75. usepin=myPins[randpos];
  76. fadein(usepin,lightlevel,mypauseup);
  77. delay(randon); //Keep light on for randon ms.
  78. fadeout(usepin,lightlevel,mypausedn); //start at lightlevel and go to zero.
  79. allpinsoff;
  80. //delay(randoff);
  81.  
  82. //Wait random seconds while led is off.
  83. randwait=random(1000,5000); //wait 2-5 seconds between blinks.
  84. delay(randwait); //Wait in ms.
  85.  
  86.  
  87. }
  88. //=================================================================
  89. //Simple Fade LED in from 0 to valin.
  90. /*Parms:
  91. * LED: led number on board.
  92. * valin: PWM value to light LED, 0-255.
  93. */
  94. void fadein(byte LED, byte valin, byte pauseup)
  95. {
  96. int i;
  97. for (i=0; i<valin; i++)
  98. {
  99. analogWrite(LED,i);
  100. delay(pauseup); //pause between steps when turning up LED.
  101. }
  102. }
  103. //=================================================================
  104. /*
  105. * Fade given LED from oldval to zero.
  106. */
  107. void fadeout(byte LED, byte oldval, byte pausedn)
  108. {
  109. int i;
  110. for (i=oldval; i>0; i--)
  111. {
  112. analogWrite(LED,i);
  113. delay(pausedn);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment