I keep receiving the “a function-definition is not allowed here before ‘{’ token” error for one line of my code and the exit status 1
int pingPin = 3;
int echoPin = 2;
int fwdRightMotor = 5;
int revRightMotor = 4;
int fwdLeftMotor = 7;
int revLeftMotor = 6;
void setup() {
Serial.begin (9600); //Starting Serial Terminal
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds (20);
digitalWrite(pingPin, HIGH);
delayMicroseconds(100);
digitalWrite(pingPin, LOW);
pinMode (echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
long microsecondsToCentimeters(long microseconds) { //this is where the error message shows for
return microseconds / 29.387 / 2;
cm=microsecondsToCentimeters(duration);
}
Serial.print(cm);
Serial.print(“cm”);
Serial.println();
delay(100);
pinMode(fwdRightMotor, OUTPUT);
pinMode(revRightMotor, OUTPUT);
pinMode(fwdLeftMotor, OUTPUT);
pinMode(revLeftMotor, OUTPUT);
if (cm <= 5) {
digitalWrite(fwdRightMotor, HIGH);
digitalWrite(revRightMotor, LOW);
digitalWrite(fwdLeftMotor, HIGH);
digitalWrite(revLeftMotor, LOW);
delay(20);
}
else if (cm > 5 && cm < 9) {
digitalWrite(fwdRightMotor, HIGH);
digitalWrite(revRightMotor, LOW);
digitalWrite(fwdLeftMotor, LOW);
digitalWrite(revLeftMotor, HIGH);
delay(20);
}
else (cm > 9); {
digitalWrite(fwdRightMotor, HIGH);
digitalWrite(revRightMotor, LOW);
digitalWrite(fwdLeftMotor, LOW);
digitalWrite(revLeftMotor, HIGH);
delay(20);
digitalWrite(fwdRightMotor, HIGH);
digitalWrite(revRightMotor, LOW);
digitalWrite(fwdLeftMotor, HIGH);
digitalWrite(revLeftMotor, LOW);
}
}
I checked my brackets and they seem to be equal, (unless I am mistaken). Help would be appreciated, thank you.