RC Car Code Error using functions

Hey guys. I wrote code to allow me to use a remote to move my robot. My first code I used if statements and everything worked great. To make the code more efficient I created functions. I defined the each button and the corresponding HEX code, which worked with the If statement code. In the new code I keep getting the following error:
expected unqualified-id before numeric constant

It doesn't like the #define statements with functions, but gave no error for If code. Am I missing something here.

Here is the code:

#include <SoftwareSerial.h>

#include <CytronMotorDriver.h>

#include <IRremote.h> //must copy IRremote library to arduino libraries

#define forward 0x8f7a25d //move forward
#define reverse 0x8f7aa55 //move in reverse
#define left 0x8f7e01f // move left
#define right 0x8f7807f //move right
#define stop_all_motors 0x8f7ba45 // stop all motors*/

// defines pins numbers ultrasonic sensor.
const int trigPin = 5;
const int echoPin = 10;

// defines variables
long duration;
int distance;

int receiver_pin = 2; //output pin of IR receiver to pin 2 of arduino
IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;

//IR sensor pin for remote control
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;

//Left motor 1
int PWMA = 13;
int DIRA = 3;

//Left motor 2
int PWMB = 4;
int DIRB = 5;

//Right motor 1
int PWMC = 6;
int DIRC = 7;

//Right motor 2
int PWMD = 8;
int DIRD = 9;

void setup()
{

Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
receiver.enableIRIn(); // Start to take the output from IR receiver
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
delay(200);

//Setting up pins for motors, PWM is speed, DIR is direction
pinMode(PWMA, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRB, OUTPUT);
pinMode(PWMC, OUTPUT);
pinMode(DIRC, OUTPUT);
pinMode(PWMD, OUTPUT);
pinMode(DIRD, OUTPUT);

}

void loop()
{
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

int speed = 100;

if (irrecv.decode(&output))
{
Serial.println(output.value, HEX);
irrecv.resume();

}
}

void forward()
{

digitalWrite(DIRA, HIGH);//back left motor forward
digitalWrite(DIRB, LOW);//front left motor forward
digitalWrite(DIRC, LOW);//back right motor forward
digitalWrite(DIRD, LOW);//front right motor forward

digitalWrite(PWMA,HIGH);
digitalWrite(PWMB,HIGH);
digitalWrite(PWMC,HIGH);
digitalWrite(PWMD,HIGH);

}

#define forward 0x8f7a25d //move forward
.
.
.

void forward()

See a problem here? It may not be the ONLY problem but it needs fixing.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

vaj4088:
#define forward 0x8f7a25d //move forward
.
.
.

void forward()

See a problem here? It may not be the ONLY problem but it needs fixing.

OMFG that's embarrassing lol. Sorry for wasting everyone's time for such a rookie mistake. Have a great weekend everyone.

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