IDE ERRORS

Hi,

I was trying to make a code for a relay and a button, but that doesn’t matter.
I made some mistakes (forgot semicolons and wrong () ). I thought the arduino IDE (version 1.6.7) “verify” function would give me the error messages i need to correct the code but instead this happened:

This is the code with the mistakes:

#define relay1 7
#define but 11

void setup() 
{
pinMode(relay1,OUTPUT);
pinMode(but,INPUT_PULLUP);
digitalWrite(relay1, LOW);
}

void loop()
{
  if(digitalRead but == LOW && digitalRead relay1 == LOW){
    digitalWrite(relay1, HIGH)
    delay(500)}

  if(digitalRead but == LOW && digitalRead relay1 == HIGH){
    digitalWrite (relay1, LOW)
    delay(500)}
}

but i got this error message:

exit status 1
expected ‘)’ before numeric constant

and line 2 was marked red

I though i used #define wrong so i used const int to correct all the other mistakes:

#define relay1 7
const int but = 11;

void setup() 
{
pinMode(relay1,OUTPUT);
pinMode(but,INPUT_PULLUP);
digitalWrite(relay1, LOW);
}

void loop()
{
  if(digitalRead (but) == LOW && digitalRead (relay1) == LOW){
    digitalWrite(relay1, HIGH);
    delay(500);}

  if(digitalRead (but) == LOW && digitalRead (relay1) == HIGH){
    digitalWrite (relay1, LOW);
    delay(500);}
}

and when I changed const int back to #define it worked perfect!

#define relay1 7
#define but 11

void setup() 
{
pinMode(relay1,OUTPUT);
pinMode(but,INPUT_PULLUP);
digitalWrite(relay1, LOW);
}

void loop()
{
  if(digitalRead (but) == LOW && digitalRead (relay1) == LOW){
    digitalWrite(relay1, HIGH);
    delay(500);}

  if(digitalRead (but) == LOW && digitalRead (relay1) == HIGH){
    digitalWrite (relay1, LOW);
    delay(500);}
}

So it appears that nothing was wrong with the #define command. My question is: Why did the arduino IDE mark line 2 red and gave me that error message? And why only line 2 and not 1 which has the same command?

Try it by coping the code!

Timon

This is missing ( ) on the digitalRead
if(digitalRead but == LOW && digitalRead relay1 == LOW){
which you fixed in the 2nd code listing
if(digitalRead (but) == LOW && digitalRead (relay1) == LOW){
and was likely the problem the whole time.

timon989:
Why did the arduino IDE mark line 2 red and gave me that error message?

The parser encounters an error when it gets to ...

digitalRead 11
            ^--

... there. ('11' has been substituted for 'but')

The offending part is the result of a macro expansion.
It does not literally occur in your code like that.
So it points you to the macro that was expanded into the code that produced the parsing error.

timon989:
My question is: Why did the arduino IDE mark line 2 red and gave me that error message? And why only line 2 and not 1 which has the same command?

The code compiled by the Arduino-IDE is NOT identically to the code you write in the editor. The Arduino-IDE will also do some "Arduino magic" invisibly in the background. Similar to the pre-compiler that handles #define and #include statements. Unfortunately the "pre-compiler magic" and the "Arduino magic" can conflict with each other, so that the line numbering in case of errors will not be correct.

To make sure you see relevant error messages, you BETTER NOT #define your button numbers, but make them 'const' variables.

So DO NOT use:

#define relay1 7
#define but 11

BUT USE INSTEAD:

const byte relay1= 7;
const byte but= 11;

This works much better with all the invisible "Arduino magic" done in the background and keep the error messages meaningful when compiling code which contains mistakes that will create compile errors.