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);
}