Expected ')' before numeric constant

Hi all! I am new returning to coding after ~20 years and I find myself stuck on likely a very basic issue...

I am trying to use 2 push buttons to control in and out on an actuator and do not undestand why i am getting error "expected ')' before numeric constant" on my 4th line of code as seen below:

 #define BUTTON_PIN 2
 #define BUTTON_PIN 3
int outButtonState = digitalRead(BUTTON_PIN 3);
int inButtonState = digitalRead(BUTTON_PIN 2); // ERROR IS WITH THIS LINE

Please advise with out beating me up too much... lol

Thanks in advance.

this defines BUTTON_PIN to 2 and then the next line defines it as 3.

the space matters.

did you mean BUTTON_PIN_2 2?

The syntax of define means - "define a macros BUTTON_PIN and set its value to 2"

So the two lines at beginning of your code defines the same macros BUTTON_PIN twice. If you need to define a two pins, the names must not be the same.
For example:

#define BUTTON_PIN1 2
#define BUTTON_PIN2 3

Than, when you use the macro in the code, you must place only the name rather than "name value" pair. So the correct variant of your 4th line will be:

Thanks for the feedback b707. That is odd as even if i change them to say:

#define BUTTON_PIN_IN 2
#define BUTTON_PIN_OUT 3

I get the same error. Does that make sense?

You wrote above that you got an error in 4th line. Did you changed it? Please show your code in full

Yes. As I responded to b707:

Even if I define them as:

#define BUTTONPIN_IN 2
#define BUTTON_PIN_OUT 3

I get the same error.

Please show your code

why are there 2 separate values: BUTTON_PIN and 3, being passed as arguments? there should only be one

digitalRead (BUTTON_PIN);
#define BUTTON_PIN_2 2
#define BUTTON_PIN_3 3
int outButtonState = digitalRead(BUTTON_PIN_3);
int inButtonState = digitalRead(BUTTON_PIN_2);

Part of using # define or other ways to give names to numbers is to... leave the numbers behind, and to make one place where the identifier <-> number translation is done, in the case you want to change your wiring, or TBH to fix mistakes in software instead of hardware, like:

#define BUTTON_A 2
#define BUTTON_B 3

int outButtonState = digitalRead(BUTTON_B);
int inButtonState = digitalRead(BUTTON_A);

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.