Commands based on how Long Button is Held

I think I can do this with a ConditionalCommand but I was having trouble figuring out how I would know how long the button had been held.

I looked at using a timer but the conditional command wanted a boolean supplier and the timer.get() > x seconds was a boolean.

You want to take your statement and put it in a lambda function. This is the core of the Java Supplier Class. Here is a pretty good overview of how they work: http://www.java2s.com/Tutorials/Java/java.util.function/Supplier/index.htm

() -> timer.get() > x this will be a BooleanSupplier. () -> is like a function declaration, that will run the statement after the ‘arrow’ and return the result. You can then pass this to your Command or whatever other Object/Method you want. Something like below will pass a BooleanSupplier, and whatever type of subsystem you passed.

new Command(() -> timer.get() > x, subsystem)

What doing this allows is, on the other side, you can call this lambda Supplier function multiple times and get an updated result from it, whereas if you passed a primitive type, like a boolean, you’d only have the answer at the moment you passed it, you wouldn’t be able to get an updated result internally, you’d need the outside resource to send updated results or some other work-around.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.