Controlling ports in a loop using Vex RobotC

131 views Asked by At

I am using Vex RobotC and have a function: setTouchLEDRGB(portx, R,G,B); which sets the RGB colour of a touch LED.

I have 9 TouchLEDs and want to change the colour of them all at once, now annoyingly this is 9 lines of code at a time, i hope to create a function with an iteration such as:

for (int i = 0, i < 9, i++)
{
    setTouchLEDRGB(port[i], R, G, B);
}

Is there a way to accomplish this?

2

There are 2 answers

2
Danny_ds On BEST ANSWER
setTouchLEDRGB(portx, R,G,B);

Not sure about the platform, but you could create an array containg the ports:

#define NUM_PORTS 9

// 'int' should be the type of your port parameter
int ports[NUM_PORTS] = {PORTA, PORTB, etc};

for (int i = 0; i < NUM_PORTS; ++i) {
    setTouchLEDRGB(ports[i], R, G, B);
}
0
pm100 On

assuming you have variable or macros for the ports called portn

   int ports[9];
    ports[0] = port0;
    ports[1] = port1;
    ...

    for (i = 0, i <9, i ++)
    {
     setTouchLEDRGB(ports[i], R, G, B);
    }