Expected unqualified ID before 'if'

This is the code:

#include <Servo.h>
Servo myservo;
int pos;
int Signal = 8;

#define LED_B 4
#define LED_R 5

void setup() {
pinMode(Signal, INPUT);
myservo.attach(9);
pinMode(LED_B, OUTPUT);
pinMode(LED_R, OUTPUT);
}
void loop() {
int buttonState = digitalRead(Signal);
delay(1);
if (buttonState == 0) { for ( ;pos >= 50; pos -= 1) { myservo.write(pos);
delay(15); } } else { for ( ;pos <= 125; pos += 1) { myservo.write(pos);
delay(5); } } }

if (buttonstate == 0)
{ digitalWrite(LED_R, HIGH);
}

else { digitalWrite(LED_B, HIGH);
}

this is the error message:
Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: “Arduino Uno”

Infrared_sensor_door_lock_using_servo_motor:23:2: error: expected unqualified-id before ‘if’

if (buttonstate == 0)

^~

Infrared_sensor_door_lock_using_servo_motor:27:2: error: expected unqualified-id before ‘else’

else { digitalWrite(LED_B, HIGH);

^~~~

exit status 1

expected unqualified-id before ‘if’

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

im a bit new to the coding world. PLS tell if I have done a mistake :confused:

    if (buttonState == 0) { for ( ;pos >= 50; pos -= 1) { myservo.write(pos);

That for looks bad to me; From what value will pos go?

    if (buttonState == 0) { for ( pos = xxx; pos >= 50; pos -= 1) { myservo.write(pos);

Where xxx is a valuet > 50;

Here is your code Auto formatted in the IDE
Note the use of code tags when posting it here

#include <Servo.h>
Servo myservo;
int pos;
int Signal = 8;

#define LED_B 4
#define LED_R 5


void setup()
{
  pinMode(Signal, INPUT);
  myservo.attach(9);
  pinMode(LED_B, OUTPUT);
  pinMode(LED_R, OUTPUT);
}
void loop()
{
  int buttonState = digitalRead(Signal);
  delay(1);
  if (buttonState == 0)
  {
    for ( ; pos >= 50; pos -= 1)
    {
      myservo.write(pos);
      delay(15);
    }
  }
  else
  {
    for ( ; pos <= 125; pos += 1)
    {
      myservo.write(pos);
      delay(5);
    }
  }
}

if (buttonstate == 0)
{
  digitalWrite(LED_R, HIGH);
}

else
{
  digitalWrite(LED_B, HIGH);
}

See how the formatting reveals that the line reported as being in error

if (buttonstate == 0)

is not inside a function, hence the error
Check your pairs of { and } to make sure that they are balanced and in the right place in the code

One too many } just above the "if"?

ok thank u so much ;).

i made this code: earlier today:

#include<Servo.h>

Servo servo;

int sensor;

int LED_RED;

void setup() {

servo.attach(8);//CONNECT SERVO PIN TO 8

pinMode(A1, INPUT);

pinMode(13, OUTPUT);//LED

pinMode(9, OUTPUT);//LED_RED

}

void loop() {

sensor = digitalRead(A1);

if(sensor == LOW){

servo.write(180);//180 DEGREES

digitalWrite(13, HIGH);//LED

digitalWrite(9, LOW);//LED_RED

}

else{

servo.write(0);//0 DEGREES

digitalWrite(13, LOW);//LED

digitalWrite(9, HIGH);//LED_RED

}

}

my task here was to move the servo when the IR sensor registered the movement with LED’s and it works like a charm… Thank you :smiley:

No code tags used and I see a smiley !

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

OK, you have made 20 posts so far.

Well beyond time you read the instructions for posting so now you need to go back and modify your original posts (not re-post them) - using the “More → Modify” option below the right hand corner of each post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. While you were lucky so far, If you do not post it as “code” it can as you now see, be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.