Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Joystick Button Press (http://www.chiefdelphi.com/forums/showthread.php?t=75558)

dboisvert 06-03-2009 20:07

Joystick Button Press
 
I am trying to accomplish a simple button task. When someone pushes a button I want it to keep doing the action (IE In this case "Relay Forward" & "Relay Reverse") until the microswitch is activated. Right now it stops as soon as you release the button.


What the code looks like right now is this
Code:

if button1 == 1 & microswitch1 = 0
  Relay Forward
else if button2 == 1 & microswitch2 = 0
  Relay Reverse
else
  Relay Off

Any suggestions?

byteit101 06-03-2009 21:16

Re: Joystick Button Press
 
Quote:

Originally Posted by dboisvert (Post 832620)
I am trying to accomplish a simple button task. When someone pushes a button I want it to keep doing the action (IE In this case "Relay Forward" & "Relay Reverse") until the microswitch is activated. Right now it stops as soon as you release the button.


What the code looks like right now is this
Code:

if button1 == 1 & microswitch1 = 0
  Relay Forward
else if button2 == 1 & microswitch2 = 0
  Relay Reverse
else
  Relay Off

Any suggestions?

int dir=0;//at the top
...
loop//your while loop
..
if (button1 ==1 && !(microswitch1==1))//if button1 pressed and microsw ! pressed
dir=1;
else if (button2==1 && !(microswitch2==1))//if button2 pressed and microsw ! pressed
dir=-1;
else if (microswitch1 ==1 || microswitch2 ==1)//if microsw pressed
dir=0;
//otherwise do nothing to the value
relay.Set((float)dir);//set the new (or old )value
...
end loop
...

The Lucas 06-03-2009 21:21

Re: Joystick Button Press
 
Quote:

Originally Posted by dboisvert (Post 832620)
I am trying to accomplish a simple button task. When someone pushes a button I want it to keep doing the action (IE In this case "Relay Forward" & "Relay Reverse") until the microswitch is activated. Right now it stops as soon as you release the button.


What the code looks like right now is this
Code:

if button1 == 1 & microswitch1 = 0
  Relay Forward
else if button2 == 1 & microswitch2 = 0
  Relay Reverse
else
  Relay Off

Any suggestions?

Try this:
Code:

if (button1 == 1 && microswitch1 == 0)
  Relay Forward
else if (button2 == 1 && microswitch2 == 0)
  Relay Reverse
else (0 == microswitch1 || 0 == microswitch2)
  Relay Off

= is the assignment operator and "microswitch = 0" sets microswitch to the value of 0 is always is true. You need to use == which is conditional equals

It is a best practice to write the statement 0 == microswitch so the compiler can catch the mistake (0=microswitch is an error)

Also it is better to use a logial and (&&) rather than a bitwise and (&) since the bitwise and will not work in all situations (2 & 1 =false)

dboisvert 06-03-2009 22:20

Re: Joystick Button Press
 
Im aware of the proper syntax but I probably should have said it was more of a "Pseudo-Code"

dboisvert 06-03-2009 23:30

Re: Joystick Button Press
 
Code:

flipperdoor->Set(Relay::flipperset);
I get an error that says "flipperset is not a member of Relay"

The Lucas 07-03-2009 04:15

Re: Joystick Button Press
 
Quote:

Originally Posted by dboisvert (Post 832682)
Code:

flipperdoor->Set(Relay::flipperset);
I get an error that says "flipperset is not a member of Relay"

You have to use

Code:

flipperdoor->Set(Relay::kForward);
OR
Code:

flipperdoor->Set(Relay::kReverse);
OR
Code:

flipperdoor->Set(Relay::kOff);

dboisvert 07-03-2009 11:09

Re: Joystick Button Press
 
What I believe I am trying to accomplish is setting it outside of the loop. So basically store a variable and set it to that variable later.

Initial Decleration of flipperset (unsure if that will work)

Code:

Relay::Value flipperset;
sets flipperset to shut the relay off
Code:

                flipperset = Relay::kOff;
If statements to change the flipperset variable
Code:

                        if (m_leftStick->GetRawButton(5) == 1 && flipperdoorright->Get() == 0 ) {
                                flipperset = Relay::kForward;
                        } else if (m_leftStick->GetRawButton(4) == 1 && flipperdoorleft->Get() == 0 ){
                                flipperset = Relay::kReverse;
                        } else if (flipperdoorleft->Get() == 1 || flipperdoorright->Get() == 1) {
                                flipperset = Relay::kOff;
                        }

Final set of flipperdoor to a certain variable
Code:

flipperdoor->Set(flipperset);


All times are GMT -5. The time now is 13:55.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi