In addition to everyone else's suggestions, the problem you are having is mostly from the structure of your if statement
Code:
if(sonar.getRangeInches() < 40) {
driveTrain.setSensitivity(0.5)
elseif(sonar.....<30){
driveTrain.setSensitivty(0.25)......
remember that if an if statement returns true, the other elses aren't always evaluated
What you wrote
Code:
If it's <40, then do something
If it's not <40 but is <30, do something else (this can't happen!)
try instead building from one direction to the other
Code:
if(sonar.getRangeInches() >= 40)
{
driveTrain.setSensitivity(1);
}
else if(sonar.....>= 30)
{
driveTrain.setSensitivity(0.5);
}
else /*if(sonar.....< 30)*/
{
driveTrain.setSensitivty(0.25);
}
you can also do it mathematically (y=mx+b)
Code:
driveTrain.setSensitivty(sonar.getRangeInches() / 40)