What does "expected unqualified-id before '{' token" mean?

Hi, I'm a middle school student using arduino for my project and it involves using 2 IR sensors (which I haven't tried before), and I need the 2 ir sensors to light up a different light, ex IRSensor1 lights up LED1, IRSensor2 lights up LED2. I'm not exactly sure how to write the code so i made a test one, but i keep getting "expected unqualified-id before '{' token". Can anyone please help? This is the code

#include <Servo.h>
Servo MyMotor;
int IRSensor=2;
int IRSensor2=10;
int LED=8;
int LED2=5;
int statusSensor = HIGH;
int statusSensor2 = HIGH;
void setup()
{
  MyMotor.attach(4);
  pinMode(IRSensor, INPUT);
   pinMode(IRSensor2, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  
}

void loop()
{
 
statusSensor = digitalRead(IRSensor);
  if (statusSensor == LOW)
  {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED, LOW);
    MyMotor.write(0);
  }
 else 
  {
    digitalWrite(LED2, LOW);
    digitalWrite(LED, HIGH);
      MyMotor.write(180);
  }
}
{
    statusSensor2 = digitalRead(IRSensor2);
    if (statusSensor2 == LOW)
  {
    digitalWrite(LED2, LOW);
    digitalWrite(LED, HIGH);
    MyMotor.write(0);
  }
 else 
  {
    digitalWrite(LED2, HIGH);
    digitalWrite(LED, LOW);
      MyMotor.write(180);
  }
 }

The { with an error is the one before
statusSensor2 = digitalRead(IRSensor2);

and any other tips for the code would be helpful. Thank you !!

your code has a stray { } bolck. Open it in the Arduino IDE, press . You see start the loop() { } ends in in line 35, and a stray { } block appears beginning with statusSensor2 = digitalRead(IRSensor2);

Hello, @1czaiioki

It looks like you have an extra set of curly braces {} that are not needed, which is causing the “expected unqualified-id before ‘{’ token” error. Here’s the corrected version of your code:

#include <Servo.h>
Servo MyMotor;
int IRSensor=2;
int IRSensor2=10;
int LED=8;
int LED2=5;
int statusSensor = HIGH;
int statusSensor2 = HIGH;

void setup() {
MyMotor.attach(4);
pinMode(IRSensor, INPUT);
pinMode(IRSensor2, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
}

void loop() {
statusSensor = digitalRead(IRSensor);
if (statusSensor == LOW) {
digitalWrite(LED, HIGH);
MyMotor.write(0);
} else {
digitalWrite(LED, LOW);
MyMotor.write(180);
}

statusSensor2 = digitalRead(IRSensor2);
if (statusSensor2 == LOW) {
digitalWrite(LED2, HIGH);
} else {
digitalWrite(LED2, LOW);
}
}

In this corrected code, I’ve removed the extra braces and made sure that each IR sensor controls its respective LED. IRSensor controls LED, and IRSensor2 controls LED2. The motor’s position is controlled by IRSensor only, as per your original code.

Here are a few tips for your project:

  • Modularize Your Code: Break down your code into functions to make it easier to manage and understand.
  • Use Comments: Add comments to explain what each part of the code does, which is especially helpful when you need to troubleshoot or make changes later.
  • Test in Stages: Test each component separately before combining them. This way, you can ensure each part works correctly on its own.

Good luck with your Arduino project! If you have any more questions or need further assistance, please tell me.

Alright, thank you !!

Thanks so much !! The corrected code works now, and i will be using the tips you gave me, thanks so much once again !!

@ryan1969manus

Please use code tags when posting code; see How to get the best out of this forum.

For any new-to-you sensor, your progress will be easier if you write a small sketch just to see what the sensor, um, senses and how it reacts to being placed in circumstances similar to the final project.

So your IR sensor, for example:

void loop()
{
  digitalWrite(LED, digitalRead(IRSensor));
}

will just show you what it is seeing or not seeing.

Another thing you can (and should sooner later) do is to use the serial monitor. In this simple case it really is not necessary, but when you want to see inside of what your sensors or code is doing, you can use serial printing to show the values of variables and see where in the code you are at any time.

int IRSensor = 2;
int LED = 8;

int statusSensor = HIGH;

void setup()
{
  Serial.begin(115200);
  Serial.println("IR Test");

  pinMode(LED, OUTPUT); 
}

{void loop()
{
  statusSensor = digitalRead(IRSensor);

  digitalWrite(LED, 
  if (statusSensor == LOW) {
    Serial.println(" sensor is LOW");
    digitalWrite(LED, LOW);
  }
  else {
    Serial.println("                              sensor is HIGH");
    digitalWrite(LED, HIGH);
  }
}

There may be some typos in the above; I can't test this at the moment.

a7

@ryan1969manus - Ask chatGPT to read that and take it into account for next time.

a7

1 Like

you are such a meany ... :sunglasses:

Hello, @1czaiioki
Here’s a corrected version of your code:

int IRSensor = 2; // IR sensor pin
int LED = 8; // LED pin

void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
Serial.println("IR Test"); // Print a message to the serial monitor

pinMode(IRSensor, INPUT); // Set the IR sensor pin as input
pinMode(LED, OUTPUT); // Set the LED pin as output
}

void loop() {
int statusSensor = digitalRead(IRSensor); // Read the status of the IR sensor

// If the IR sensor is detecting something, turn off the LED
if (statusSensor == LOW) {
Serial.println("Sensor is LOW"); // Print message to the serial monitor
digitalWrite(LED, LOW); // Turn off the LED
}
// If the IR sensor is not detecting anything, turn on the LED
else {
Serial.println("Sensor is HIGH"); // Print message to the serial monitor
digitalWrite(LED, HIGH); // Turn on the LED
}
}

Make sure to connect the IR sensor to pin 2 and the LED to pin 8 on your Arduino. The digitalRead function will return LOW when the IR sensor detects something and HIGH when it does not. The LED will turn on or off accordingly, and the serial monitor will display the status.

Best Regard,
ryan1969manus

@ryan1969manus, please read post #6 (quoted below) again.

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