Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Modified May 18, 2017. [email protected], v1.0 firefly blinking green LEDs
- For Digispark ATTiny85 and clones.
- Fade in and out LEDs for random on and off times. This uses PWM and analogwrite().
- Attach 120ohm resistor to ground/GND. Put positive leg of LEDs on different holes
- like P0 as labeled on the board.
- */
- /* pin names and C code values
- * onboard LED=1
- * p0=0 (PWM)
- * p1=1 (PWM)
- * p2=2
- * p3=3
- * p4=4 (PWM)
- * p5=do not use
- */
- // Do includes here
- //=================================================================
- //=================================================================
- //=================================================================
- // Begin global vars.
- //int p0=1; //onboard LED
- byte p0=0; //Pin labeled p0 on actual board.
- byte LEDONBOARD=1;
- byte p1=1; //Same as LEDONBOARD.
- byte p2=2;
- byte p3=3;
- byte p4=4;
- byte mypauseup=30; //milliseconds between steps to light up an LED. Increase to make LED light up slower.
- byte mypausedn=mypauseup/2; //ms between steps to dim an LED.
- byte randon; //random number to light LED in milliseconds.
- byte randoff; //random ms to turn off led.
- byte lightlevel; //light level when LED on. 10-255
- byte randpos=0;
- const byte NUMPINS=3;
- byte myPins[NUMPINS] = {p0, p1, p4};
- byte usepin;
- int randwait;
- //Declare subs here.
- //=================================================================
- void allpinsoff()
- {
- byte k;
- for (k=0; k<NUMPINS; k++)
- {
- analogWrite(myPins[k],0); // Turn off pin.
- }
- return;
- }
- // the setup function runs once when you press reset or power the board
- void setup() {
- // initialize 4 pins as an output.
- byte k=0;
- for (k=0; k<NUMPINS; k++)
- {
- pinMode(myPins[k], OUTPUT);
- analogWrite(myPins[k], 0); //Turn off LED.
- }
- allpinsoff();
- }
- // the loop function runs over and over again forever
- void loop() {
- randon=random(500,800); //Time to leave led on. Range 50-1000ms.
- //randoff=random(500,1000);
- lightlevel=random(200,255); //Light intensity of LED.
- randpos=random(0,(NUMPINS-1));
- usepin=myPins[randpos];
- fadein(usepin,lightlevel,mypauseup);
- delay(randon); //Keep light on for randon ms.
- fadeout(usepin,lightlevel,mypausedn); //start at lightlevel and go to zero.
- allpinsoff;
- //delay(randoff);
- //Wait random seconds while led is off.
- randwait=random(1000,5000); //wait 2-5 seconds between blinks.
- delay(randwait); //Wait in ms.
- }
- //=================================================================
- //Simple Fade LED in from 0 to valin.
- /*Parms:
- * LED: led number on board.
- * valin: PWM value to light LED, 0-255.
- */
- void fadein(byte LED, byte valin, byte pauseup)
- {
- int i;
- for (i=0; i<valin; i++)
- {
- analogWrite(LED,i);
- delay(pauseup); //pause between steps when turning up LED.
- }
- }
- //=================================================================
- /*
- * Fade given LED from oldval to zero.
- */
- void fadeout(byte LED, byte oldval, byte pausedn)
- {
- int i;
- for (i=oldval; i>0; i--)
- {
- analogWrite(LED,i);
- delay(pausedn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment