Wow, I haven’t been on here in a while. I’ve been programming in C++ (that’s different) for the FRC!

My question to you guys is how do I calculate distance using an ultrasonic sensor? I know that you have to calculate how much time it takes for the signal to get back to the robot, but I don’t know how to write that in C.

So, could someone please show me how to program the robot to start counting after it starts the encoder and then stops when it receives a signal?? Thanks!

unsigned int distance;
StartUltrasonic(ping,listen);
while (1) {
distance = GetUltrasonic(ping, listen);
printf("Distance = %d",distance);
}

Then they start taking samples of what they get back for the distance numbers. They do the math, plot curves, etc. They then update the code to be:

unsigned int distance, inches;
StartUltrasonic(ping,listen);
while (1) {
distance = GetUltrasonic(ping, listen);
inches = distance / 5; // tests show that there were 5 distances to an inch.
printf("Distance = %d, which is %d inches",distance, inches);
}