View Single Post
  #4   Spotlight this post!  
Unread 04-10-2016, 10:14 AM
engunneer's Avatar
engunneer engunneer is offline
Alumni turned Mentor
AKA: Branden Gunn
FRC #4761
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1996
Location: Reading, MA
Posts: 721
engunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond reputeengunneer has a reputation beyond repute
Re: Limit Switch Programming

Code:
public void teleopPeriodic() {
  if (stick.getRawButton(1) && !limitSwitch ) {
    motor1.set(0.5);
   }
  else if (stick.getRawButton(2)) {
    motor1.set(-0.5);
  }
  else { 
    motor1.set(0.0); 
  }
}
do you have two limit switches, or just one? Which way is it supposed to prevent motion?

The key changes i made here are using boolean logic to modify the if statement, and to chain both statements together using else if.

I'd also suggest formatting your code in a nested fashion. It's really useful when debugging

Lastly, i am a proponent of making sure all your logic only has one way to set the motor, so I move all my Motor Set commands to the very end, and use a variable to keep track of what i want the motor to do. This is also helpful for displaying the desired command on smartdashboard for debugging. Similarly, gather all your inputs at the begining to meaningful variable names as well

Code:
public void teleopPeriodic() {
  boolean intakeMotorForward = stick.getRawButton(1);
  boolean intakeMotorBackward = stick.getRawButton(2);

  if (intakeMotorForward && !limitSwitch ) {
    intakeMotorSpeed = 0.5;
   }
  else if (intakeMotorBackward) {
    intakeMotorSpeed = -0.5;
  }
  else { 
    intakeMotorSpeed = 0;
  }

  motor1.set(intakeMotorSpeed); 
}
__________________
Student FRC23 (1996-1999), Mentor FRC246 (2000), Mentor FRC1318 (2007-2009), Mentor FRC93 (2011), Mentor FRC2151 (2012), Mentor FRC23 (2013), Mentor FRC4761 (2014-2017)
1998 - National Chairman's Award and Woodie Flowers Award (FRC23, Mike Bastoni ) | 2007 - PNW SF (488, 1595) | 2008 - Oregon RCA - Seattle #2 Seed, SF (488, 1696) | 2009 - Oregon #1 Seed, Winners (1983, 2635) - Seattle SF (945, 2865) - Galileo #2 Seed, SF (973, 25) | 2012 Midwest F (111, 71) | 2014 RIDE Winners (78, 125), Inspector - NEU #24, QF (3479, 3958) - NECMP #35 | 2015 Reading #11, SF (1058, 190), Inspector - RIDE #17, QF(4055, 5494), Inspector - NECMP #57 | 2016 Reading #4, SF (133, 4474), DCA, Inspector - Ride #22, SF (1735, 2067), Creativity, Inspector - NECMP #48, RCA - Archimedes
Reply With Quote