如何在Arduino的阅读20随机排序销兆丰有效? [英] How to read 20 randomly ordered pins on Arduino Mega efficiently?

查看:167
本文介绍了如何在Arduino的阅读20随机排序销兆丰有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们对的Arduino 兆丰的是这样的:

#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 如果语句读取每个引脚。我想到的是使用数组的,但我怎么有效地做到这一点?

As you can see the pin numbers do not follow a specific order? I do not want to use 20 if statements to read each pin. I am thinking of using arrays, but how do I do it efficiently?

推荐答案

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

So you have x1-20 in your application, and you have these values hooked up to arbitrary pins on the I/O connector?

简单地定义它们之间的阵列映射

Simply define an array mapping between them

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

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

PS。你不再需要再#define语句,只需要使用 X [N] 代替 XN

这篇关于如何在Arduino的阅读20随机排序销兆丰有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆