![]() |
Java Toggle Button
Hello, we have a conveyor belt powered by a motor on our bot. We want to press a button and make it run until we press the same button again. We cannot figure out how to do this. Here is what we currently have for our code:
Code:
//Conveyor Belt |
Re: Java Toggle Button
You need an interlock, something along the pseudo-code lines of:
Code:
boolean belt = false; |
Re: Java Toggle Button
Quote:
Also, to implement a toggling routine in code that takes input from a human, you need a way to slow it down or track previous states. For example, if a toggle function runs once every 10ms, a human pressing a button for 1/2 a second will cause the code to toggle 50 times. There are two ways to overcome this: timers (only allowing the code to toggle once every ___ ms), or by only toggling the output on button transitions (such as from a not pressed state to a pressed state). Here's a real quick piece of [untested] code that works off the latter principle: Code:
// Run this only once to initialize the variablesCode:
// Run the following code continuously |
Re: Java Toggle Button
Quote:
beltStatus = !beltStatus; |
In terms of having things run only once per button press the command based template for WPILib has a nice feature where it takes care of that for you in your OI class.
Pseudo code for OI.java Button toggle = new JoystickButton(BUTTON-NUMBER); //put a joystick button into a var toggle.whenPressed(new toggleConveyorCommand); //upon press run the toggle command once Pseudo code for toggleConveyorCommand.java: execute() { //the execute part if(Conveyor.getInstance().isRunning()) {//if the conveyor singleton is running Conveyor.getInstance().stop(); //stop it } else { //if not Conveyor.getInstance().start();// start it } } |
Re: Java Toggle Button
You could do this in 2 parts:
Code:
//In the code that iterates:Code:
if (GAMEPAD_R1) shiftUp = true; |
| All times are GMT -5. The time now is 10:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi