如何有效地读取Arduino Mega上20个随机排列的引脚?

时间:2011-03-25 00:03:26

标签: mapping arduino

我们通过Arduino Mega这样定义了引脚:

#define x1 3
#define x2 14
#define x3 22
#define x4 52
#define x5 24
#define x6 50
#define x7 26
#define x8 48
#define x9 28
#define x10 46
#define x11 2
#define x12 15
#define x13 23
#define x14 53
#define x15 25
#define x16 51
#define x17 27
#define x18 49
#define x19 29
#define x20 47  

您可以看到针脚编号不符合特定订单?我不想使用20 if语句来读取每个引脚。我正在考虑使用数组,但我该如何有效地做到这一点?

1 个答案:

答案 0 :(得分:4)

所以你的应用程序中有x1-20,你有这些值连接到I / O连接器上的任意引脚吗?

只需在它们之间定义一个数组映射

int x[] = { 3, 14, 22 ........ 47 };

// Then just 
digitalRead(x[2]); // Reads pin 14.

PS。您不再需要#defines,只需使用x[N]代替xN