Why is my nested while loop not working? -
i'm programming in robotc, vex 2.0 cortex. i'm using encoders make robot go straight.
this code:
void goforwards(int time) { int tcount = 0; int speed1 = 40; int speed2 = 40; int difference = 10; motor[lm] = speed1; motor[rm] = speed2; while (tcount < time) { nmotorencoder[rm] = 0; nmotorencoder[lm] = 0; while(nmotorencoder[rm]<1000) { int rencoder = -nmotorencoder[rm]; int lencoder = -nmotorencoder[lm]; if (lencoder > rencoder) { motor[lm] = speed1 - difference; motor[rm] = speed2 + difference; if (motor[rm]<= 0) { motor[rm] = 40; motor[lm] = 40; } } if (lencoder < rencoder) { motor[lm] = speed1 + difference; motor[rm] = speed2 - difference; if (motor[rm]<= 0) { motor[rm] = 40; motor[lm] = 40; } tcount ++; } } } } task main() { goforwards(10); }
for reference, these pragma settings:
#pragma config(i2c_usage, i2c1, i2csensors) #pragma config(sensor, dgtl2, , sensordigitalin) #pragma config(sensor, dgtl7, , sensordigitalout) #pragma config(sensor, i2c_1, , sensorquadencoderoni2cport, , autoassign ) #pragma config(sensor, i2c_2, , sensorquadencoderoni2cport, , autoassign ) #pragma config(motor, port1, rm, tmotorvex393_hbridge, openloop, reversed, encoderport, i2c_2) #pragma config(motor, port10, lm, tmotorvex393_hbridge, openloop, encoderport, i2c_1)
when excecute code, robot's encoder values close, robot stops moving when reach 1000. thought code wrote should return values of encoders 0 after reach 1 thousand, , thereby code should re-iterate in shell loop 10 times (in case). have done wrong?
you updating tcount
@ wrong place. @ end of outer loop.
what have written makes tcount
increase everytime moves ahead. time reaches 1000 steps, tcount
1000.
your times
10. `tcount >= time , wont enter outer while loop again.
Comments
Post a Comment