Error, expected constructor, destructor, or type conversion before '(' token

Im new to Arduino, haven't really learn much about it but my college final year's project is coming and you are very unlucky to be in a shit college that the lecturer did not teach you anything about Arduino but expect you to include Arduino in your project. So here's my coding...and I do not know what is wrong. Please do list out all my mistake for me thanks.

int irmotionPin = 4; // Pin of IR Motion Sensor
int relayPin = 8; // Pin of Relay Module

void setup(){
Serial.begin(9600);
pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
digitalWrite(relayPin, LOW); // Set Pin to LOW to turn Relay OFF
}

void loop(){

while (digitalRead(irmotionPin) == HIGH); // If Motion detected
digitalWrite(relayPin, HIGH); // Turn Relay ON
Serial.println("Relay is OFF");
delay(500);
}

digitalWrite(relayPin, LOW); // Turn Relay OFF
Serial.println("Relay is OFF");
delay(500);
}

and my error message is this :

Arduino: 1.8.3 (Windows 10), Board: "Arduino/Genuino Uno"

Sketch_project_1_lvl_3:18: error: expected constructor, destructor, or type conversion before '(' token

digitalWrite(relayPin, LOW); // Turn Relay OFF

^

Sketch_project_1_lvl_3:19: error: 'Serial' does not name a type

Serial.println("Relay is OFF");

^

Sketch_project_1_lvl_3:20: error: expected constructor, destructor, or type conversion before '(' token

delay(500);

^

Sketch_project_1_lvl_3:21: error: expected declaration before '}' token

}

^

exit status 1
expected constructor, destructor, or type conversion before '(' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks..

Please edit you post to include code-tags. See How to use the forum. That way it's readable.

To the problem, there is NOTHING in the while() (nor should you use a while, use a if :wink: ) So the bracket at line 16 is ending your loop() putting everything after that outside any function which isn't allowed.

Quick tip, if you set the cursor next to a bracket it will highlight the corresponding bracket.

Thanks for the advise I read the How to use forum, I will do it the proper way next time. Back to the answer I still don't understand what you mean by there's NOTHING in while() .. it keeps highlighting my last sentence delay(500) saying expected constructor etc. Can you help me correct my coding and repost for me that would save some time explaining, I seriously have no idea at all now.

Go back and edit this post as well :wink:

See the reference for while(). Just because you indented the code after while, it's not linked to while. You just ended the whole while() by putting a ; at the end of the line. What is the closing bracket for the bracket on line 16? And on line 21?

And again, no need for a while-loop. The loop() is already looping, let it do it's job of looping and just use a if.

See the <<< comments I added, adds to the earlier replies:

int irmotionPin = 4; // Pin of IR Motion Sensor
int relayPin = 8; // Pin of Relay Module

void setup(){
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
  digitalWrite(relayPin, LOW); // Set Pin to LOW to turn Relay OFF
 }

void loop(){

  while (digitalRead(irmotionPin) == HIGH){ // If Motion detected << needed { vs ;
       digitalWrite(relayPin, HIGH);  // Turn Relay ON
       Serial.println("Relay is OFF");
       delay(500);
}  << otherwise this } closes out loop(), and the code that follows is not part of anything and causes the compile error
       
       digitalWrite(relayPin, LOW);  // Turn Relay OFF
       Serial.println("Relay is OFF");
       delay(500);
}

Your Serial.prints are not clear - when is the relay really off?

Thanks a lot for the write back. Let me be clear, I wanted the Relay to really turned off when there's no motion detected after a 5 seconds delay, meaning when a person walks into the room motion was detected and the relay turned ON, after 5 seconds and no motion is detected ( the person might sit down doing nothing ) then the Relay will turned off....

You are not doing that...

Written correct, with while() or if(), you ALWAYS turn of the relay, no matter the state of the pin. I think the easiest way is to check the state change example. Turn on the output when the pin BECOMES high (which is not the same as is high). When the pin BECOMES low you save that time and wait untill 5 seconds have past. Only then you turn off the output. For the last part I would make use of millis() because that makes it possible to do other stuff with the Arduino in the mean time where as delay() does not.

And I assume the 500ms is just a typo for 5000ms because that's 5 seconds :wink:

Im sorry but like I said above I really have no idea what ‘while()’ and ‘if()’ is at all… What is the actual problem ? I attached a screenshot of it below, pls do help me recorrect my code to the way I explained it on my previous message. Thanks

Juanlau:
I really have no idea what 'while()' and 'if()' is at all....

Then go and look it up :slight_smile:

Juanlau:
What is the actual problem?

That your code doesn't do what you tell us you want it to do. Fine by me, but I think it strange if you go like "Meh, close enough" while it's doing something else...

Juanlau:
I attached a screenshot of it below, pls do help me recorrect my code to the way I explained it on my previous message. Thanks

A screenshot of code is the most useless thing of the day. And no, we are here to help you learn, not to write code for you. We do have a section where you can ask for that but the people there expect some sort of legal tender in return.

And a hint on the error, every opening bracket { needs a closing bracket }. :wink:

Ps Small tip, turn on line numbers in the settings

@Rest Damn guys, why isn't this on by default?!

Alright some of the replies to explain a little for me, thanks for the small tips I will try to re do it again. Thanks a lot for everyone's time tho. :slight_smile: