OK so here’s the rundown. i am new to robot c ( have been using easy c for the past 3 years and i decided to make the big jump) and in about 2 hours i got all the drive and autonomous working and so today i went to go put encoders on because i thought that OK, so if drive wasn’t so bad how bad can this be… its bad. right now this is all i have done and looking at the examples robot c has made hasn’t helped . i talked to a teammate that has been using robot c for about 6 months now and he wasn’t able to figure it out because he is using PID controls. however we found that it is never leaving the IF loop. currently rde= Right Drive Encoder and we are testing it on one motor before we attach it to our robot. (motor ml_bl__Y)
if (SensorValue[rde] < 1000)
{
motor[ml_bl__Y]= 127;
motor [fl]=127;
motor [mr_br__Y]=127;
motor [fr] =127;
}
else
{
motor[ml_bl__Y]= 0;
motor [fl]=0;
motor [mr_br__Y]=0;
motor [fr] =0;
}
First of all, an if() statement is not a loop, but just a statement that declares running the portion inside the curly brackets “{}” only once from top to bottom if the statement inside the brackets "()"is true. A while() statement is a loop that runs the portion inside the curly brackets “{}“infinitely as long as what’s inside the parenthesis”()” has a value that is true… If that’s not the solution to what you’re asking, read more:
The first thing I would do is check if the encoder is working correctly. Try following these steps to see if your encoder works:
Make sure your robot is plugged in to your computer is turned on
When connected to your computer, go to Robot>Open Debugger Manually
While the debugger is open, go to Robot>Debugger Windows>Sensors (This should let you see the current sensorvalue of the sensors on your robot.
If the encoder is showing a -1(skip this and go to my second suggestion if it doesn’t show a -1), the input/output ports are probably switched, the encoder is on the wrong ports, or the encoder is broken. Double check that the encoder is on the correct port, and try swapping the input/output pins on the ports.
If even after these steps, and you still have a -1, try replacing the encoder.
When I first started with ROBOTC, I didn’t actually learn by following the videos. I rather more opened the sample programs and dissected what the programs mean. Sample programs are really useful for people who don’t exactly know how to program a specific sensor, variable, and much more. Go to File>Open Sample Program to see a great amount of examples you can use when learning to program. When you open a sample program, and you wish to tinker that program, make sure you save it before doing anything else :).
Hopefully this helps
we were able to see the encoder count but our problem was that when the IF statement hit 1000 the motor didn’t stop. it just kept going. we have also tried another encoder but had the same result (may i add this is a 2 wire encoder not single for both)
As Connor said, a “while” statement is better suited for this task. There’s a sample program that does something similar in VEX2\Shaft Encoder. It’s called Forward For Distance.
//Clear Encoders
SensorValue[rightEncoder] = 0;
SensorValue[leftEncoder] = 0;
while(SensorValue[leftEncoder] < 1800) // While less than 5 rotations on the leftEncoder...
{
//...Move Forward
motor[rightMotor] = 63;
motor[leftMotor] = 63;
}
This is the main part of that program. First, it clears the encoder values, which I believe is automatically done each time you compile. Then comes the while loop. It checks the condition in the parentheses, and if it’s true, runs the program in the {}s, and returns to checking the condition in the parentheses again. When the SensorValue goes over 1800, the condition becomes false, and the robot exits out of the loop.
The only part missing from this program is stopping the motors. The sample works because the whole program ends when the while loop is finished. I believe your program has more components than that, so it may be that the stop command is never given. If you were to use something similar to the sample program, you would add,
motor[rightMotor] = 0;
motor[leftMotor] = 0;
at the end, after the while loop.
If that doesn’t work, you could post your whole program here, and we could look at it for you.
so i just went to try and see if the pre-made program would work… it didn’t. i tried 2 different encoders and had the same results. any other ideas? i also posted the program i wrote in first thread
You might want to check if your cortex port is broken or not, it goes into digital, and you have to make sure that you plugged your wires in correctly, a good way to check is by using the debugger window after you have downloaded your program into the cortex, the encoder reading should change as you rotate your robot. If that works, then it must be your code.