Error Creating a library, calling from another library

I am building my own 4 wheeled dc motor car that I want to move in random patterns (direction and distance). In order to clean up my code and to practice with arduino I am attempting to create my own library.

the end goal would be to call library functions from the library I am creating along with a random number generator

something to the effect of :

#include "customlibrary.h"

void setup()
{
//standard serial setup stuff
}

void loop()
{
int distance = random(0,5); // generate a random distance from 0 - 5
int speed = random(0,3); // generate a random speed 0 -3,
int direction = random(1,4); //generate a random integer to correspond to direction
int time = random(500,3000); //generate a random time between 500 and 3000

switch(direction):

case 1: // direction 1 is forward

customlibrary.fuction1(speed); //set all wheels to the randomyl generated speed
while(int i=distance;i>=0;i--)
{
customlibrary.function2(distance); //make the car go forward for random distance
}
customlibrary.fuction3(); //stop the car
delay(time);
break;

case 2: //direction 2 is reverse

//similar stuff as case 1
break;

case 3: //etc. etc.

deafult:
customlibrary.function3(); function to stop the car
}

ok so now that you can see the idea I am going for, I am using an official arduino Mega2560 and the adafruit motorshield v2.3.

when creating my custom library wont compile. it gives me an error in the custom library when invoking the adafruit motorshield library commands. Specifically the :
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *FR = AFMS.getMotor(1);
Adafruit_DCMotor *FL = AFMS.getMotor(2);
Adafruit_DCMotor *RR = AFMS.getMotor(3);
Adafruit_DCMotor *RL = AFMS.getMotor(4);

FR,FL,RR,RL are the front left, front right.... wheels. If I dont use this code when I attempt to create the custom library

void forward(int _distance)
{
FR->FORWARD;
FL->FORWARD;
etc.....
}

returns that FR, FL are not defined......

This is the error returned....

Using library adafruit_motor_shield_v2_library_1_0_5 at version 1.0.5 in folder: /home/ubuntu/opt/libraries/latest/adafruit_motor_shield_v2_library_1_0_5

/tmp/ccmbXdx5.ltrans0.ltrans.o: In function `main':

ccmbXdx5.ltrans0.o:(.text.startup+0xe0): undefined reference to `setup'

ccmbXdx5.ltrans0.o:(.text.startup+0xe8): undefined reference to `loop'

collect2: error: ld returned 1 exit status

exit status 1

In my custom library I am including the correct adafruit library, which works on my test programs but for some reason wont compile when I try to use it inside of my library.....

Is this some sort of circular error message? can I not set variables using another library function inside of my library?

any help will be greatly appreciated.

This post makes no sense without code.

The errors you posted:

Using library adafruit_motor_shield_v2_library_1_0_5 at version 1.0.5 in folder: /home/ubuntu/opt/libraries/latest/adafruit_motor_shield_v2_library_1_0_5

/tmp/ccmbXdx5.ltrans0.ltrans.o: In function `main':

ccmbXdx5.ltrans0.o:(.text.startup+0xe0): undefined reference to `setup'

ccmbXdx5.ltrans0.o:(.text.startup+0xe8): undefined reference to `loop'

collect2: error: ld returned 1 exit status

exit status 1

Only make note that your code doesn't contain a setup and loop function which it MUST if you want it to compile. DO you have an .ino file that you are compiling with this library?

Instead of handwaving about this and that, post the ACTUAL code that you are trying to use and the ACTUAL error messages that you get. That's the only way you're going to get help with it.

Don't worry if it is long. We're coders, we can handle long code. But we can't debug what we can't see.

library attempt code

/*
  Movement.h a library to allow movement of car
  Released into the public domain.
*/
#ifndef Movement_h
#define Movement_h

//include libraries in the class to clean up the overall code, including motor shield
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

class Movement
{
  public:
    //Movement(int pin);
    void getlibraries();
    void forwardright(int distance, int speed);
    void forwardleft();
    void reverseright();
    void reverseleft();
    void randompatter();
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    Adafruit_DCMotor *FR = AFMS.getMotor(1);
    Adafruit_DCMotor *FL = AFMS.getMotor(2);
    Adafruit_DCMotor *RR = AFMS.getMotor(3);
    Adafruit_DCMotor *RL = AFMS.getMotor(4);
  //private:
    //int _pin;

  void lowSpeed() //class to set speed to 33% of max for all wheels
  {
    
    FR->setSpeed(84);
    FL->setSpeed(84);
    RR->setSpeed(84);
    RL->setSpeed(84);
  }

  void mediumSpeed() //class to set speed to 67% of max for all wheels
  {
    FR->setSpeed(171);
    FL->setSpeed(171);
    RR->setSpeed(171);
    RL->setSpeed(171);
  }

  void maxSpeed() //class to set speed to 100% of max for all wheels
  {
    FR->setSpeed(255);
    FL->setSpeed(255);
    RR->setSpeed(255);
    RL->setSpeed(255);
  }

  void Forward() //class for all wheels forward
  {
  
  FR->run(FORWARD);
  RR->run(FORWARD);
  FL->run(FORWARD);
  RL->run(FORWARD);
  }

  void Reverse() //class for all wheels reverse
  {
    FR->run(BACKWARD);
    RR->run(BACKWARD);
    FL->run(BACKWARD);
    RL->run(BACKWARD);
  }

  void stopCar() //class for all wheels stop
  {
    FR->run(RELEASE);
    RR->run(RELEASE);
    FL->run(RELEASE);
    RL->run(RELEASE);
  }

  void forwardRight() // class for a forward right turn
  {
    FR->run(RELEASE);
    RR->run(RELEASE);
    FL->run(FORWARD);
    RL->run(FORWARD);
  }

  void forwardLeft() //class for a forward left turn
  {
    FR->run(FORWARD);
    RR->run(FORWARD);
    FL->run(RELEASE);
    RL->run(RELEASE);
  }

  void reverseRight() //class for a reverse right turn
  {
    FR->run(RELEASE);
    RR->run(RELEASE);
    FL->run(BACKWARD);
    RL->run(BACKWARD);
  }

};

#endif

the project i want to clean up and plant the code into (still very much in progress)

/* 
PROGRAM SETUP, LIBRARIES AND PIN INITIALIZATION
*/

//ir remote control debug
#include <IRremote.h>
int RECV_PIN = 52; //defines input pin as pin 52 on Arduino
IRrecv irrecv(RECV_PIN);
decode_results results;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 5, 4, 3, 2);

#include <Wire.h> //include adafruit motorshield library
#include <Adafruit_MotorShield.h> //include adafruit motorshield library

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *FR = AFMS.getMotor(1);
Adafruit_DCMotor *FL = AFMS.getMotor(2);
Adafruit_DCMotor *RR = AFMS.getMotor(3);
Adafruit_DCMotor *RL = AFMS.getMotor(4);

/*
SETUP BLUETOOTH NAMED "ROBOT_CAR"
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int BTcmd; //initialize bluetooth variable to receive and store bluetooth inputs

/*
SETUP IR AVOIDANCE SENSORS
*/

int ObstacleFrontLeftPin = 22;  // Front obstacle input pin
int ObstacleFrontLeft = HIGH;  // HIGH MEANS NO OBSTACLE
int ObstacleFrontRightPin = 23;  // Front obstacle input pin
int ObstacleFrontRight = HIGH;  // HIGH MEANS NO OBSTACLE
int ObstacleRearPin = A10;  // Rear obstacle input pin
int ObstacleRear = HIGH;  // HIGH MEANS NO OBSTACLE

 
/*
CREATE SETUP AND CLASSES FOR TURNING, SPEED, MOVEMENT COMMANDS
*/
void lowspeed() //class to set speed to 33% of max for all wheels
{
  FR->setSpeed(84);
  FL->setSpeed(84);
  RR->setSpeed(84);
  RL->setSpeed(84);
}

void mediumspeed() //class to set speed to 67% of max for all wheels
{
  FR->setSpeed(171);
  FL->setSpeed(171);
  RR->setSpeed(171);
  RL->setSpeed(171);
}

void maxspeed() //class to set speed to 100% of max for all wheels
{
  FR->setSpeed(255);
  FL->setSpeed(255);
  RR->setSpeed(255);
  RL->setSpeed(255);
}

void forward() //class for all wheels forward
{
  FR->run(FORWARD);
  RR->run(FORWARD);
  FL->run(FORWARD);
  RL->run(FORWARD);
}

void reverse() //class for all wheels reverse
{
  FR->run(BACKWARD);
  RR->run(BACKWARD);
  FL->run(BACKWARD);
  RL->run(BACKWARD);
}

void stopcar() //class for all wheels stop
{
  FR->run(RELEASE);
  RR->run(RELEASE);
  FL->run(RELEASE);
  RL->run(RELEASE);
}

void forwardright() // class for a forward right turn
{
  FR->run(RELEASE);
  RR->run(RELEASE);
  FL->run(FORWARD);
  RL->run(FORWARD);
}

void forwardleft() //class for a forward left turn
{
  FR->run(FORWARD);
  RR->run(FORWARD);
  FL->run(RELEASE);
  RL->run(RELEASE);
}

void reverseright() //class for a reverse right turn
{
  FR->run(RELEASE);
  RR->run(RELEASE);
  FL->run(BACKWARD);
  RL->run(BACKWARD);
}

void reverseleft() //class for a reverse left turn
{
  FR->run(BACKWARD);
  RR->run(BACKWARD);
  FL->run(RELEASE);
  RL->run(RELEASE);
}

void setup() 
{
  // initialize the serial communications:
  Serial1.begin(9600); //serial 1 not put onto monitor, used to receive bluetooth commands
  Serial.begin(9600); //serial for output onto monitor
  /*
  adafruit motorshield items
  */
  AFMS.begin();  // create with the default frequency 1.6KHz ---adafruit motorshield
  
  /*
  IR receiver setup
  */
  irrecv.enableIRIn(); // set up the LCD's number of columns and rows:
  
  /*
  LCD setup
  */
  lcd.begin(16,2);  //set the cursor to start at the top left character position
  lcd.rightToLeft(); // setup the lcd to write right to left, NEEDS DEBUGGING
  lcd.clear(); //clear the lcd screen
  
  /*
  IR AVOIDANCE SENSOR SETUP
  */
  pinMode(ObstacleFrontLeftPin, INPUT);
  pinMode(ObstacleFrontRightPin, INPUT);
  pinMode(ObstacleRearPin, INPUT);
  
  //LET USER KNOW THAT THE PROGRAM IS READY TO BEGIN
  lcd.print("BEGIN PROGRAM");
  delay(1500);
  lcd.clear();
}



void loop()
{
  ObstacleFrontLeft = digitalRead(ObstacleFrontLeftPin);
  ObstacleFrontRight = digitalRead(ObstacleFrontRightPin);
  ObstacleRear = digitalRead(ObstacleRearPin);

  while (ObstacleFrontLeftPin == HIGH || ObstacleFrontRightPin==HIGH)
  {
      Serial.println("step");//debug for while command....for some reason if and whiel commands wont go into loop....
      BTcmd = Serial1.read();
      irrecv.resume();   // Receive the next value
      lcd.clear();
      switch (BTcmd) 
      {

        case 1: //forward bluetooth command
          Serial.println("Success");
          lcd.write("FORWARD");
          lcd.setCursor(0,2);
          lcd.write("LOW SPEED");
          for (int i=500; i>0; i--) 
          {
            Serial.println(i);
            lowspeed();
            forward();
            stopcar();
          }
          break;

        case 2: //reverse bluetooth command
          lcd.write("REVERSE");
          lcd.setCursor(0,2);
          lcd.write("MEDIUM SPEED");
          for (int i=500; i>0; i--) 
          {
            Serial.println("reverse " + i);
            mediumspeed();
            reverse();
            stopcar();
          }
          break;

        case 3: //forward right bluetooth command 
          lcd.write("FORWARD RIGHT");
          lcd.setCursor(0,2);
          lcd.write("LOW SPEED");
          for (int i=500; i>0; i--) 
          {
            Serial.println(i);
            lowspeed();
            forwardright();
            stopcar();
          }
          break;

        case 99: //emergency stop command
          lcd.write("EMERGENCY");
          lcd.setCursor(0,2);
          lcd.write("STOP");
          stopcar();
          break;
        default:
          lcd.write("AWAITING COMMAND");
          stopcar();//added for BT command, if no signal, car stops
          delay(5000);
          break;
      }
    }
    
    while (ObstacleFrontLeft==LOW || ObstacleFrontRight==LOW)
      {
        Serial.println("OBJECT DETECTED!!!");
        lcd.clear();
        lcd.write("OBJECT DETECTED!!!");
        stopcar();
        maxspeed();
        reverse();
        ObstacleFrontLeft = digitalRead(ObstacleFrontLeftPin);
        ObstacleFrontRight = digitalRead(ObstacleFrontRightPin);
        ObstacleRear = digitalRead(ObstacleRearPin);
        break;
      }
}

the errors being returned when trying to compile the custom library I am attempting

Using library adafruit_motor_shield_v2_library_1_0_5 at version 1.0.5 in folder: /home/ubuntu/opt/libraries/latest/adafruit_motor_shield_v2_library_1_0_5

/tmp/ccmbXdx5.ltrans0.ltrans.o: In function `main':

ccmbXdx5.ltrans0.o:(.text.startup+0xe0): undefined reference to `setup'

ccmbXdx5.ltrans0.o:(.text.startup+0xe8): undefined reference to `loop'

collect2: error: ld returned 1 exit status

exit status 1

What are the names of the two files you just posted? The second one has a setup and loop function so I know that isn't the one the errors came from.

The first one looks like it should be in a .h file. It's going to fail to compile for a number of other reasons. But for now, the main issue is that you don't have a setup and loop function.

You have to have a .ino file with a setup and a loop function to compile. Even if those functions are empty, they must be there or you can't compile. That's the whole deal with Arduino.

the .h file I am trying to create is the first set of code.
the .ino file is the second set of code.

I am debugging the .h file I am trying to create, however it seems to not like that I am invoking the adafruit motor shield library and setting the variables FR,FL,RR,RL as DC motors 1,2,3,4 as prescribed by the adafruit motor library.

the .ino file with the same code works no problems but when I try to put it into the library I am creating it throws me an error.

Yeah, you can't do that there in the class definition. You gotta do that in the constructor. Read up on classes in C++. Constructors are important.

Lesson #2 before it bites you next. Don't mess with hardware in a constructor. Always write a begin method or something you can call from setup for setting pinModes and stuff.

You also need to move all the function implementation into a similarly named .cpp file. Only the declarations need to be in the class definition.

I've been reading up on constructors for the last half hour. I appreciate the help, I only took 2 java classes in highschool 12 years ago lol. So I am very new to this but I have the desire and its fun as hell.

I'll give it a shot tomorrow and see what I can come up with.

Just to be sure you aren't confused. This is C++. It is NOT Java.

You can NOT call functions in the DEFINITION of a class.

The class has a field where the instances of the Adafruit_DCMotor class will be stored. Those instances need to be valued when an instance of you class is instantiated, NOT when the class is DEFINED.

@Delta_G thank you for the patience. I know its C++ so my java, I am slowly learning through the University of Google.

@PaulS thank you also.

I want the finished product to me clean and elegant but I told myself that it doesn't have to be perfect right now as memory ins't an issue on the Mega2560 for the short code I am writing.

I was reading up on formats and decided to go a different route for now to get my project running. I am still pursuing the custom library on the side as I go along.

I will keep the forum updated as I work toward completion.