Hi Joel,
<shamelessplug>
If you were using my BASIC Stamp Preprocessor (available in the wite papers section with additional information elsewhere in this Technical Discussion forum), you could rewrite your code thus to make it more understandable:
Code:
{$if logged = 0}
{$if count1 = 0}
{$if p1_sw_top = 1}
relay1_fwd = 1
relay1_rev = 0
count1 = 1
{$endif}
{$else} ' count1 <> 0
{$if p1_sw_top = 1}
relay1_rev = 1
relay1_fwd = 0
count1 = 0
{$endif}
{$endif}
{$elseif}
logged = p1_sw_top
</shamelessplug>
Now that I did that, and examined the code further, I'm not sure this code is going to do exactly what you want. Let me analyze what this code does:
Code:
If the joystick thumb button was NOT pressed the last time we
came through the loop, then (depending upon whether count1 is
set or clear) either:
- Check the status of the thumb button, and if it's currently
being pushed, turn on relay1 forward, and SET count1 for the
next time through the loop.
OR
- Check the status of the thumb button, and if it's currently
being pushed, reverse relay1, and CLEAR count1 for the next
time through the loop.
Finally, save the current thumb button state for the next time
through the loop.
Do you really only want the arm to go forward or reverse? Do you sometimes want to stop it? (Maybe you have limit switches, and you always want your arm to travel the full distance between stops.) Or maybe you would like to make the button a three state toggle thusly:
Code:
{$if prevButtonState = 0}
' The button was up the last time through the loop ...
{$if p1_sw_top = 1}
' ... but it's now being pressed, so do something:
{$if relayState = 0}
relayState = 1 ' Relay on forward
relay1_fwd = 1
relay1_rev = 0
{$elseif relayState = 1}
relayState = 2 ' Relay off. Next time reverse
relay1_fwd = 0
relay1_rev = 0
{$elseif relayState = 2}
relayState = 3 ' Relay reversed
relay1_fwd = 0
relay1_rev = 1
{$elseif relayState = 3}
relayState = 0 ' Relay off. Next time forward
relay1_fwd = 0
relay1_rev = 0
{$endif}
{$endif}
{$elseif}
prevButtonState = p1_sw_top
Notice: I changed your variable names to make their functions more clear. I also rearranged some of the code. For example, I check the current button state before checking the relay state so I only have to do it once. I also always set relay1_fwd before relay1_rev to make reading your code less error prone. (When I first read your code, I thought you were always turning the relay on FORWARD, and never reversing it.)