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

JBAS018733 : Failed to process phase PARSE of subdeployment
Running java process command using Tomcat-7
Pass user input array to method and return an integer?
Mixing a 2D array
How do I create an async caching http client?
Making a game with Processing, running into issues with time methods
Upload image from android app gallery to local spring server
Java and C# interpret the multiplication * sign differently, why?
Accessing AWS parameter store values with custom KMS key
Java Preparedstatement error when reading from MySql Database
process.waitFor(timeout, timeUnit) does not work as expected
Java: Two inputs have same initial character
Java - How to check if dynamically instantiated method is called at runtime?
Send a file from my website to another website using Java
why this method is not giving me the subnet mask in android
Jersey Guice Integration exception

Categories

HOME
ms-access
pdf
proxy
omnet++
mediawiki
google-play
hp-exstream
syntax
octobercms
spagobi
framework7
adb
cvs
jsrender
rubygems
mvvmcross
opengl-es-2.0
wamp
azure-media-services
medical
node-pdfkit
offline
collectd
spring-tool-suite
task
visual-studio-2005
hex-editors
quickfix
serverless-framework
vaadin7
django-simple-history
windows-7-x64
google-static-maps
flux
normalizr
beyondcompare
jasonette
spark-jobserver
user-interaction
ping
buck
librato
gsoap
pim
android-kernel
hexo
wpf-controls
overriding
simplexml
serve
event-driven
android-browser
twitch
ansible-playbook
greenrobot-eventbus
squib
preconditions
spring-security-kerberos
qwt
logfiles
smartcontracts
chain-builder
clean-architecture
domain-model
return-value
git-diff
darcs
setter
crypt
windows-iot-core-10
capacity
vtigercrm
blacklist
background-service
topbeat
gridpane
moveit
superstack
master-slave
citrus-pay
tarjans-algorithm
twirl
processmodel
gulp-less
markers
code-access-security
viadeo
openlaszlo
html5-notifications
terminal-services
oam
xsockets.net
cdc
blending
dataservice
plasma
cassini-dev
factory-method
distutils
onsubmit
regsvr32
removeclass
netdna-api
eclipse-templates
psi
f#-powerpack
mysql-error-1005
xfbml
fluent-interface
coda-slider
firefox-5
kdbg
text-coloring

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