0

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?

shreyshrey
  • 515
  • 6
  • 20
Andrew O'Rourke
  • 127
  • 1
  • 9
  • Indexing starts with `0` in C. – pzaenger Jan 18 '16 at 19:07
  • 1
    @pzaenger while you are generally right and most likely are hinting a bug the OP has in his code, the port array in question might in fact have ANY offset - maybe even 1 :) – Till Jan 18 '16 at 19:08
  • 1
    @Till That's a good point. But well, I guess it is fine to let him know in case he/she doesn't know. Actually it should be `i < 9` now. – pzaenger Jan 18 '16 at 19:10

2 Answers2

2
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);
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • How about avoiding the preprocessor and using a static const int for the number of ports? – Till Jan 18 '16 at 19:12
  • 1
    @Till - Not sure if those would take any space on the OP's platform, but could be used as well. – Danny_ds Jan 18 '16 at 19:15
1

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);
    }
pm100
  • 48,078
  • 23
  • 82
  • 145