We’re working on a method that when we hit a button on the joystick, we want 2 pneumatic cylinders to fire forward and stay forward until we hit the same button again at which point the cylinders will retract. So far however all we get is the cylinders to fire once and not retract. I’m sure we’re pretty close, but just missing something small. We’re using a boolean to make sure that we don’t try to fire forward if they are already forward. Are we using everything correctly? Here’s our pseudo code. All of this code is in the teleop periodic method.
boolean status = false;
if(joystick.getRawButton(3))
{
if(status == false)
{
solenoid.kforward();
status = true;
}
else
{
solenoid.kreverse();
status = false;
}
}
I think your code will flip the valve back and forth as long as the button is held down. Do you need to detect when the button first goes down, and only execute your logic then?
Untested code:
// run once
boolean status = false;
boolean buttonWasDown = false;
// run inside operator control loop, or teleopPeriodic, or whatever
boolean buttonIsDown = joystick.getRawButton(3);
if(buttonIsDown && !buttonWasDown)
{
if(!status)
{
solenoid.kforward();
status = true;
}
else
{
solenoid.kreverse();
status = false;
}
}
buttonWasDown = buttonIsDown;
oh!!! you want the code that looks at the button to be in teleopPeriodic; if it’s in teleopInit, the button will only get checked once at the beginning of teleop, and never again.
Hey! our team is trying to do basically the same thing and our code is hitting an error on the “kreverse/kforward” when we try to get our solenoid object do them. we used the code provided above so instead of that i will provide the declaration
public class Robot extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*
*/ RobotDrive myDrive, catcherDrive;
Joystick moveStick;
Gyro scotbot;
Solenoid S;
public void robotInit() {
Talon TalLF = new Talon(0);
Talon TalLR = new Talon(1);
Talon TalRF = new Talon(2);
Talon TalRR = new Talon(3);
Talon TalCatcher = new Talon(4);
Talon TalCatcher1 = new Talon(5);
myDrive = new RobotDrive(TalLF,TalLR,TalRF,TalRR);
moveStick = new Joystick(0);
catcherDrive = new RobotDrive(TalCatcher, TalCatcher1);
scotbot = new ADXRS450_Gyro();
Compressor c = new Compressor(0);
Solenoid S = new Solenoid(1);
}