In response to @iperez 's question about his code, which I can’t answer because it’s in the ROBOTC tech support subchannel:
I see two things that could be fixed with this code.
Unless it’s in the rest of the code that was not included, there’s no brace that ends your if statement. Unless it is there, everything after that will run only when the if statement is true, which I don’t think is what you’re trying to do.
Also, instead of having other if statements to see if
reverse
is 1 or -1, you could just multiply it by -1, essentially creating a “toggle”. You could do this by saying
reverse = reverse * -1;
or by saying
reverse *= -1;
.
Here’s an updated version of that code:
int reverse = 1;
while(true){
if(vexRT[Btn7U]){
reverse *= -1;
}
// Rest of the code
Thank you for your answer, @Mystellianne
So there was a typo when I wrote the code in the forum. It’s supposed to be:
int reverse = 1;
while(true){
if(vexRT[Btn7U]){
if(reverse == 1)
reverse =-1;
else if(reverse==-1)
reverse = 1
}
// Rest of the code
I will change it to how you did it, but do you know why there seems to be a delay in in activating it. Sometimes you have to wait until the robot completely stops for it to work.
I’m not so sure you’re seeing what you think you’re seeing. I suspect you are not seeing a delay at all. Rather, you’re probably seeing things happen so fast that you think it’s delaying, rather like watching a wagon wheel in a moving and seeing it roll backward while its actually rolling forward.
I would highly, highly recommend putting a delay in your loop. How long does it take that entire loop to finish? Consider how much time it takes a person to press and release the button. What I believe is happening is that you press the button and the loop happens a whole bunch of times before you release the button. So that if statement gets triggered a whole bunch of times, meaning it’s essentially random which outcome you’ll get.
If you don’t want a delay in the rest of the code, you can always insert it here
if(vexRT[Btn7U]){
if(reverse == 1)
reverse = -1;
else if(reverse == -1)
reverse = 1;
wait1Msec(20); <------ delay only if the button is seen to have been pressed
}
It would be better to look for a release after the button’s been pressed, though:
if(vexRT[Btn7U]){
if(reverse == 1)
reverse = -1;
else if(reverse == -1)
reverse = 1;
while(vexRT[Btn7U]) { <-------- delay until the button is released.
wait1Msec(5); <-------- set this to what seems to work best, small amounts are OK, and I believe RobotC is OK if you just leave the while loop empty.
}
}