如何使用#define在Arduino中分配引脚?

Joe*_*zas 0 macros arduino c-preprocessor

我试图用来#define创建一个常量并定义一个引脚,检查这个代码

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(PIN_MICROPHONE);
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试编译时,它会说这个错误:

: In function 'void loop()':
error: expected `)' before ';' token
error: expected primary-expression before ')' token
error: expected `;' before ')' token
Run Code Online (Sandbox Code Playgroud)

如何使用#define宏来定义引脚?

这段代码编译好了

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(13);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Arduino 1.0.5

Joh*_*n b 7

问题是你的分号.

#define 它的末尾不需要分号.

#define PIN_MICROPHONE 13
void loop()
{
    analogRead(PIN_MICROPHONE);
}
Run Code Online (Sandbox Code Playgroud)

通常,#define是预编译的指令.这意味着在编译代码之前,对文本进行查找和替换.所以IDE"看到"了下面的代码.

void loop()
{
    analogRead(13;); //not going to work 
}
Run Code Online (Sandbox Code Playgroud)

PS:我认为#define在Arduino风格指南中并没有受到鼓励.

  • #defines很好;-) http://arduino.cc/en/Reference/Define虽然显然**const**是首选. (2认同)