Need help with my arduino code

hey guys, I would like some help on my code for my Arduino project. it is a diy project and I am building a autonomous rc car, but every time I try to load my code in to the Arduino, I keep getting the following message:

sketch\Autonomous_Race_car.ino.cpp.o: In function `loop':

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:88: undefined reference to `Ultrasonic::Ranging(int)'

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:106: undefined reference to `Ultrasonic::Ranging(int)'

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:117: undefined reference to `Ultrasonic::Ranging(int)'

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:120: undefined reference to `Ultrasonic::Ranging(int)'

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:125: undefined reference to `Ultrasonic::Ranging(int)'

sketch\Autonomous_Race_car.ino.cpp.o:C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:141: more undefined references to `Ultrasonic::Ranging(int)' follow

sketch\Autonomous_Race_car.ino.cpp.o: In function `__static_initialization_and_destruction_0':

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:58: undefined reference to `Ultrasonic::Ultrasonic(int, int)'

C:\Users\14781\Downloads\Autonomous_Race_car/Autonomous_Race_car.ino:60: undefined reference to `Ultrasonic::Ranging(int)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino 101.

This happens even when the Arduino is plugged in. Please help me

@airoil123 Even though I’m sure many a folks here can help you troubleshoot your Arduino code… This is a forum for Particle products. Just wanted to point that out. The maker community is very helpful and typically friendly, so you probably won’t get torn to shreds for positing on here with a non-particle product question.

Could you please copy and paste your code so it can be looked over for errors?

2 Likes

sure here is my code:

int motorPin1 =  6;    // motor wire connected to digital pin 5
int motorPin2 =  5;    // motor wire connected to digital pin 6
int motorPin3 =  7;    // steering motor wire connected to digital pin 7
int motorPin4 =  8;    // steering motor wire connected to digital pin 8

int steeringMotorSpeed = 150;  // Set default power to send to steering motor


int avoidDistance = 80;
int endAvoidDistance = 100;

int goToReverseDistance = 55;
int reverseTime = 2000;

int fullTrottleMinDistance = 250;


int fullTrottleSpeed = 220;
int cruiseSpeed = 160;
int avoidSpeed = 120;
int reverseSpeed = 130;

int sensorDelay = 100;  //

long randNumber;
int avoidDirection = 1;
int avoidCirclesBeforeDirectionChange = 10;
int countDown = 1;
int distanceCountDown = 1;

#ifndef Ultrasonic_h
#define Ultrasonic_h

#define ARDUINO_H
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>

#define CM 1
#define INC 0

class Ultrasonic
{
  public:
    Ultrasonic(int TP, int EP);
    long Timing();
    long Ranging(int sys);

  private:
    int Trig_pin;
    int Echo_pin;
    long  duration, distacne_cm, distance_inc;

};

#endif
Ultrasonic ultrasonic(12, 13);

int distance = (ultrasonic.Ranging(CM));

// The setup() method runs once, when the sketch starts

void setup()   {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  randomSeed(analogRead(0));

  // initialize the digital pins as an output:
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop()
{
  // Serial.println("start loop  ");

  // Serial.println(ultrasonic.Ranging(CM));

  //choseMode();



  distance = (ultrasonic.Ranging(CM));

  if (distance <= goToReverseDistance) {
    avoidDirection = avoidDirection * -1;

    //   while(distance < avoidDistance) {
    // Serial.println("start reverse  ");
    if (avoidDirection == 1) {                    // turn one way
      analogWrite(motorPin3, steeringMotorSpeed);
      digitalWrite(motorPin4, LOW);
    } else {                                      // ... or the other way
      analogWrite(motorPin4, steeringMotorSpeed);
      digitalWrite(motorPin3, LOW);
    }
    analogWrite(motorPin2, reverseSpeed);            //rotates motor - backwards
    digitalWrite(motorPin1, LOW);
    delay(reverseTime);                              // for time set in reverseTime
    //   delay(sensorDelay);
    distance = (ultrasonic.Ranging(CM));
    //   }


    digitalWrite(motorPin2, LOW);                    // stop motors
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);


    avoidDirection = avoidDirection * -1;             // switch steering direction

    distance = (ultrasonic.Ranging(CM));
  }

  distance = (ultrasonic.Ranging(CM));

  if (distance < avoidDistance) {
    // Serial.println("start avoid  ");

    distance = (ultrasonic.Ranging(CM));
    countDown = avoidCirclesBeforeDirectionChange;
    distanceCountDown = distance;

    while (distance <= endAvoidDistance && distance > goToReverseDistance) {
      if (avoidDirection == 1) {
        analogWrite(motorPin3, steeringMotorSpeed); // turn one way
        digitalWrite(motorPin4, LOW);
      } else {
        analogWrite(motorPin4, steeringMotorSpeed); // or turn the other way
        digitalWrite(motorPin3, LOW);
      }

      analogWrite(motorPin1, avoidSpeed); //rotates motor
      digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
      delay(sensorDelay);
      distance = (ultrasonic.Ranging(CM));
      if (distance < distanceCountDown) {
        countDown--;  // if obstacle is getting closer - count down to changing direction
      }
      if (countDown < 1) {
        avoidDirection = avoidDirection * -1;   // switch steering direction
        countDown = avoidCirclesBeforeDirectionChange;
        distanceCountDown = distance;
      }
      // Serial.println(distance);
      // Serial.println(avoidDirection);
    }   // end while (avoid)
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
  }

  distance = (ultrasonic.Ranging(CM));

  while (distance > avoidDistance  && distance < fullTrottleMinDistance) {
    analogWrite(motorPin1, cruiseSpeed); //rotates motor
    digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
    delay(sensorDelay);
    distance = (ultrasonic.Ranging(CM));
    // Serial.println("cruise");
    // Serial.println(distance);
  }   // end while

  distance = (ultrasonic.Ranging(CM));

  while (distance > fullTrottleMinDistance) {
    analogWrite(motorPin1, fullTrottleSpeed); //rotates motor
    digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW
    delay(sensorDelay);
    distance = (ultrasonic.Ranging(CM));
    // Serial.println("FULL");
    // Serial.println(distance);
  }   // end while

  // Serial.println("REstart loop  ");

  // Serial.println(distance);


}

the bolded things are just a mistake by my computer. it is supposed to be the same size
(ScruffR: corrected formatting by Kenneth)

It seems you just copied content of ultrasonic.h into your main sketch. You have Ultrasonic class declaration, but not implementation. Copy both ultrasonic.h and ultrasonic.cpp files to the same folder you have the sketch and add #include "ultrasonic.h" instead. If you have library installed correctly in Arduino, you can try Sketch / Include library from menu, it should include the library correctly. Or try #include <ultrasonic.h>

2 Likes

I altered the code a bit and now I get a new message saying:

sketch\Ultrasonic.cpp:8:22: fatal error: WProgram.h: No such file or directory

 #include "WProgram.h"

                      ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino 101.

I don’t know what that means. this is my new code:

int motorPin1 =  6;    // motor wire connected to digital pin 5
int motorPin2 =  5;    // motor wire connected to digital pin 6
int motorPin3 =  7;    // steering motor wire connected to digital pin 7
int motorPin4 =  8;    // steering motor wire connected to digital pin 8

int steeringMotorSpeed = 150;  // Set default power to send to steering motor


int avoidDistance = 80;
int endAvoidDistance = 100;

int goToReverseDistance = 55;
int reverseTime = 2000;

 int fullTrottleMinDistance = 250;


int fullTrottleSpeed = 220;
int cruiseSpeed = 160;
int avoidSpeed = 120;
int reverseSpeed = 130;

int sensorDelay = 100;  //  

long randNumber;
int avoidDirection = 1;
int avoidCirclesBeforeDirectionChange = 10;
int countDown = 1;
int distanceCountDown = 1;



#define Ultrasonic_h



#if ARDUINO >= 100

  #include "Arduino.h"

#else

  #if defined(ARDUINO) && ARDUINO >= 100

  #include "Arduino.h"

  #else

  #include "WProgram.h"

  #endif

#include <Wire.h>

#include "wm_crypto.h"

#endif



#define CM 1

#define INC 0



class Ultrasonic

{

  public:

    Ultrasonic(int TP, int EP);

  Ultrasonic(int TP, int EP, long TO);

    long Timing();

    long Ranging(int sys);



  private:

    int Trig_pin;

    int Echo_pin;

  long Time_out;

    long duration,distance_cm,distance_inc;
    
};
Ultrasonic ultrasonic(12,13);

int distance = (ultrasonic.Ranging(CM));

// The setup() method runs once, when the sketch starts

void setup()   {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  randomSeed(analogRead(0));
  
  // initialize the digital pins as an output:
  pinMode(motorPin1, OUTPUT); 
  pinMode(motorPin2, OUTPUT);  
  pinMode(motorPin3, OUTPUT); 
  pinMode(motorPin4, OUTPUT);  
}

////////////   LOOP    ////////////

void loop()                     
{
   // Serial.println("start loop  ");
    
   // Serial.println(ultrasonic.Ranging(CM));

//choseMode();



distance = (ultrasonic.Ranging(CM));

////////////   REVERSE MODE    ////////////
  if(distance <= goToReverseDistance){
    avoidDirection = avoidDirection * -1;
    
//   while(distance < avoidDistance) {
    // Serial.println("start reverse  ");
      if(avoidDirection == 1){                      // turn one way
        analogWrite(motorPin3, steeringMotorSpeed);
        digitalWrite(motorPin4, LOW);  
      } else {                                      // ... or the other way
        analogWrite(motorPin4, steeringMotorSpeed); 
        digitalWrite(motorPin3, LOW); 
      }
    analogWrite(motorPin2, reverseSpeed);            //rotates motor - backwards
    digitalWrite(motorPin1, LOW); 
    delay(reverseTime);                              // for time set in reverseTime
 //   delay(sensorDelay);
    distance = (ultrasonic.Ranging(CM));
 //   }

 
    digitalWrite(motorPin2, LOW);                    // stop motors
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    
 
    avoidDirection = avoidDirection * -1;             // switch steering direction
                               
    distance = (ultrasonic.Ranging(CM));
  }
////////////////////////////////////////////////

////////////   AVOID MODE    ////////////

        distance = (ultrasonic.Ranging(CM));
        
  if(distance < avoidDistance){
  // Serial.println("start avoid  ");

    distance = (ultrasonic.Ranging(CM));
    countDown = avoidCirclesBeforeDirectionChange;
    distanceCountDown = distance;
    
  while(distance <= endAvoidDistance && distance > goToReverseDistance){
        if(avoidDirection == 1){
            analogWrite(motorPin3, steeringMotorSpeed); // turn one way
            digitalWrite(motorPin4, LOW);
        } else {
            analogWrite(motorPin4, steeringMotorSpeed); // or turn the other way
            digitalWrite(motorPin3, LOW);
        }
        
        analogWrite(motorPin1, avoidSpeed); //rotates motor
        digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
        delay(sensorDelay);
        distance = (ultrasonic.Ranging(CM));
          if(distance < distanceCountDown) { countDown--; }           // if obstacle is getting closer - count down to changing direction
          if(countDown < 1) {
              avoidDirection = avoidDirection * -1;   // switch steering direction
              countDown = avoidCirclesBeforeDirectionChange;
              distanceCountDown = distance;  
          }
    // Serial.println(distance);  
    // Serial.println(avoidDirection);  
    }   // end while (avoid) 
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
  }
////////////////////////////////

////////////   CRUISE MODE    ////////////
        distance = (ultrasonic.Ranging(CM));
        
    while(distance > avoidDistance  && distance < fullTrottleMinDistance){
        analogWrite(motorPin1, cruiseSpeed); //rotates motor 
        digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
        delay(sensorDelay);   
        distance = (ultrasonic.Ranging(CM));
    // Serial.println("cruise");
    // Serial.println(distance);  
    }   // end while
    
    
////////////   FULL SPEED MODE    ////////////
        distance = (ultrasonic.Ranging(CM));
        
    while(distance > fullTrottleMinDistance){
        analogWrite(motorPin1, fullTrottleSpeed); //rotates motor 
        digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
        delay(sensorDelay);   
        distance = (ultrasonic.Ranging(CM));
              // Serial.println("FULL");
              // Serial.println(distance);  
    }   // end while

      // Serial.println("REstart loop  ");
    
   // Serial.println(distance);
  

}

if you can tell me what this means, I will be grateful.


(ScruffR: reformatted)

Well, you still seem to have all ultrasound.h code pasted into main code. You need to get rid of this first. It is everything from #define Ultrasonic_h to }; just line above Ultrasonic ultrasonic(12,13);

My only suggestion now is to discard everything you have and start with something simpler. Or try using library example sketch, if the library has one (menu File > Examples), check it is working and modify it until it does what you want. Possibly adding code from other example if you want do multiple things at once. I suspect you are building a robot, so you will need a motor controlling library too. This is how I approach unknown code (I learned HTML this way back in… around 1995?)

I did not find Ultrasound library in mu Arduino IDE, so it might not be a standard library. Make sure it is installed correctly.

That said, I am afraid I can’t help you with basic Arduino specific stuff. I will try to answer generic programming questions as I am able, it can be helpful to other members using Particle devices. But let’s not clutter this board with Arduino only stuff, please.

1 Like

Perhaps you should take a look at this article on creating a library.

Once you understand the structure of Arduino-like code, you may be able to work that out on your own.

Hint: Unless you make the complete class definitions inside your .ino file, you will need a header and implementation file (.h and .cpp) for each class/library.

1 Like

@airoil123, In order to format code blocks properly, wrap them in a set of these

 ```cpp
 // your code block
*(note these are not ordinary single quotes (') but so called grave accent (```))*
*For more formatting info look at [Forum Tips and Tricks](https://community.particle.io/t/forum-tips-and-tricks/3999)*

when I add two files to the same file as the Arduino code is in, the code finally gets updated. But when I power on the car, it does not want to move at all and only the sensor is working. my code is below, as is the other two files:

int motorPin1 = 6; // motor wire connected to digital pin 5
int motorPin2 = 5; // motor wire connected to digital pin 6
int motorPin3 = 7; // steering motor wire connected to digital pin 7
int motorPin4 = 8; // steering motor wire connected to digital pin 8

int steeringMotorSpeed = 150; // Set default power to send to steering motor

int avoidDistance = 80;
int endAvoidDistance = 100;

int goToReverseDistance = 55;
int reverseTime = 2000;

int fullTrottleMinDistance = 250;

int fullTrottleSpeed = 220;
int cruiseSpeed = 160;
int avoidSpeed = 120;
int reverseSpeed = 130;

int sensorDelay = 100; //

long randNumber;
int avoidDirection = 1;
int avoidCirclesBeforeDirectionChange = 10;
int countDown = 1;
int distanceCountDown = 1;

#define Ultrasonic_h

#if ARDUINO >= 100

#include “Arduino.h”

#else

#if defined(ARDUINO) && ARDUINO >= 100

#include “Arduino.h”

#else

#include “Arduino.h”

#endif

#include <Wire.h>

#include “wm_crypto.h”

#endif

#define CM 1

#define INC 0

class Ultrasonic

{

public:

Ultrasonic(int TP, int EP);

Ultrasonic(int TP, int EP, long TO);

long Timing();

long Ranging(int sys);

private:

int Trig_pin;

int Echo_pin;

long Time_out;

long duration,distance_cm,distance_inc;

};
Ultrasonic ultrasonic(12,13);

int distance = (ultrasonic.Ranging(CM));

// The setup() method runs once, when the sketch starts

void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

randomSeed(analogRead(0));

// initialize the digital pins as an output:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}

//////////// LOOP ////////////

void loop()
{
// Serial.println("start loop ");

// Serial.println(ultrasonic.Ranging(CM));

//choseMode();

distance = (ultrasonic.Ranging(CM));

//////////// REVERSE MODE ////////////
if(distance <= goToReverseDistance){
avoidDirection = avoidDirection * -1;

// while(distance < avoidDistance) {
// Serial.println("start reverse ");
if(avoidDirection == 1){ // turn one way
analogWrite(motorPin3, steeringMotorSpeed);
digitalWrite(motorPin4, LOW);
} else { // … or the other way
analogWrite(motorPin4, steeringMotorSpeed);
digitalWrite(motorPin3, LOW);
}
analogWrite(motorPin2, reverseSpeed); //rotates motor - backwards
digitalWrite(motorPin1, LOW);
delay(reverseTime); // for time set in reverseTime
// delay(sensorDelay);
distance = (ultrasonic.Ranging(CM));
// }

digitalWrite(motorPin2, LOW);                    // stop motors
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);


avoidDirection = avoidDirection * -1;             // switch steering direction
                           
distance = (ultrasonic.Ranging(CM));

}
////////////////////////////////////////////////

//////////// AVOID MODE ////////////

    distance = (ultrasonic.Ranging(CM));

if(distance < avoidDistance){
// Serial.println("start avoid ");

distance = (ultrasonic.Ranging(CM));
countDown = avoidCirclesBeforeDirectionChange;
distanceCountDown = distance;

while(distance <= endAvoidDistance && distance > goToReverseDistance){
if(avoidDirection == 1){
analogWrite(motorPin3, steeringMotorSpeed); // turn one way
digitalWrite(motorPin4, LOW);
} else {
analogWrite(motorPin4, steeringMotorSpeed); // or turn the other way
digitalWrite(motorPin3, LOW);
}

    analogWrite(motorPin1, avoidSpeed); //rotates motor
    digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
    delay(sensorDelay);
    distance = (ultrasonic.Ranging(CM));
      if(distance < distanceCountDown) { countDown--; }           // if obstacle is getting closer - count down to changing direction
      if(countDown < 1) {
          avoidDirection = avoidDirection * -1;   // switch steering direction
          countDown = avoidCirclesBeforeDirectionChange;
          distanceCountDown = distance;  
      }
// Serial.println(distance);  
// Serial.println(avoidDirection);  
}   // end while (avoid) 
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);

}
////////////////////////////////

//////////// CRUISE MODE ////////////
distance = (ultrasonic.Ranging(CM));

while(distance > avoidDistance  && distance < fullTrottleMinDistance){
    analogWrite(motorPin1, cruiseSpeed); //rotates motor 
    digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
    delay(sensorDelay);   
    distance = (ultrasonic.Ranging(CM));
// Serial.println("cruise");
// Serial.println(distance);  
}   // end while

//////////// FULL SPEED MODE ////////////
distance = (ultrasonic.Ranging(CM));

while(distance > fullTrottleMinDistance){
    analogWrite(motorPin1, fullTrottleSpeed); //rotates motor 
    digitalWrite(motorPin2, LOW);    // set the Pin motorPin2 LOW    
    delay(sensorDelay);   
    distance = (ultrasonic.Ranging(CM));
          // Serial.println("FULL");
          // Serial.println(distance);  
}   // end while

  // Serial.println("Restart loop  ");

// Serial.println(distance);

}

my first other file, ultrasonic.h, is below:

#ifndef Ultrasonic_h
#define Ultrasonic_h

#include “Arduino.h”

#define CM 1
#define INC 0

class Ultrasonic
{
public:
Ultrasonic(int TP, int EP);
long Timing();
long Ranging(int sys);

private:
int Trig_pin;
int Echo_pin;
long  duration,distacne_cm,distance_inc;

};

#endif

my other file, ultrasonic.cpp, is below:

#include “Arduino.h”
#include “Ultrasonic.h”

Ultrasonic::Ultrasonic(int TP, int EP)
{
pinMode(TP,OUTPUT);
pinMode(EP,INPUT);
Trig_pin=TP;
Echo_pin=EP;
}

long Ultrasonic::Timing()
{
digitalWrite(Trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(Trig_pin, LOW);
duration = pulseIn(Echo_pin,HIGH);
return duration;
}

long Ultrasonic::Ranging(int sys)
{
Timing();
distacne_cm = duration /29 / 2 ;
distance_inc = duration / 74 / 2;
if (sys)
return distacne_cm;
else
return distance_inc;
}

Please edit your post to format the code properly, in the manner described in ScruffR’s last post.

2 Likes

If you want support from a community not actually meant/responsible for your platform, then heeding the community culture would be the least we ask for.
Otherwise we’ll close that thread.

2 Likes