A quick question

I know i should post this in the techinical but i need an answer now. Here is my question

We are trying to extend and retract a cylinder with a button push. We’ve already made it work well with two different buttons, but now we’re just trying to make the joystick more user-friendly. We have all programmed in C++ and Turing languages, and have tried to incorporate that type of logic in our programs, but have failed miserably. For example, logically one would think that “p1_sw_top = 1” means that the button is pressed, but we have found that it is actually “p1_sw_top = 0”. Does Basic not allow If statements inside other If statements?

This is part of the program, and we want to know what part doesn’t work. We have declared count1 and assigned a value of 1 to it at the beginning of the program, outside the main loop.

if p1_sw_top = 0 then turn:
if count1=1 then left:
relay1_fwd = 0
relay1_rev = 1
count1 = 2
left:
if count1=2 then right:
relay1_fwd = 1
relay1_rev = 0
count1 = 1
right:
turn:

Another example program that we have tried is :

'if ((p1_sw_top = 0) &~ (count1=2))then turn:
’ relay1_fwd = 1
’ relay1_rev = 0
’ count1 = 2
'turn:

'if (p1_sw_top = 0) & (count1 = 2) then turn1:
’ relay1_fwd = 1
’ relay1_rev = 0
’ count1 = 1
'turn1:

For some reason, the controller doesn’t wait for a button to be pressed, it just sees “count1 = 1,” and extends.

Any help and/or suggestions would be greatly appreciated.

if p1_sw_top = 0 then turn:
if count1=1 then left:
relay1_fwd = 0
relay1_rev = 1
count1 = 2
left:
if count1=2 then right:
relay1_fwd = 1
relay1_rev = 0
count1 = 1
right:
turn:

Ummm, looking at it, I can’t see any problems, except for the 2. The values have to be 1 or 0 or it doesn’t work. If your adding or subtracting its a different story of course.

What is changing the value of count1?

Athena

*Originally posted by Joelster *
**if p1_sw_top = 0 then turn:
**

Try using the debug feature of PBASIC(see the manual) to output the values of count1 and p1_sw_top to see what is going on…

OK you said that “p1_sw_top=0” occurs when the switch is pressed
If that is the case then the first program skips to “turn:” when the button is drepressed and doesn’t execute the commands.
(Did you intend “p1_sw_top=1” as in the switch is NOT being pressed?) You said it executes the following commands without a button being pressed… well that’s what’s you’ve got it programmed to do.

SO if you’re NOT pressing the top button, and count1 =1 then the program skips to “left:” , fails the “count1=2” test and executes the “relay1_fwd = 1” and " relay1_rev = 0 " commands…reaffirms “count1 = 1 " (which it already is) and falls through the “right:” and “turn:” labels and continues to the rest of the program.
if no button is pushed and count1 is not changed externally, it repeats the sbove sequence.
If you’re NOT pressing the top button, and count1=2 then the program fails the “count1=1” test and continues and sets “relay1_fwd = 0” and relay1_rev = 1” reaffirms count1=2 (which it already does) drops through “left:” taed the branch from “if count1 = 2 then right:” goes to “right:” drops through “turn:” and continues on throught the rest of the program.

In the second program… if you’re NOT pressing a button, both conditional statements "'if ((p1_sw_top = 0) &~ (count1=2))then turn: " and "if (p1_sw_top = 0) & (count1 = 2) then turn1: " FAIL to branch (because p1_sw_top = 0 is false or “0” unless you push the button) and the program executes all the statements … well you get the idea.

Anyone read this differently? I get confused easily.

Best wishes

Steve Alaniz

“What good is technology if you can’t abuse it?” - Ted Forth

Your code looks about right. You may have a hardware problem.

Are you using a joystick or a switch wired in to the p1_sw_top pin on port 1? Using the joystick, you should get 0 when the button is not depressed and 1 when it is. It’s easy enough to throw in a debug to verify that.

If you are using a switch wired in to the port make sure that you are wired to the normally open side of the switch. If the switch has three tabs on it, usually one will have NO and another will have NC next to them. NO is normally open, NC is normally closed. The third tab wil go to ground.

Once again, debug statements are the way to go. I always check out the code operation without any PWMs or relays plugged in to the Robot Controller and use debug statements to verify operation. This tip comes from a bitter experience as the team rookie coder when I loaded up my first ever piece of code and promptly destroyed our lifting mechanism. Ouch, that hurt.

I hope this helps.

if count1= 1 | p1_sw_top = 0 then do:
relay7_fwd = 1
goto loop:
do:

if count1= 0 | p1_sw_top = 0 then do2:
relay7_fwd = 0
goto loop:
do2:

if p1_sw_top = 0 then s:
loop:
if p1_sw_top = 1 then j:
goto red:
j:
goto loop:
red:
if count1 = 1 then yo:
count1=1
yo:
if count1 = 0 then yo2:
count1 = 0
yo2:
s:

This is our other flaw…think you can make it work??? please reply asap!!

I only did a quick read but I think I see two problems:

                     loop: 
                     if p1_sw_top = 1 then j: 
                     goto red: 
                     j: 
                     goto loop: 

If p1_sw_top = 1 is true, then you have the program stuck in an infinite loop until you change p1_sw_top = 1 to false. That will probably shut down your robot since you exceed the time limit on the watchdog timer… (delta T)

                     if count1 = 1 then yo: 
                     count1=1 
                     yo: 
                     if count1 = 0 then yo2: 
                     count1 = 0 
                     yo2: 
                     s: 

if count1 = 0 you then change count1 to 1 go past the yo: label, test if count1 = 0 which it never will (since you just changed it to 1) and then change it with count1 = 0 so you’re right back with count1 = 0 just like you started. I think you intended to have an unconditional “goto s:” between “count1 = 1” and “yo:”

Good luck… I’m off to N.O. for Mardi Gras!

Steve Alaniz

" What good is technology if you can’t abuse it?" - Ted Forth