使用Vex RobotC控制环路中的端口
我使用的是使用Vex RobotC控制环路中的端口,c,for-loop,ioports,robotc,C,For Loop,Ioports,Robotc,我使用的是VexRobotC,有一个函数:setTouchLEDRGB(portx,R,G,B)设置触摸LED的RGB颜色 我有9个TouchLED,想要一次改变它们的颜色,现在令人烦恼的是,一次只需9行代码,我希望通过迭代创建一个函数,例如: for (int i = 0, i < 9, i++) { setTouchLEDRGB(port[i], R, G, B); } for(int i=0,i
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);
}
for(int i=0,i<9,i++)
{
setTouchLEDRGB(端口[i],R,G,B);
}
有什么方法可以做到这一点吗?假设您有变量或宏用于名为portn的端口
int ports[9];
ports[0] = port0;
ports[1] = port1;
...
for (i = 0, i <9, i ++)
{
setTouchLEDRGB(ports[i], R, G, B);
}
int端口[9];
端口[0]=端口0;
端口[1]=端口1;
...
对于(i=0,i假设您有变量或宏用于名为portn的端口
int ports[9];
ports[0] = port0;
ports[1] = port1;
...
for (i = 0, i <9, i ++)
{
setTouchLEDRGB(ports[i], R, G, B);
}
int端口[9];
端口[0]=端口0;
端口[1]=端口1;
...
对于(i=0,i
不确定平台,但您可以创建包含以下端口的阵列:
#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);
}
#定义NUM_端口9
//“int”应该是端口参数的类型
int-ports[NUM_-ports]={PORTA、PORTB等};
对于(int i=0;i
不确定平台,但您可以创建包含以下端口的阵列:
#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);
}
#定义NUM_端口9
//“int”应该是端口参数的类型
int-ports[NUM_-ports]={PORTA、PORTB等};
对于(int i=0;i
索引从C中的0
开始。@pzaenger虽然您通常是对的,并且很可能暗示OP的代码中存在错误,但所讨论的端口阵列实际上可能有任何偏移量-甚至可能是1:)@直到这是一个好的观点。但是,如果他/她不知道,我想让他知道是可以的。实际上现在应该是I<9
。索引从C中的0
开始。@pzaenger虽然你通常是对的,而且很可能是暗示OP在他的代码中有一个bug,但所讨论的端口数组实际上可能有任何偏移量-也许甚至1:)@直到这是一个好的观点。但是,如果他/她不知道,我想最好让他知道。实际上现在应该是i<9
。如何避免预处理器并使用静态常量int作为端口数?@Till-不确定这些是否会占用OP平台上的空间,但是也可以使用。如何避免预处理器并使用静态常量int作为端口数?@Till-不确定这些是否会占用OP平台上的空间,但也可以使用。