0

我正在使用Vex RobotC并有一个功能:setTouchLEDRGB(portx, R,G,B);设置触摸 LED 的 RGB 颜色。

我有 9 个 TouchLED,想一次改变它们的颜色,现在烦人的是一次 9 行代码,我希望创建一个迭代函数,例如:

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

有没有办法做到这一点?

4

2 回答 2

2
setTouchLEDRGB(portx, R,G,B);

不确定平台,但您可以创建一个包含端口的数组:

#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);
}
于 2016-01-18T19:10:32.473 回答
1

假设您有名为 portn 的端口的变量或宏

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

    for (i = 0, i <9, i ++)
    {
     setTouchLEDRGB(ports[i], R, G, B);
    }
于 2016-01-18T19:10:03.863 回答