Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get the error "52:1: error: expected unqualified-id before '{' token". I'm using tinkercad to program my project before I assemble it. This code is for a PIR sensor and a buzzer combined with the coding for a ultrasonic sensor with a buzzer. I'm very new to coding I've never done it before so I used a two YouTube videos to help me with the circuit and the coding. the error is the "{" above the line "//Clears the trigPin"

int pinSensor =2;
int pinLed =12;
int pinBuzzer =13;
int pirSensor =0;

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup()
{
  pinMode(pinSensor, INPUT);
  pinMode(pinLed, OUTPUT);
  pinMode(pinBuzzer, OUTPUT);

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop()
{

  pirSensor = digitalRead(pinSensor);
  if (pirSensor == HIGH)
  {

    digitalWrite(pinLed, HIGH);
    tone(pinBuzzer, 1000, 500);

  }

  else {

    digitalWrite(pinLed, LOW);
  }

  delay(10);
}

{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}


What I have tried:

I tried searching on how to combine two codes
Posted
Updated 13-Oct-21 4:46am
v3

1 solution

You need to put all your code into your loop method or break it up.

Look at:
  delay(10);
}

{
// Clears the trigPin


The } after delay closes your loop method and then you have an open bracket and more code. It either needs to be part of loop or a separate method.
 
Share this answer
 
Comments
[no name] 12-Oct-21 10:48am    
THANK YOU XD :)
Member 15329613 12-Oct-21 11:35am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900