#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…