Help me debug my code please, im new to this stuff

i just wrote on a code for my arduino project but i keep getting errors after errors. Can anyone help me decode it. The code if two diferent circuit, in this case the servos with the pot and the sensor with the led and buzzer. I want them to work simultanueosly at th same time not one work after the other. Any help would be gratefull

code

#include <Servo.h> 
#define trigPin 6
#define echoPin 7
#define led1 1
#define led2 2
#define led3 4
#define led4 8 
#define buzzer 12

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin = 0; // analog pin used to connect the potentiometer
int potpin1 =1;
int potpin2 =2;
int potpin3 =3;
int potpin4 =4;
int val; // variable to read the value from the analog pin

int sound = 250;


void setup() {
 Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
   pinMode(led3, OUTPUT);
   pinMode(led4, OUTPUT);
  pinMode(buzzer, OUTPUT);

myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(9);
myservo4.attach(10);
myservo5.attach(11);


}

void loop() {
 //servo 1 analog pin 0
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there

val = analogRead(potpin1); // servo 2 analog pin 1
val = map(val, 0, 1023, 0, 179);
myservo2.write(val);
delay(15);

val = analogRead(potpin2); // servo 3 analog pin 2
val = map(val, 0, 1023, 0, 179);
myservo3.write(val);
delay(15);

val = analogRead(potpin3); // servo 4 analog pin 3
val = map(val, 0, 1023, 0, 179);
myservo4.write(val);
delay(15);

val = analogRead(potpin4); // servo 5 analog pin 4
val = map(val, 0, 1023, 0, 179);
myservo5.write(val);
delay(15);

 long duration, distance;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;


 if (distance <= 20) {
   digitalWrite(led, HIGH);
    digitalWrite(led2, HIGH);
       
}
 else {
   digitalWrite(led,LOW);
   digitalWrite(led2, LOW);
   
 } 
 if (distance > 20|| distance <= 0){
   Serial.println("Out of range");
   noTone(buzzer);
   noTone(buzzer1);
 }
 else {
   Serial.print(distance);
   Serial.println(" cm");
   tone(buzzer, 400); // play 400 Hz tone for 500 ms
   tone(buzzer1, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(buzzer, 800); // play 800Hz tone for 500ms
tone(buzzer1, 800); // play 800Hz tone for 500ms
delay(500);
tone(buzzer, 400); // play 400 Hz tone for 500 ms
tone(buzzer1, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(buzzer, 800); // play 800Hz tone for 500ms
tone(buzzer1, 800); // play 800Hz tone for 500ms
delay(500);
tone(buzzer, 400); // play 400 Hz tone for 500 ms
tone(buzzer1, 400); // play 400 Hz tone for 500 ms
delay(500);
tone(buzzer, 800); // play 800Hz tone for 500ms
tone(buzzer, 800); // play 800Hz tone for 500ms
delay(500);

}
delay(300);
}

Hello,

This is the first error:

sketch_aug29a.ino:73:15: error: expected constructor, destructor, or type conversion before '(' token

So the first thing to do is look at line 73. No apparent problem on this line, so look at the lines above it.

Line 71, you have a "}", and the corresponding "{" is on line 45: "void loop() {". So the code after line 71, is outside of the loop() function.

Fix this problem first, then you will have some other errors remaining, but they will be easy to solve.

@swizzy555: Welcome to the forum.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Read this before posting a programming question

thanks to guix for you help, but would the two circuit work at the same time without interfering with one another like for esxample, one would work first then the stop then the other would continue

I just realized you have been double posting. Your other Thread is about the same code. Don't double post. I will suggest to the moderator to merge them.

...R

Which kind of Arduino ?

If I try to compile your sketch for Arduino UNO, I receive 4 errors:

sketch_aug29a.ino: In function 'void loop()':
sketch_aug29a:84: error: 'led' was not declared in this scope
sketch_aug29a:89: error: 'led' was not declared in this scope
sketch_aug29a:96: error: 'buzzer1' was not declared in this scope
sketch_aug29a:102: error: 'buzzer1' was not declared in this scope

Do you also receive compiler errors ?

#define led1 1

Pins 0 and 1 are used by Serial on most boards.

The microseconds clock has a granularity of 4.

You want a loop or sequence of NOP (assembly commands) to wait out 2 usecs.

Those delays can all vanish in an afternoon.

I am using arduino uno i want the arduino to multitask in the void loop secion but in a way that one task doesnt affect the other

Welcome to Arduino land.

Robin 2 has a great discussion on doing multiple things:

swizzy555:
I am using arduino uno i want the arduino to multitask in the void loop secion but in a way that one task doesnt affect the other

swizzy555:
I am using arduino uno i want the arduino to multitask in the void loop secion but in a way that one task doesnt affect the other

Everything that has a delay() (as opposed to delayMicroseconds()) before it needs to be put inside of:

if ( thisPartsWaitIntervalMs > 0 ) // this part only runs when another part sets its wait time else is off
{
  if ( millis() - thisPartsStartToWaitMs >= thisPartsWaitIntervalMs ) // check if time is up
  {
   // do the thing that needs to wait

   // and if needed, set the next part's start time to "now" millis() and wait time to the next delay
  }
}

or like:

if ( aProcessStateThisPartChecks == thisPartsTriggerValue )
{
    // same state variable can control many related parts, think state = step to do
   // do the thing that should run when the state is the right one for this part

   // and if needed, set the next part's timers/triggers
}

Then only the needed parts will run on any one pass through loop and NONE of them holds everything up while waiting for a delay() to finish.

1 millisecond is 16000 cycles. A delay(1) in loop() can easily waste more than 90% of loop() run time.[/code]