Hi,
Does anyone have an example of how this "spaghetti code" can be converted to a more "classic" style of program and maintain the same logic as oracacle's code in #9 above? Jims
As hippy says it's not really a good (or should it be bad) example of spaghetti code. However, there may be some "spaghetti thinking" because these two statements come from the first 5 lines of the OP:
"
turn on a light connected to b.5
for the time duration of the loop."
"the light connected to b.5
blinks instead of staying lit."
The real issue is that there are absolutely no comments or symbolic names, so it's difficult to determine what the code is doing, or what the Programmer
thinks it is doing. There are 3 inputs and 7 outputs, but all we know (from the OP text) is that there are two "lights", which might be anything from a LED to a 1000 watt relay-controlled flloodlight. Is this the full program (as there are no init: or main: labels)? Are the inputs and outputs "Active High" or "Active Low" and might more than one input become active at a time (if so does it matter what happens)?
The actual code structure also makes one "suspicious" that it might not accurately represent what was intended. There is no need to set b0 = 0 immediately before it's set in a FOR loop, the green/blue loops are executed
51 times, and why are the High / Low commands repeated with every pass through the FOR loop? However, the fall-through from greenflash into blueflash perhaps
is intended, as there is a goto from the end of blueflash back to greenflash.
Therefeore, I had to make a number of wild assumptions to derive this allternative code. It might not be "correct", but is shorter and hopefully clearer in its functioning. Note the use of constants (in place of "magic numbers") allows the delay times, and even the Active High / Low inputs, to be changed without delving deep into the code (although the PICaxe Basic "switch" command does assume Active High).
Code:
#picaxe 14m2
#no_data ; Avoid unnecessary downloading time
symbol GBDELAY = 10 ; Green/Blue alternation period (1 unit = 50ms)
symbol WHITEDELAY = 20000 ; White pulse time (1 unit = 10 us)
symbol FIRSTPIN = b.0 ; First "white" pin
symbol LASTPIN = b.4 ; Last "white" pin
symbol GREENLED = b.5 ; Pin Active High
symbol BLUELED = c.4 ; Pin Active High
symbol outpin = b0
symbol whitebutton = pinc.0
symbol greenbutton = pinc.1
symbol bluebutton = pinc.2
symbol PUSHED = 1 ; Buttons Active High
whiteflash:
switchoff GREENLED,BLUELED
main:
for outpin = FIRSTPIN to LASTPIN
pulsout outpin,WHITEDELAY
if greenbutton = PUSHED then greenflash
if bluebutton = PUSHED then blueflash
next outpin
goto main ; Repeat the white sequence
greenflash:
switchoff BLUELED
switchon GREENLED
for b0 = 0 to 50
pause GBDELAY
if whitebutton = PUSHED then whiteflash
if bluebutton = PUSHED then blueflash
next b0 ; Then fall through into blueflash
blueflash:
switchoff GREENLED
switchon BLUELED
for b0 = 0 to 50
pause GBDELAY
if whitebutton = PUSHED then whiteflash
if greenbutton = PUSHED then greenflash
next b0
goto greenflash
Cheers, Alan.