Hello everyone. I am fairly new to programming in C++ and I have been working for a few weeks on trying to build a car that will run through a code uploaded to an Arduino. I was told that I need to have a sensor, a servo motor, and 2 sets of stepper motors be a part of it. Well individualy I have gotten each code to work. However when I try to put them all together I am getting an error message. Right now I have the code setup to where the Servo motor will first turn on and then the HC-SR04 sensor will activate.
My code for these 2 programs is as follows:
//Turn on the Servo motor to begin rotating
//and then surn on the sensor
#include <Servo.h>;
Servo myservo; //create servo object to control a servo
//int angle = 0; //variable to store the servo position
const int ledPin = 13;
int cm;
void setup()
{
myservo.attach(7);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
{
{
void SendA(); //call SendA subroutine
delay(120); //pause 120 ms
}
delay(200); //pause 200 ms
Serial.println("a"); //This is to verify that the code returns from SendA
{
void SendB(); //call SendB suboutine
delay(120); // pause 120 ms
}
delay(200); //pause 200 ms
Serial.println("b"); //This is to verify that the code returns from SendB
}
//Declare the SendA Subroutine with no parameters
//Servo myservo; //create servo object to control a servo
int angle = 0; //variable to store the servo position
//myservo.attach(7);
//Serial.begin(9600);
Serial.println("SendA"); //This is to verify that the code returns from SendA
{
for(angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for(angle = 180; angle >= 1; angle -=1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
}
}
{
//Declare the SendB subroutine with no parameters
Serial.println("SendB"); // This is to verify that the code returns from SendA
const int PingPin = 12;
const int ledPin = 13;
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin);
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds);
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
However, when I try to verify it I get this error message:
Maybe_Potentially.ino:61:2: error: expected unqualified-id before ‘{’ token
Error compiling.
Now I have been trying to figure what could cause this error but I’m not finding any results and was hoping maybe posting to a forum could help. If anyone sees something im missing please let me know as that would be a huge help
I recommend running the IDE's auto format tool on your code, getting rid of the extraneous braces, and calling the functions, not merely supplying prototypes for the functions.
//Turn on the Servo motor to begin rotating
//and then surn on the sensor
#include <Servo.h>;
Servo myservo; //create servo object to control a servo
//int angle = 0; //variable to store the servo position
const int ledPin = 13;
int cm;
void setup()
{
myservo.attach(7);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
{
{
void SendA(); //call SendA subroutine
delay(120); //pause 120 ms
}
delay(200); //pause 200 ms
Serial.println("a"); //This is to verify that the code returns from SendA
{
void SendB(); //call SendB suboutine
delay(120); // pause 120 ms
}
delay(200); //pause 200 ms
Serial.println("b"); //This is to verify that the code returns from SendB
}
//Declare the SendA Subroutine with no parameters
//Servo myservo; //create servo object to control a servo
int angle = 0; //variable to store the servo position
//myservo.attach(7);
//Serial.begin(9600);
Serial.println("SendA"); //This is to verify that the code returns from SendA
{
for (angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
}
}
{
//Declare the SendB subroutine with no parameters
Serial.println("SendB"); // This is to verify that the code returns from SendA
const int PingPin = 12;
const int ledPin = 13;
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin);
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds);
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
I noticed if I delete some of the braces then I get more error messages. And something I never understood was how to properly call a function to do what I needed.
//Turn on the Servo motor to begin rotating
//and then surn on the sensor
#include <Servo.h>;
Servo myservo; //create servo object to control a servo
//int angle = 0; //variable to store the servo position
const int ledPin = 13;
int cm;
void setup()
{
myservo.attach(7);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
//Declare the SendA Subroutine with no parameters
//Servo myservo; //create servo object to control a servo
int angle = 0; //variable to store the servo position
//myservo.attach(7);
//Serial.begin(9600);
Serial.println("SendA"); //This is to verify that the code returns from SendA
{
for (angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
}
{
//Declare the SendB subroutine with no parameters
Serial.println("SendB"); // This is to verify that the code returns from SendA
const int PingPin = 12;
const int ledPin = 13;
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin);
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds);
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Maybe_Potentially_3-24.ino:25:3: error: expected initializer before ‘int’
Maybe_Potentially_3-24.ino:28:3: error: ‘Serial’ does not name a type
Maybe_Potentially_3-24.ino:29:3: error: expected unqualified-id before ‘{’ token
void loop(){
//Declare the SendA Subroutine with no parameters
//Servo myservo; //create servo object to control a servo
int angle = 0; //variable to store the servo position
//myservo.attach(7);
//Serial.begin(9600);
Serial.println("SendA"); //This is to verify that the code returns from SendA
{
for (angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
}
}
{
//Declare the SendB subroutine with no parameters
Serial.println("SendB"); // This is to verify that the code returns from SendA
const int PingPin = 12;
const int ledPin = 13;
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin);
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds);
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Maybe_Potentially_3-24.ino:44:1: error: expected unqualified-id before ‘{’ token
Ok so now that I have everything cleaned up, why is it still giving me this error? Is there something I’ve done wrong?
void loop(){
//Declare the SendA Subroutine with no parameters
//Servo myservo; //create servo object to control a servo
int angle = 0; //variable to store the servo position
//myservo.attach(7);
//Serial.begin(9600);
Serial.println("SendA"); //This is to verify that the code returns from SendA
for (angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
}
{
//Declare the SendB subroutine with no parameters
Serial.println("SendB"); // This is to verify that the code returns from SendA
const int PingPin = 12;
const int ledPin = 13;
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin);
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds);
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Same error as before. And I have looked at the IDE and its not really helping. Now this code is partly based around this code
/* Ping))) Sensor
* prints distance and changes LED flash rate
* depending on distance from the Ping))) sensor
*/
const int PingPin = 12;
const int ledPin = 13; //pin connected to LED
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin)
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
} <--- loop() ends here
Ok. I got to thinking that maybe Im approaching this all wrong. Basically I am totally lost at how to combine all these different programs together. Basically how I want the car to run is as follows:
Car
From starting in a neutral position
{Turn on the servo motor to determine location,
Then turn on the sensor to detect obstacles
Turn on the stepper motors to move foward
If obstacles= o<----- if <-----Main loop
move foward()
else
stay()
}
move foward()
{
Move right wheel forward <------Subroutine move foward
Move left wheel forward
}
(All while still in the main loop of the sensor determining if there are obstacles or not)
And I need this to continuously check so the car can move on its own while being supplied power from a battery pack. And as stated earlier I am completely lost about how I would combine all the programs together to make them work. I thought it would be something as simple as copy and paste each one, but clearly I was mistaken. So any help would be greatly appreciated.
I don't know how you can decide your approach is wrong when you haven't even tested the current approach.
Take some time out to study the examples provided with the IDE, and see if you can get your current code to compile, then at least you'll have a basis for further testing.
I just feel like I am getting it wrong because every time I try to fix one of the errors you have brought up, another one seems to replace it. But im trying to essentially rewrite it to see if maybe I will discover something.
This is your code from your earlier post, corrected and made to compile.
I don’t know if it will do what you want, but I suggest you spend some time comparing it to what you had earlier and see if you can spot your mistakes.
#include <Servo.h>
const int PingPin = 12;
const int ledPin = 13;
Servo myservo;
void setup ()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
myservo.attach(7);
}
void loop()
{
int angle = 0; //variable to store the servo position
Serial.println("SendA"); //This is to verify that the code returns from SendA
for (angle = 0; angle < 180; angle += 1); //goes from 0 degrees to 180 degrees
{
myservo.write(angle); //tell servo to go to position in variable 'angle'
delay(10); // wait 10 ms between servo commands
}
for (angle = 180; angle >= 1; angle -= 1) //goes from 180 to 0 degrees
{
myservo.write(angle); //move servo in opposite direction
delay(10); // waits 10 ms between servo commands
}
Serial.println("SendB"); // This is to verify that the code returns from SendA
int cm = ping(12) ;
Serial.println(cm);
digitalWrite(13, HIGH);
delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
digitalWrite(ledPin, LOW);
delay( cm * 10);
//return cm;
}
//following code based on http://www.arduino.cc/en/Tutorial/Ping
// returns the distance in cm
int ping(int pingPin)
{
//establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
I did notice some of the differences but I do have a quick question. I am running the code now and the Ultrasonic Sensor is returning no value except for 0.