Hi,
"The Butterfly Effect" :
I wasn't going to waste any more time on this trivial program, but the recent modifications happen to have introduced some features that might be useful or of interest to others.
I often use them when writing my very "fast" program code for "Bit-Banging" serial data, such as for example, custom Infra Red or Radio Communications, etc., although the speed is not at all relevant to the Programs in this thread.
First, the three consecutive lines from post #30 :
if b1 <=160 then flsh1 : goto main : flsh1: are "inefficient" because they can be replaced by a single instruction:
if b1 > 160 then goto
main . Both versions give a "Jump Backwards" and a "Fall Through" (to the next line in the Program), depending on the state of the variable (b1). The latter is a very efficient instruction because the "Fall Through" (i.e. False condition path) is fast(er) and the (True) "Jump" (GOTO) path gives us the opportunity to "Close" a program loop, without the need for a separate (Unconditional) GOTO.
Programming Purists may argue that the inversion of the Condition (<= becoming >) is confusing, but they would probably agree that "One (or No) GOTOs are better than Two"
. Thus the complete program could be written as:
Code:
#Picaxe 08M2
#No_Data
setfreq m2 ; Allows PULSOUT to generate 1 second pulses
main1:
readadc C.1,b1
if b1 > 160 then GOTO main1 ; Fall Through when the Battery condition is low
pulsout C.0 , 50000 ; One second at 2 MHz clock
pause 2500 ; 5 seconds at 2 MHz clock
main2:
readadc C.1,b1
if b1 > 160 then GOTO main2
pulsout C.4 , 50000
pause 2500
goto main1
It hasn't been discussed whether the maximum 0.65 ms (at 4 MHz clock) PULSOUT would be sufficient to drive the relay in practice, but for this version I've used a SETFREQ to allow PULSOUT to generate 1 second pulses. However, the original
HIGH C.4 : PAUSE 1000 {; or WAIT 1}
: LOW C.4 still could be used, perhaps as a "One Liner".
The program in #24 used a variable named
Relay to "remember" the state of the external hardware, so that it "knew" which pulse to generate the next time. The programs in #30 and #33 achieve the same by dividing the program into two very similar parts, with the particular section being executed "remembering" which pulse is needed next. I have called this style a "Butterfly" program, because if the first main Loop were written on the Left Hand Page of a notebook, then the second Loop could be a copy with very few changes onto the Right Hand Page, with the two loops or pages being considered the "Wings" of a Butterfly. These Wings can effectively act as an "invisble" (zero execution time) "memory" for the state or progress of the Program.
Off Topic : My fast bit-banging programs often take the form of a "Butterfly", for example with the Wings handling the "(Modulated) Carrier" and "Gap" (e.g. 0/1) sections of the Program. Or sometimes even repeating a whole routine separately for Odd and Even phases (i.e. in the two Wings): This allows some time-consuming sections of the routine, such as any "Housekeeping", and the fundamental "Jump Backwards" (to close the loop), to be shared between the two phases. It may even allow bit-strings of more than 8 or 16 bits to be handled directly by single Byte or Word variables. Note that I haven't used the term SUBROUTINE anywhere, because they (or in particular the RETURN) are generally "Too Slow" Instructions to use with Bit-Banging.
Moving on to the program in post #32, note that it does not have the same "memory" embedded in the loops, it always executes both sections in sequence. Does this matter? Well Yes and No.
When the input (b1) crosses the threshold level, there is an even (i.e. 50%) chance of which pulse will be generated, depending on which instruction is currently being executed. If it happens to be the "Wrong" Pulse (i.e. the same as last time) then nothing will happen and 5 seconds (or whatever) later, it will generate the "Correct" Pulse. Of course if the input voltage does not return across the Threshold voltage within the 5 seconds, then all these versions of the program will produce alternating A and B pulses every 5 seconds.
Cheers, Alan.