USE OF Functions with libraries

I’ve been using Arduino for a while now and want to write a library to make my code cleaner. However while writing my library I’ve got a problem in one of the functions. The library is essentially a sub version of the servo library which i need to control speed controllers on a vehicle. I’ve looked through guides online and the essentials, I’ve got down declaration of functions within the class etc in the header file , what I’m having a problem with is the actual function themselves. For one of the functions i have to arm them (the speed controllers). This is done as you probably know through a servo function writeMicroseconds. Now what i don’t know is how to use that function within the cpp file. i don’t know whether i create a servo object within the cpp file. I’ve done the obvious and included servo.h in the h file but i don’t know how to access the writeMicroseconds function within the cpp. I’ve never made a library so i apologise in advance for my incompetence

the code if you should need :
any questions please ask, thank you very much for your time

header

#ifndef MOTOR_h
#define MOTOR_h

#include <Arduino.h>
#include <Servo.h>

class Motor

{
public**:**
Motor(int northpin, int westpin,int eastpin, int southpin);
void initMotor();
void setNMotorspeed(int spd);
void setEMotorspeed(int spd);
void setSMotorspeed(int spd);
void setWMotorspeed(int spd);

private**:**
int _northpin, _westpin , _eastpin , _southpin;

}

cpp

#include “Arduino.h”
#include “Motor.h”

Motor::Motor(int northpin, int westpin,int eastpin, int southpin);
{
_northpin= northpin;
_eastpin= eastpin;
_southpin= southpin; //assigning the private variables with the function variables
_westpin= westpin;

}

void initMotor();
{

/*heres where i need the microseconds function on the integer pins from the object initialiser
(northpin, westpin,etc) for arming */

}

void setNMotorspeed(int spd); // irrelevant functions please ignore

{

}
void setEMotorspeed(int spd);
{

}
void setSMotorspeed(int spd);
{

}
void setWMotorspeed(int spd);
{

}

:slight_smile:

Hmmm…

Just thinking in text here.. You may want to declare 4 servo objects in your class. (I'm assuming you are using 4) Set them up in your constructor or init routine. Then they would be available to your .cpp routines. I mean, "methods".

-jim lee

To declare a function as a member of a class… outside of the class, you must use explicit scoping ( just like the constructor ),
also when you define a function as opposed to declaring it, you do not use a semicolon before the curly brackets ‘{}’

Motor::Motor(int northpin, int westpin,int eastpin, int southpin){

}

void Motor::initMotor(){

}

As mentioned, you can create the servo variables as part of the class. If you declare them in the .cpp it would also work, however the raw servo objects will not be visible elsewhere ( which may be what you want, but similar to declared private in the class ). However get practice with a class as you’re using one already.

class Motor{
public:
  Motor(int northpin, int westpin,int eastpin, int southpin);
  void initMotor();
  void setNMotorspeed(int spd);
private:
  Servo servos[ 4 ]; //0 north, 1 east, 2 south, 3 west
};

//IN CPP FILE
Motor::Motor(int northpin, int westpin,int eastpin, int southpin){
  servos[ 0 ].attach( northpin );
  //... same for 1,2,3
}

void Motor::setNMotorspeed(int spd){
  servos[ 0 ].writeMicroseconds( spd );
}

If you assign the pins to the servos in the constructor, you may find you do not need the variables “int _northpin, _westpin , _eastpin , _southpin;

It is not clear, either, why a Motor has a north pin, a south pin, and east pin, and a west pin. Better names for something are in order here. Either there are multiple motors, connected to each of the pins, in which case a singular name for the class is misleading, or the names of the pins make no sense.

apologies for my late reply, internet has been very patchy.

thanks very much for the response i tried the first option of declaring a servo object inside the header and it seemed to work more thorough testing will follow.
there are 4 motors and each pin is for 1 speed controller, apologies for the confusion

olly

right I’ve tried it and am still getting a recurring error

i get the error “in motors.h Servo does not name a type” I’ve included the library and declared it so i don’t know what I’m doing it wrong and the libraries fine i tried with a different sketch and it worked so I’m a bit confused ?

#ifndef Motors_h
#define Motors_h

#include <Arduino.h>
#include <Servo.h>

class Motor
{
public:
Motor();
void setNmotor(int spd);
void setEmotor(int spd);
void setSmotor(int spd);
void setWmotor(int spd);

private:
int _north,_east,_south,_west;
Servo servo0,servo1,servo2,servo3;
};

#endif

I've included the library

In your sketch, too?

i've included servo.h in the header file of my library and i've included my library (motor.h) in the testing sketch if that answers your question. I didn't think i needed to include the servo.h in the testing sketch if its in the library.

cheers olly

I didn't think i needed to include the servo.h in the testing sketch if its in the library.

With the Arduino IDE, you do. There is information here ...

IT WORKED!!!

after three days the library finally compiles,

thanks to everyone I'm sure ill be asking another question soon :stuck_out_tongue:

olly