November 23, 2024, 06:52:05 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


FireFlies?

Started by Specter, March 21, 2009, 06:29:37 PM

Previous topic - Next topic

Specter

Hi Jon-
A friend of mine shared some code you wrote to do some LED fireflies... I LOVE the program- my only thing is I want to make the 'max on time' a little less. I've been messing with the code, but can't figure out how to change it. Can you tell me which line in this to change so that it will shorten the max on time of the LEDs?

' =========================================================================
'
' File...... Firefly2.BS1
' Purpose...
' Author.... Jon Williams, EFX-TEK
' E-mail.... jwilliams@efx-tek.com
' Started...
' Updated...
'
' {$STAMP BS1}
' {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ Variables ]-------------------------------------------------------

SYMBOL idx = B2 ' loop controller
SYMBOL theBug = B3 ' pin to light
SYMBOL last = B4 ' last pin lit
SYMBOL stpSize = B5 ' to control PWM speed
SYMBOL level = B6 ' brightness level
SYMBOL delay = W4
SYMBOL lottery = W5 ' random value


' -----[ Program Code ]----------------------------------------------------

Main:
FOR idx = 1 TO 3
RANDOM lottery ' stir random number
NEXT

theBug = lottery // 6 ' select pin, 0 to 5
IF theBug = last THEN Main ' don't repeat
last = theBug ' save for next cycle

Bug_On:
RANDOM lottery
stpSize = lottery // 8 + 1 ' randomize loop speed
FOR level = 0 TO 255 STEP stpSize ' brighten
PWM theBug, level, 1
NEXT
HIGH theBug

On_Time:
RANDOM lottery
delay = lottery // 151 + 50 ' 150 to 300 ms
PAUSE delay

Bug_Off:
RANDOM lottery
stpSize = lottery // stpSize + 1 ' off no slower than on
FOR level = 255 TO 0 STEP -stpSize
PWM theBug, level, 1
NEXT
LOW theBug

Off_Time:
RANDOM lottery
delay = lottery // 250 + 500
PAUSE delay
GOTO Main

' -------------------------------------------------------------------------

BigRez

There are possibly three parts to look at: the part where it brightens (Bug_on), the part where it's held on (On_time), and the part that dims it to off (Bug_Off).

Looks like the line of code in the On_Time section is responsible for the random amount of ON time:

delay = lottery // 151 + 50 ' 150 to 300 ms

I believe the comment above is wrong though, because if I follow the logic, delay (the amount of time the bug is left on) is set to 50 + the remainder of a random number (0-65535) divided by 151. The modulus operation will return a value of 0 - 150. So add 50 to that range and you get 50 - 200 ms.

If you want to shorten the on time, you can reduce both of the numbers. The minimum on time is the second number and the max is the two added together (minus one.)  So, if you only wanted it to be on for 25 - 125 ms, you could use

delay = lottery // 101 + 25     '  min 25 to max 125 (25 + 100) ms

If you want to speed up the brightening time, follow the same method by increasing either or both the 8 + 3 portion in the Bug_On section. (Thus increasing the step size.)

hth,

mike

JonnyMac

This line:

  delay = lottery // 151 + 50

creates the delay period.  Let's say you want to go from 25 to 100 milliseconds -- the line changes to:

  delay = lottery // 76 + 25

In the "Programming Techniques" section I have a discussion on using the modulus operator to do this stuff.
Jon McPhalen
EFX-TEK Hollywood Office

Specter

ok, that's what I thought.  I tried changing this, but didn't see that big of a change. Maybe I didn't lower the #s enough...
Thanks!