0

I am trying to use #define to create a constant and define a pin, check this code

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(PIN_MICROPHONE);
}

But when trying to compile, it says this error:

: In function 'void loop()':
error: expected `)' before ';' token
error: expected primary-expression before ')' token
error: expected `;' before ')' token

How can I use #define macros to define pins?

This code compiles ok

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(13);
}

I am using Arduino 1.0.5

4
  • Arduino language is more like Java, not C (it supports class uses). Commented Jun 3, 2013 at 3:06
  • how that would help me in this problem? Commented Jun 3, 2013 at 15:26
  • 1
    another semicolon sighting, no one is free ... Commented Jun 3, 2013 at 19:29
  • very true my friend :( Commented Jun 3, 2013 at 22:36

1 Answer 1

8

The issue is your semicolon.

#define does not require a semicolon on the end of it.

#define PIN_MICROPHONE 13
void loop()
{
    analogRead(PIN_MICROPHONE);
}

Typically the #define is a pre-compiled directive. That means before the code is compiled a find and replace is done on the text. So the IDE "saw" the code below.

void loop()
{
    analogRead(13;); //not going to work 
}

PS: I thought #defines were not encouraged in Arduino style guides.

Sign up to request clarification or add additional context in comments.

2 Comments

#defines are fine ;-) arduino.cc/en/Reference/Define though apparently const is preferred.
Oh my god, I feel so noob right now..., I have years of experience coding in C, C++, I'm very surprised right now, how could this happen to me... thank you John b, I believe I need to rest a little :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.