How to Stop a Firefly program?

clownbrother

New Member
Hello,

I was wondering if anyone could help me to stop this firefly program, after a set amount of time. The firefly program in question is listed in the forum under fireflies.

This is a great program but I was wondering if it can be stoped after a set amount of time?

The programmer uses some, ...for...next... operations to get random setting to light some LEDs'. Then the programmer uses a ...Return... to start the program over.

I tried to add a third ....for...next... to try and ..end.. the program but it would not work.

Is there anyway to ...end... a program out of the ....for...next... after a set amount of time?

The reason I would like to stop or end the program, is after the sun(or lights) are turned off the fireflies will run until the next morning or a light is turned on. This kind of sucks if they run all night so I would like to turn them off after a few hours, automatic off.
http://www.picaxeforum.co.uk/showthread.php?t=8781&highlight=firefly
Thanks
Clownbrother
 
Last edited:

manuka

Senior Member
I tried to add a third ....for...next... to try and ..end.. the program but it would not work.
Placement? This is normally a standard "timeout" approach,with further slow down loops often nested too. Please show your code !
 

clownbrother

New Member
Added code "Sorry"

Sorry, here is the code I was working on. I listed where I made add ons, is there a better way to do this that will work? I would like to stop the program after a few hours, give or take.

Ect.

led10:
b1= 4
b2= 2
b3= 1
b6= 2
gosub blink



goto start


blink: 'single blinking light

for b10 = 0 to 5 'I added this to try to stop the program

for b5 = 1 to b6
for b0 = 0 to b8 step 10

pwm b1, b0, 8
low b2
input b3
next b0

for b0 = b9 to 0 step -10

pwm b1, b0, 2
low b2
input b3
next b0
next b5 'added

next b10 'I also added this

if b10 = 5 then Done 'Also added this
pause 500

return

blink2: 'two lights at once

for b11 =0 to 5 'Added

for b5 = 1 to b6
for b0 = 0 to b8 step 10

pwm b1, b0, 2
low b2
low b3
next b0

for b0 = b9 to 0 step -10

pwm b1, b0, 16
low b2
low b3
next b0
next b5

next b11 'added
if b11 = 5 then Done 'added
pause 1500

return

Done: 'I added this
pause 500
end
 

hippy

Technical Support
Staff member
I think the problem is here, after 'next b11' ...

if b11 = 5 then Done 'added

Because the FOR loop is "b11 = 0 to 5", when the FOR-NEXT loop finishes and you reach that line 'b11' is above 5, and is actually 6.

Of course, you could just put an END there because that's what you're going to do as soon as the FOR-NEXT loop finishes anyway.
 

clownbrother

New Member
like this? LEDs' will not light. I ended on both.

blink: 'single blinking light

for b10 = 0 to 5 'I added this to try to stop the program

for b5 = 1 to b6
for b0 = 0 to b8 step 10

pwm b1, b0, 8
low b2
input b3
next b0

for b0 = b9 to 0 step -10

pwm b1, b0, 2
low b2
input b3
next b0
next b5 'added

next b10 'I also added this

pause 500

end

blink2: 'two lights at once

for b11 =0 to 5 'Added

for b5 = 1 to b6
for b0 = 0 to b8 step 10

pwm b1, b0, 2
low b2
low b3
next b0

for b0 = b9 to 0 step -10

pwm b1, b0, 16
low b2
low b3
next b0
next b5

next b11 'added

pause 500
end
 

Mycroft2152

Senior Member
Run your program in the simulator and watch the variables change (or not change).

You may have a problem wioth B9.

Myc
 
Fireflies with Timer

You can use a long-period cascading-counter timer to time out the fireflies.

I've attached a new version of the firefly basic program that will time out after some period of time. I've tested it in the simulator but not on hardware, so I'm not sure how long it will actually run.

Basically, I poke a value into a series of SFR registers, then after each call to the blink subroutine I decrement the first register. If it reaches zero, I decrement the next, and so on until all of the registers have been zeroed out.

You can set 47 registers ($50 to $7F), and set the initial value of each to 255 ($FF). I'm pretty sure you'll be able to set the delay that you need.

Here's the code to initialize the counters:

Code:
	Counter = TimerCount
	FOR index = FirstTimerValue TO LastTimerValue
		POKE index, Counter
	NEXT
and here's the code that decrements the counters to form the timer:

Code:
DecrementTimer:
	' Decrement counters and end program if they time out.
	
	FOR index = FirstTimerValue TO LastTimerValue
		PEEK index, Counter
		DEC Counter
		
		IF Counter != 0 THEN
			POKE index, Counter
			GOTO Main
		ELSE
			Counter = TimerCount
			POKE index, Counter
		END IF
	NEXT
Let me know how it works out for you.

Chuck
 

Attachments

Timing tests

I made a couple of tests with the cascading timer and here are some results.

With LastTimerValue set to $51, i.e. using two counters, and TimerCount set to $16, I was able to get a period of just under 1 second. When I changed LastTimerValue to $52, i.e. using three counters, I got a period of around 17 seconds.

By tweaking the TimerCount value and the number of counters, you should be able to get a timeout value that you like.

Remember when you're setting this up that the counters are in addition to the time that it takes to run through the "Blink" subroutine, which can vary quite a bit. Any timeout that you set using this cascading timer is going to approximate.

Here's the code that I tested with (I have an active low green LED on 2 and an active low red LED on 4 to signal when the test starts and stops):

Code:
' ---- [ Symbols ] -----------------
SYMBOL FirstTimerValue = $50
SYMBOL LastTimerValue = $52

SYMBOL TimerCount = $16

SYMBOL Counter   = B5

SYMBOL index = B13

' ---- [ Initialization ] -----------------

PowerOnReset:
	
	Counter = TimerCount
	FOR index = FirstTimerValue TO LastTimerValue
		POKE index, Counter
	NEXT
	
	LOW  2

Main:

	'DEBUG

	' Decrement counters and end program if they time out.
	
	FOR index = FirstTimerValue TO LastTimerValue
		PEEK index, Counter
		DEC Counter
		
		IF Counter != 0 THEN
			POKE index, Counter
			GOTO Main
		ELSE
			Counter = TimerCount
			POKE index, Counter
		END IF
	NEXT
	
	HIGH 2
	LOW  4
	
	END
 
Top