Iterating through Sensors in RobotC

I was just working on some Sensor stuff for our robots and needed the use of a loop with sensors.

Is there any way to iterate through Sensors like you would an array? What would the index be at that point?

Because for the SensorValues], its index values are either in1, dgtl1, or I2C_1.

Sure, take a look in the RobotCIntrinsics.c header file and you will see the numeric values for the various sensor types (the cortex is defined as VEX2). You may need to cast to tSensors to stop any warnings. I can post an example later of you get stuck.

As you can access sensor values as an array, you can use SensorValue*, with index 0-7 being analog, 8-19 being digital and 20-27 being I2C ports.

Arrays are indexed with integers, the compiler substitutes the name you define at the top (or the port if you haven’t changed the name) with an integer.

If you want to find out the index of something else you can use


writeDebugStreamLine("%d", name_of_sensor_or_motor_port);

To answer your original question, you can iterate through the sensor values simply with a for loop from 0-27.*

This is actually really cool. I have thought of things before that would be so much easier with iterating through sensors with loops, but I just did not know how to to it.
Is there a way to iterate through motors like this?

Yep, it is the same principle, motor[0] through to motor[9].

Got the iteration working. Thanks a lot!

You know the rule, sample code or it didn’t happen. :rolleyes: