java


I'm trying to program a “latched button” for a robot, however when I run through the code the robot does not respond


I need to program a robot to drive at 50% speed when the 'a' button on a game controller is pressed and released, and then to drive at 100% when 'a' is pressed and released again, like a latched button. I have the following code that makes the robot drive a 50% when 'a' is held down constantly, and at 100% when 'a' is released, it works fine.
telemetry.addData("Status", "Running: " + runtime.toString());
if(gamepad1.a)
{
telemetry.addData("Status", "A pressed");
leftMotor.setPower(-gamepad1.left_stick_y*0.5);
rightMotor.setPower(-gamepad1.right_stick_y*0.5);
telemetry.addData("Status", "Running Slow");
}
else {
leftMotor.setPower(-gamepad1.left_stick_y);
rightMotor.setPower(-gamepad1.right_stick_y);
telemetry.addData("Status", "Running regular");
}
}
And this is the code I have for the latched button, it does not drive when running. I think it is just a matter of fixing the if and else statements, but I cant pinpoint the problem .
telemetry.addData("Status", "Running: " + runtime.toString());
if(gamepad1.a)
{
telemetry.addData("Status", "A pressed");
//if A was pressed
if(aPressed == false)
{
aPressed = true;
if(goingSlow)
{
leftMotor.setPower(-gamepad1.left_stick_y);
rightMotor.setPower(-gamepad1.right_stick_y);
telemetry.addData("Status", "Running Regular");
goingSlow = false;
}
else {
leftMotor.setPower(-gamepad1.left_stick_y*0.5);
rightMotor.setPower(-gamepad1.right_stick_y*0.5);
telemetry.addData("Status", "Running Slow");
goingSlow = true;
}
}
}
else
{
aPressed = false;
}
I think that you are trying to do too much inside the branches of the "if" statement. Moving some of the functionality to other methods should make things easier to understand.
It's difficult to know exactly what's going on, since you don't show the entire method, or provide any context about how you are notified of the button presses / releases, but you might want to try something like this, once you've dealt with the button presses and figured out whether you should be going slow or fast:
if(shouldGoFast) {
goFast();
} else {
goSlow();
}
public void goFast() {
leftMotor.setPower(-gamepad1.left_stick_y);
rightMotor.setPower(-gamepad1.right_stick_y);
telemetry.addData("Status", "Running Regular");
}
public void goSlow() {
leftMotor.setPower(-gamepad1.left_stick_y*0.5);
rightMotor.setPower(-gamepad1.right_stick_y*0.5);
telemetry.addData("Status", "Running Slow");
}
I'm assuming that you don't really need to keep track of whether you're currently going slow or fast, i.e. there's no problem if you execute goFast() when you're already going fast.

Related Links

publishEvent doesn't work
hadoop program not executing… throwing null pointer exception
Java ArrayList using in JSTL(<c:foreach>) [duplicate]
Byte array to String back to byte array - Performance wise
Maven error when using command line to execute java program
How do I make my program show the reverse order of the integers that was entered by the user?
Hadoop - No job jar file set. User classes may not be found
can we update a document in mongodb using _id in java?
how to break last 2 for loops out of 4 for loops? [duplicate]
Jersey Application 404 not found error
Java's getWheelRotation() always returns 1 or -1
At remote system .isDirectory function of java is not working properly
how to get exponents without using the math.pow for java
camel-redis: why does this not print to the console?
hadoop: can many map running parallelly in a single node share static data?when enble jvm reuse
Java fillRect() Inconsistencies

Categories

HOME
testing
openstack
image-processing
json.net
stock
relayjs
react-router
at-command
jpeg
jxls
binary-tree
fingerprint
serverspec
session-variables
node-pdfkit
phaser
static-libraries
dtrace
windows-server-2012
ups
designer
netflix
seaborn
graphlab
reactcsstransitiongroup
nhibernate-envers
visjs
mps
android-widget
opennlp
shopware
url-scheme
linkerd
windows-error-reporting
web-api-testing
applozic
ping
google-cloud-nl
newline
nouislider
typed.js
jmonkeyengine
splice
trim
node-gyp
ibpy
heightmap
outlook-api
qcombobox
jsch
sage-one
worker-thread
lift-json
windows-mobile-6.5
pdfclown
specrun
cubes
trash
iad
rtbkit
spim
xna-4.0
r-tree
lemon
iis-arr
pundit
sframe
cakephp-3.1
ibaction
ideamart
service-accounts
graph-api-explorer
markers
issuu
project-planning
hippomocks
imdbpy
mcts
c18
xsockets.net
commoncrypto
sitemesh
dataadapter
eventual-consistency
sublist
trusted
parametric-equations
online-compilation
nsdatecomponents
libc++
gcj
coredump
radcombobox
getresponsestream
meego
backcolor
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App