Quote:
|
ok, let's just say, if you push button 2, that will turn the motor on relay 1 on forward direction. when they push it again, it will turn it off.
|
This is a "push-on, push-off" or "toggle" function. Our team uses LabVIEW, so I can't be certain I'm giving you working code, but the structure should be correct.
Code:
// Define and initialize a couple of variables
static char current_state = 0; // what the button was last time we looked
static char motor1state = 0; // what the last thing we told the motor to do was
:
:
// Read the button
current_state = leftStick->GetRawButton(2);
// Did the button just get pushed?
if (current_state != previous_state) {
// Yes, the button wasn't pushed before, but it is now
if (current_state == 1) {
// Is the motor already on?
if (motor1state == 0} {
// Yes, the motor is on. Turn it off.
motor1state = 1;
motor1Relay->Set(Relay::kForward);
} else {
// No, the motor is off. Turn it on.
motor1state = 0;
motor1Relay->Set(Relay::kOff);
}
}
// remember the button so you don't do this more than once each press
previous_state = current_state;
You can get fancy with using the kForward and kOff enumerations in the motor state variable if you like, so the code becomes a little more self-documenting, but that's not important right now.
Quote:
|
also let's say button 3 is pushed, when they hold it down, it moves the motor relay 2 in the forward direction. otherwise, it doesnt move at all.
|
This is easy, as it requires no "memory" of past events.
Code:
if (left_stick->GetRawButton(3) == 1) {
motor2Relay->Set(Relay::kForward);
} else {
motor2Relay->Set(Relay::kOff);
}
I have used more braces than necessary, and my formatting convention might not match what you want to use. This should give you a good start on what you need to do, though.