I'm getting an error message on my void loop saying a function definition is not allowed here before '{' token.
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TriggerPin A0
#define EchoPin A1
#define MAX_DISTANCE 200
NewPing sonar(TriggerPin, EchoPin, MAX_DISTANCE);
int distance = 100;
int readPing() {
delay(70);
int cm = sonar.ping_cm();
Servo myservo1;
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(0);
delay(400);
myservo2.write(0);
delay(400);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (distance <= 10)
{
Serial.print("distance is less than or equal to");
myservo1.write(90);
delay(400);
myservo1.write(100);
delay(400);
myservo2.write(90);
delay(400);
myservo2.write(100);
delay(400);
}
else {
myservo1.write(80);
delay(400);
myservo1.write(90);
delay(400);
myservo2.write(80);
delay(400);
myservo2.write(90);
delay(400);
}
if(cm==0)
{
cm = 250;
}
}
Your readPing() function needs a closing }.
If you use the autoformat and/or indent your code properly you can catch these errors easily.
You need to assign a value to the variable distance in loop instead of just printing it.
the variable cm and is declared in readPing() and is out of scope in loop(). Declare cm global.
Ok now I'm getting an error compiling in Arduino Uno board but its not hooked up
The compiler doesn't care if you're connected to the board or not. What is the error?
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
When I try to verify the error says exit status 1 Error compiling for Arduino Uno
That's what it says after the actual useful error message(s).
post the code that generates the error. Please, always, post the latest version of the code after you make changes so that we can keep up. Put it in a new post, do not overwrite old posts.
Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
I already told everyone what the message said word for word and here's my code
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TriggerPin A0
#define EchoPin A1
#define MAX_DISTANCE 200
NewPing sonar(TriggerPin, EchoPin, MAX_DISTANCE);
int distance = 100;
int readPing();
int cm = sonar.ping_cm();
Servo myservo1;
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(0);
delay(400);
myservo2.write(0);
delay(400);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
distance = 100;
if (distance <= 10)
{
Serial.print("distance is less than or equal to");
myservo1.write(90);
delay(400);
myservo1.write(100);
delay(400);
myservo2.write(90);
delay(400);
myservo2.write(100);
delay(400);
}
else {
myservo1.write(80);
delay(400);
myservo1.write(90);
delay(400);
myservo2.write(80);
delay(400);
myservo2.write(90);
delay(400);
}
if (cm == 0)
{
cm = 250;
}
}
What happened to your readPing() function? The compiler is confused because you don't have a readPing() function.
No, you only posted part of the error messages.
The least useful part.
Your readPing function definition was written improperly and the cm variable was still only local to the readPin function. I fixed those things and the code compiles with no warnings or errors for me.
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TriggerPin A0
#define EchoPin A1
#define MAX_DISTANCE 200
NewPing sonar(TriggerPin, EchoPin, MAX_DISTANCE);
int distance = 100;
int cm = 0; // ********** declare cm in global scope
// **************** fix the readPing function definition
int readPing()
{
cm = sonar.ping_cm();
return cm;
}
Servo myservo1;
Servo myservo2;
void setup()
{
Serial.begin(9600);
myservo1.attach(9);
myservo2.attach(10);
myservo1.write(0);
delay(400);
myservo2.write(0);
delay(400);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop()
{
distance = 100;
if (distance <= 10)
{
Serial.print("distance is less than or equal to");
myservo1.write(90);
delay(400);
myservo1.write(100);
delay(400);
myservo2.write(90);
delay(400);
myservo2.write(100);
delay(400);
}
else
{
myservo1.write(80);
delay(400);
myservo1.write(90);
delay(400);
myservo2.write(80);
delay(400);
myservo2.write(90);
delay(400);
}
if (cm == 0)
{
cm = 250;
}
}
Though I fail to see the usefulness of the code,
Distance will never be less than or equal to 10.
Still kind of silly though...
Why not simply
int readPing()
{
return sonar.ping_cm();
}
Why even bother with readPing at all?
I don't know just thought it would work