rewriting code
icepuck
Posts: 466
I was messing around with my old BS1 revD and some old parts and ended up with a circuit to flash a light bulb.
The code isn't pretty but I like the way it works.
Is there a better way to do same with less code?
-dan
Main:
PWM 0, 5, 20 'starts really dim
PWM 0, 10, 20
PWM 0, 20, 20
PWM 0, 30, 20
PWM 0, 40, 20
PWM 0, 50, 20
PWM 0, 60, 20
PWM 0, 70, 20
PWM 0, 80, 20
PWM 0, 90, 20
PWM 0, 100, 20
PWM 0, 110, 20 'really bright
PWM 0, 100, 20
PWM 0, 90, 20
PWM 0, 80, 20
PWM 0, 70, 20
PWM 0, 60, 20
PWM 0, 50, 20
PWM 0, 40, 20
PWM 0, 30, 20
PWM 0, 20, 20
PWM 0, 10, 20
PWM 0, 5, 20 'dim
GOTO Main
END
The code isn't pretty but I like the way it works.
Is there a better way to do same with less code?
-dan
Main:
PWM 0, 5, 20 'starts really dim
PWM 0, 10, 20
PWM 0, 20, 20
PWM 0, 30, 20
PWM 0, 40, 20
PWM 0, 50, 20
PWM 0, 60, 20
PWM 0, 70, 20
PWM 0, 80, 20
PWM 0, 90, 20
PWM 0, 100, 20
PWM 0, 110, 20 'really bright
PWM 0, 100, 20
PWM 0, 90, 20
PWM 0, 80, 20
PWM 0, 70, 20
PWM 0, 60, 20
PWM 0, 50, 20
PWM 0, 40, 20
PWM 0, 30, 20
PWM 0, 20, 20
PWM 0, 10, 20
PWM 0, 5, 20 'dim
GOTO Main
END
bs1
612B
Comments
reps VAR byte' counter for the FOR/NEXT loop
pw pin 0
FOR reps = 10 TO 100 step 10 ' repeat with for the full value in steps of 10
PWM pw, reps, 20
NEXT
pause 100
FOR reps = 100 TO 10 step -10 ' repeat with for the full value in steps down
PWM pw, reps, 20
NEXT
Or for someone else to be able to look at your code and be able to understand what it is doing.
A few comments added here and there is a big help, but also it may make it easier to understand if it is longer - goes step by step - rather than shorter.
So sometimes I will intentionally make something longer than necessary just because it is easier to look at it and understand what is going on.
However if there is not enough memory available, then that might not be possible. Everything might need to be squished into the shortest code possible. But you can always add comments to explain what is going on, then many years later read that and be able to understand what is what.
Comments do not take up memory space.
At the moment this is just a learning experience for me for something to do with my time since I'm unemployed again but I see want you mean, comments must get striped out before going to the stamp because the memory map on the stamp doesn't change.
-dan
This is what I ended up with on the bs1
' {$STAMP BS1}
' {$PBASIC 1.0}
SYMBOL reps =B0 ' counter for the FOR/NEXT loop
SYMBOL pw = PIN0
LET PINS = 0 'set pins to a low state
Main:
FOR reps = 10 TO 100 STEP 10 ' repeat with for the full value in steps of 10
PWM pw, reps, 20
NEXT
FOR reps = 100 TO 10 STEP -10 ' repeat with for the full value in steps down
PWM pw, reps, 20
NEXT
GOTO Main
There is no difference how this runs vs my first attempt but when looking at the memory map the new prog uses half the space which is interesting...
Now I know what to do when space gets tight.
-dan