How to Fix . . . “Glitchy” Servo Code Mike Zook 05-Sep-2016
Original Code //Partially working method to experiment with the servo, works but is very glitchy public void servoTest2(){ if (gamepad1.a) { if(buttonAPushed) servo1.setPosition(1); buttonAPushed = false; } else if(!buttonAPushed) servo1.setPosition(0); buttonAPushed = true;
Original Code Sequence
The Glitch The servo position is continuously toggling between 0% and 100% while the Gamepad <A> button is pressed. The Fix Toggle servo position one time per button press.
Alternative Sequence
Alternative Code public void servoTogglePosition(Servo servo, double dLastServoCommand, bool bGamepadPB, bool bPBPressed) // This method toggles the position of a servo. The servo's range of motion is 0% to 100%. { if (bGamepadPB) if(!bPBPressed) // If true, this is the first scan where gamepad button is pressed. bPBPressed = 1; // Acknowledge gamepad button pressed // Toggle servo position if (dLastServoCommand = 0.0) dLastServoCommand = 1.0; else dLastServoCommand = 0.0; servo.setPosition(dLastServoCommand); } else bPBPressed = 1; // Unacknowledge gamepad button pressed when button released.
Something to Think About How would you change the sequence (and code) to slowly toggle the servo to prevent damage to the servo gears?