So, I'm in this class, and I need to make an Arduino project, and I got a problem with coding, I always try doing it again and again, but it just wont work, and an error comes up. basically, what my project is, is contains a ultrasonic sensor, 2 servos, and nothing else. ITS JUST SO IRRITATING. my project is 'used' for security reasons, here is how it will work, (if I fix this code); a ultra sonic sensor, scans the area around it, and it is placed on top of a platform that a servo rotates(servo1) and when an object is closer than 300 centimeters (3 m), it will turn on a servo(servo2) that makes a platform fall and make noise.
Any further or do, here's the code (btw I'm 14, and I'm new to Arduino)
#define ECHOPIN 7 #define TRIGPIN 8
#include <Servo.h>
Servo myservo;
int pos = 0; //
void setup() {
Serial.begin(9600);
** myservo.attach(9);**
** pinMode(7,OUTPUT);**
** pinMode(8,INPUT);**
** pinMode(ECHOPIN, INPUT);**
** pinMode(TRIGPIN, OUTPUT);**
}
void Print (int R , int T)
{
** Serial.print(R);Serial.print(", ");**
** Serial.print(T);Serial.println(".");**
** delay(100);**
}
float Distance () {
** digitalWrite(TRIGPIN, LOW);**
** delayMicroseconds(2);**
** digitalWrite(TRIGPIN, HIGH);**
** delayMicroseconds(10);**
** digitalWrite(TRIGPIN, LOW);**
** // Distance Calculation**
** float distance = pulseIn(ECHOPIN, HIGH);**
** distance= distance/58;**
** return(distance);**
}
void loop() {
** myservo.write(45);**
** delay(400);**
** for(pos = 45; pos <= 135; pos += 7)**
** { // in steps of 1 degree**
** myservo.write(pos); **
** Print(Distance() , pos); **
** delay(10);**
** }**
** delay(400);**
** for(pos = 135; pos>=45; pos-=7)**
** {**
** myservo.write(pos); Print(Distance() , pos); **
** delay(10);**
** } **
** if (distance < 300) { **
** myservo.attach(3);**
** }**
** myservo.write(45);**
** delay(400);**
;
** for(pos = 45; pos <= 135; pos += 35)**
** {**
** myservo.write(pos);**
** Print(Distance() , pos); **
** delay(10);**
** }**
** delay(400);**
** for(pos = 135; pos>=45; pos-=35)**
** {**
** myservo.write(pos);**
** Print(Distance() , pos); **
** delay(10);**
** );**
}
** else {**
** Serial.print(distance);**
** Serial.println(" cm");**
** }**
** if (distance >= 200 || distance <= 0){**
** Serial.println("Out of range");**
** }**
** else {**
** Serial.print(distance);**
** Serial.println(" cm");**
** }**
** delay(500);**
}
And here's the error :
Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"
aaa.ino: In function 'void loop()':
aaa:64: error: 'distance' was not declared in this scope
aaa:89: error: expected primary-expression before ')' token
aaa:89: error: expected ';' before ')' token
aaa:91: error: 'else' without a previous 'if'
aaa:92: error: 'distance' was not declared in this scope
aaa:95: error: 'distance' was not declared in this scope
'distance' was not declared in this scope
I would like if someone could help , figuring out this problem , either by fixing it or telling me the problem. Anyway thanks