Error- Making an Remote Control Car for School Project

#include <IRremote.h>

const int pingPin = 4; 
const int trigPin = 5;

int enableA = 11;
int pinA1 = 10;
int pinA2 = 9;

int enableB = 8;
int pinB1 = 7;
int pinB2 = 6;

int RECV_PIN = 53;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  pinMode(pingPin, INPUT);
  pinMode(trigPin, OUTPUT);
  
  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  irrecv.enableIRIn();
}
void loop()
{
 if(irrecv.decode(&results))
 {
  irrecv.resume();
 }
 if(results.value==0xFF18E7)
 {
  analogWrite(enableA,200);
  analogWrite(enableB,200);
 }
  int cm = pingPin;
  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(trigPin, OUTPUT); 
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
   digitalWrite(trigPin, HIGH); 
   delayMicroseconds(5); 
   digitalWrite(trigPin, 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; 
}
{if (cm <5)
{
  analogWrite(enableA,255);
  analogWrite(enableB,255);
  backward(600);
coast(200);
turnLeft(600);
coast(200);}
else {
forward(1);

}
}

//Define high-level H-bridge commands

void enableMotors()
{
motorAOn();
motorBOn();
}

void disableMotors()
{
motorAOff();
motorBOff();
}

void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}

void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}

void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}

void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}

void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}

void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
digitalWrite(enableA, HIGH);
}

void motorBOn()
{
digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
digitalWrite(enableB, LOW);
}

void motorBOff()
{
digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}

void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}

void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}
}
R_Remote_Car_1:48: error: a function-definition is not allowed here before '{' token
 { long duration, cm; 
 ^
IR_Remote_Car_1:67: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:78: error: 'backward' was not declared in this scope
   backward(600);
               ^
IR_Remote_Car_1:79: error: 'coast' was not declared in this scope
 coast(200);
          ^
IR_Remote_Car_1:80: error: 'turnLeft' was not declared in this scope
 turnLeft(600);
             ^
IR_Remote_Car_1:83: error: 'forward' was not declared in this scope
 forward(1);
          ^
IR_Remote_Car_1:91: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:97: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:103: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:110: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:117: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:124: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:131: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:138: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:147: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:152: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:158: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:163: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:169: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:175: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:182: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:188: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:195: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:201: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:207: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:213: error: a function-definition is not allowed here before '{' token
 {
 ^
IR_Remote_Car_1:216: error: expected '}' at end of input
 }
 ^
Using library z3t0-Arduino-IRremote-18f12d3 at version 2.2.3 in folder: /Users/johanntorres/Documents/Arduino/libraries/z3t0-Arduino-IRremote-18f12d3 
exit status 1
a function-definition is not allowed here before '{' token

This error comes up. Sorry…

This dialect of C++ does not allow you to define a function inside another function.

Code is a lot easier to read if each { and } is on a line by itself.

So where did I go wrong AWOL? Is it that long duration part?

You are trying to create a function called ping() inside the function loop(). That's not allowed.

The way you have laid out your code makes it hard to see that one is inside the other. Use the Auto-Format tool to get proper indentation.

...R

I intended return as a function to return the data in this case
'cm'

I have substantially changed Reply #3 which may have overlapped with your Reply #4

...R

Ok?

ThePhenomenalCheese:
Ok?

You've got five minutes between posts - use them sensibly.

Can you actually pls help me…
I’m just a seventh-grader doing a project…
:frowning:
And trying to combine 3 codes,
A distance sensing one,
An IRremote one
And a 4wd automatic car…

And what changes in the 'ping' script do I have to make?

The necessary ones, of course.

YOU need to understand what a function is, where a function starts, and where a function ends. Then, when we say that you can't put a function (ping()) inside another function (loop()), you'll know what that means.

Get rid of ALL of the code for the ping function. Determine where loop() ends. AFTER that is where you can retype your ping function.

So this is my code,

#include <IRremote.h>

const int pingPin = 4;
const int trigPin = 5;

int enableA = 11;
int pinA1 = 10;
int pinA2 = 9;

int enableB = 8;
int pinB1 = 7;
int pinB2 = 6;

int RECV_PIN = 53;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  pinMode(pingPin, INPUT);
  pinMode(trigPin, OUTPUT);

  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  irrecv.enableIRIn();
}
void loop()
{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 0xFF18E7)
  {
    analogWrite(enableA, 200);
    analogWrite(enableB, 200);
  }
  {
    int cm = pingPin;
    int ping(int pingPin);
    long duration, cm;
   }
    pinMode(trigPin, OUTPUT);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(trigPin, LOW);
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    cm = microsecondsToCentimeters(duration);
    return cm ;
  }
  {
    long microsecondsToCentimeters(long microseconds)
    return microseconds / 29 / 2;
  }

It returns with an error:

error: expected unqualified-id before '{' token
   {
   ^

expected initializer before '}' token
  {
    int cm = pingPin;
    int ping(int pingPin);
    long duration, cm;
   }
    pinMode(trigPin, OUTPUT);

I have no idea what is going on there.

See reply #1

Can somebody please help me!!!! :frowning:

See reply #1 and #7

AWOL,
this code

#include <IRremote.h>

const int pingPin = 4;
const int trigPin = 5;

int enableA = 11;
int pinA1 = 10;
int pinA2 = 9;

int enableB = 8;
int pinB1 = 7;
int pinB2 = 6;

int RECV_PIN = 53;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  pinMode(pingPin, INPUT);
  pinMode(trigPin, OUTPUT);

  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  irrecv.enableIRIn();
}
void loop()
{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 0xFF18E7)
  {
    analogWrite(enableA, 200);
    analogWrite(enableB, 200);
  }
  {
    int cm = pingPin;
    int ping(int pingPin);
    long duration, cm;
  }
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  return cm ;
}
{
  long microsecondsToCentimeters(long microseconds)
  return microseconds / 29 / 2;
}

now has a new error:

error: expected unqualified-id before '{' token
 {
 ^
conflicting declaration 'long int cm'
  long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Get rid of all the extraneous { and }

int ping(int pingPin);

This is a function prototype.
The compiler won't complain, but it really has no place here.

long ping(int pingPin)
{
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  pinMode(pingPin, INPUT);
  long duration = pulseIn(pingPin, HIGH);
  return microsecondsToCentimeters(duration);
}

You are still sprinkling curly braces around like they were some magic potion that will make the compiler behave. They are NOT. Pay attention to when they are needed, and STOP PUTTING THEM WHERE THEY ARE NOT.

This is my new code…

#include <IRremote.h>

const int pingPin = 4;
const int trigPin = 5;

int enableA = 11;
int pinA1 = 10;
int pinA2 = 9;

int enableB = 8;
int pinB1 = 7;
int pinB2 = 6;
int ping;
int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(pingPin, INPUT);
  pinMode(trigPin, OUTPUT);

  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  irrecv.enableIRIn();
}
void loop()
{
  if (irrecv.decode(&results))
  {
    irrecv.resume();
  }
  if (results.value == 0xFF18E7)
  {
    analogWrite(enableA, 200);
    analogWrite(enableB, 200);
  }

}
int ping(int pingPin);
{

  long duration, cm;

  {
    pinMode(trigPin, OUTPUT);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(trigPin, LOW);

    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    cm = microsecondsToCentimeters(duration);
    return cm ;
  }

  long microsecondsToCentimeters(long microseconds);
  return microseconds / 29 / 2;
}

And this error is showing up

'int ping(int)' redeclared as different kind of symbol

Can somebody please fix my issue in reply #17

You have a variable and a function with identical names. Change one of them.

...R