Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Button coding (http://www.chiefdelphi.com/forums/showthread.php?t=144505)

nlt5 02-23-2016 10:25 AM

Button coding
 
Can someone help me with the code that would be used to have a motor come on when a button is pressed. I can get the motors to turn on and off with the press of a button but I want the motor to run while the button is pressed then when the button is released the motor stops.

Thanks for all the help on this forum,
what a great resource for new teams.

Oh and I am using iterative

Pratik Kunapuli 02-23-2016 10:29 AM

Re: Button coding
 
Code:

if(joystick.getRawButton(buttonNumber)
{
    speedController.set(1.0);
}
else
{
    speedController.set(0.0);
}

If this code is in the periodic teleop method, it should do what you want.

nlt5 02-23-2016 10:31 AM

Re: Button coding
 
Great,
thank you very much

nlt5 02-24-2016 07:58 AM

Re: Button coding
 
So I wrote the code and was able to get this to work for the motors in one direction. I would press and hold the button, the motors would spin, and when I released the button they would stop. We want the motors to spin in both directions so I wrote additional code and set the motors to -1 using a different button. When I ran the code the motor would pulse at different rotational rates. When I would program to use 2 buttons, one for on, one for off the pulsing would stop. Any ideas??? I'm not sure why in one direction the motor spins fine but in the other they pulse.

thanks

Ether 02-24-2016 09:13 AM

Re: Button coding
 
Quote:

Originally Posted by nlt5 (Post 1545789)
I wrote additional code and set the motors to -1 using a different button. When I ran the code the motor would pulse at different rotational rates.

This sounds like the common coding error where one button is commanding the motor to go while the other is commanding it to stop.

You need to change your if/then/else logic so that only one command is issued to the motor(s).


if your code looks something like this:
Code:

if (button1) {setmotor(1)} else setmotor(0);

if (button2) {setmotor(-1)} else setmotor(0);



... then change it to this:
Code:

if (button1) {setmotor(1)}
else
if (button2) {setmotor(-1)}
else setmotor(0);




nlt5 02-24-2016 12:15 PM

Re: Button coding
 
Sounds good,
I will test in about a month when it is legal to unbag our robot.

Thank you very much.

Potatonator 02-28-2016 11:41 AM

Re: Button coding
 
Quote:

Originally Posted by Pratik Kunapuli (Post 1544973)
Code:

if(joystick.getRawButton(buttonNumber)
{
    speedController.set(1.0);
}
else
{
    speedController.set(0.0);
}

If this code is in the periodic teleop method, it should do what you want.

Is there any way to set this to run from multiple joysticks? For instance, having button 1 on joystick 1 start and button one on joystick 2 stop it?

Pratik Kunapuli 02-28-2016 01:54 PM

Re: Button coding
 
Quote:

Originally Posted by Potatonator (Post 1548363)
Is there any way to set this to run from multiple joysticks? For instance, having button 1 on joystick 1 start and button one on joystick 2 stop it?

You could do it like this:

Code:

if(joystick1.getRawButton(buttonNumber)
{
    speedController.set(1.0);
}
else if(joystick2.getRawButton(buttonNumber)
{
    speedController.set(0.0);
}


Potatonator 02-28-2016 02:25 PM

Re: Button coding
 
Thank you. Looks like it's working.


All times are GMT -5. The time now is 07:59 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi