easyC question

I want to use a button on the joystick to start a function that I wrote. Then when I press the button again, the function stops.

For example, I’ve written a function that spins a light on the top of a robot when the light sensor detects it is dark. (useless function, but there it is)

I want to be able to enable and disable this functionality with the joystick. Press a button, the function is running. Press button again, function is not running.

Any ideas?


instead of using a function you could use a task. So when you press the function “StartTask(whatever);” when you press it again “StopTask(whatever);” thats how you would do it in RobotC i don’t know the coding for EasyC But basically I would use tasks instead of functions, unless there is a way to do it with functions that I don’t know about.

OP said EasyC (which does not have tasks) not RobotC.
The simplest usual way for this kind of thing in EasyC is to call your function conditionally from the main event loop.
Make sure your function only takes a short time to execute, or it will slow down the entire event loop.

As for the “toggle” function of the joystick button, I think there is a relatively new EasyC function with “toggle” in the name to toggle a variable when a button is pressed. If not, you’ll have to find one previously posted, or write your own.

ya my bad. I knew he said easyc i just didn’t know EasyC didn’t have tasks

Never tried this out before, but something similar works on Processing.
Perhaps try it out?

And to create a function I think you can just right click on “User Functions” under function blocks then “add new function” and just make one there.
easyC example 3.jpg

Using a user function is the best way to do it, as the above poster posted the loop can be used to detect whether a button is being pressed and used to send the control into the loop.