Expected initializer before 'void'

Hello, I'm trying to make robot and getting the expected initializer before 'void' error when I try to upload.

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int(motor1pin2)

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
 safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(motor1pin2, LOW);
  digitalWrite(motor1pin1, HIGH);
}
else{
  digitalWrite(motor1pin2, HIGH);
  digitalWrite(motor1pin1, LOW);

  digitalWrite(motor2pin2, LOW);
  digitalWrite(motor2pin1, HIGH);
  delay(1000);
}

Incomplete code.

You are missing setup(), and the braces in the loop function are not matched. Some pin definitions are missing.

Are you missing ; something ?

1 Like

why not start with just trying to read the distance sensor and printing the value on the Serial monitor?

then add some code the prints something (e.g. "stop") when the distance is close?

then add code to run the motor if not close and stop the motor if close?

looks like you're trying to turn the robot by running each motor in opposite directions, but i don't see any code to stop turning or even stop both motors

No it tells the robot to go backward because of motor polarity.

looks like you're have 2 motors. do both motors need to be reversed?

I added mor pin definitions and now I'm getting exit status 1
expected constructor, destructor, or type conversion before 'int' on the const int trigPin = 9;

// defines pins numbers
setup()
const int trigPin = 9;
const int echoPin = 10;
const int(motor1pin2)
const int(motor1pin1)
const int(motor2pin1)
const int(motor2pin2)

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
 safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(motor1pin2, LOW);
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH);
delay(1000);
  digitalWrite(motor1pin1, LOW);
  digitalWrite(motor1pin2, HIGH);
  digitalWrite(motor2pin2, LOW);
  digitalWrite(motor2pin1, HIGH);
}
else{
  digitalWrite(motor1pin2, HIGH);
  digitalWrite(motor1pin1, LOW);

  digitalWrite(motor2pin2, LOW);
  digitalWrite(motor2pin1, HIGH);
  delay(1000);
}

No { or } in setup()
No ; after the last 4 lines of setup()

Yes.

Here is a complete Arduino program that compiles without errors. Start there and have fun.

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

isn't the above the same as

if (safetyDistance <= 5){
  digitalWrite(motor1pin2, LOW);
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH);
  delay(1000);
}

digitalWrite(motor1pin2, HIGH);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor2pin2, LOW);
digitalWrite(motor2pin1, HIGH);

won't this just result in the robot moving back and forth in front of some obstacle? why not turn while reversing?


// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int motor1pin2 = 2;
const int motor2pin2= 3; 
const int motor1pin1 = 4; 
const int motor2pin1= 5; 
int distance, duration, safetyDistance;


void setup(){
pinMode(tripPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(motor1pin1,OUTPUT);
pinMode(motor1pin2,OUTPUT);
pinMode(motor2pin1,OUTPUT);
pinMode(motor2pin2,OUTPUT);
}


void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(motor1pin2, LOW);
  digitalWrite(motor1pin1, HIGH);
}
else{
  digitalWrite(motor1pin2, HIGH);
  digitalWrite(motor1pin1, LOW);

  digitalWrite(motor2pin2, LOW);
  digitalWrite(motor2pin1, HIGH);
  delay(1000);
}

}

Hi, @snoopybeach

It looks like you have wrtten your code as one big proceedure.

Can I suggest you forget your code for the moment AND write some code that JUST makes the motors run back and forth at 5 second intervals.

You need to get your motor control done first BEFORE adding ultrasonics etc.

A schematic would help as we do not even know ;
What Arduino controller your are using.
What type of motor controller you are using.
What the motor specs are.
What you are using for a power supply.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.