Including Arduino Library in a Class?

Hi,

I am trying to write a simple class to handle signal being sent to the Electronic Speed Controllers in my quadcopter using the Servo Library. The Class is not really necessary, but I just want to have it more organize and object oriented. So here’s my code:

header:

#ifndef Motor_h
#define Motor_h

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

class Motor
{
  private:
    const int motorNum = 4;
    extern Servo motor[4];

  public:
    Motor( int _pin0, int _pin1, int _pin2, int _pin3);
    void writeChannel(int _pin, int _pwm);
    void writeAll(int _pwm);
    void writePWM(int* _pwm);
};

#endif

And here’s the cpp file:

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

Motor::Motor( int _pin0, int _pin1, int _pin2, int _pin3)
{
  motor[0].attach(_pin0);
  motor[1].attach(_pin1);
  motor[2].attach(_pin2);
  motor[3].attach(_pin3);
}

void Motor::writeChannel(int _channel, int _pwm)
{
  motor[_channel].writeMicroseconds(_pwm);
}

void Motor::writeAll(int _pwm)
{
  for (int i = 0; i < motorNum; i++)
    writeChannel(i, _pwm);
}

void Motor::writePWM(int* _pwm)
{
  for (int i = 0; i < motorNum; i++)
    writeChannel(i, _pwm[i]);
}

And here’s the main test code:

#include "Motor.h"

Motor motor(47,49,51,53);

void setup() {
  
  Serial.begin(115200);
  delay(3000);
  
  Serial.println("ARM!");
  motor.writeAll(1000);
  
  delay(1000);
}

void loop() {
  
  for (int i = 1000; i < 1350; i += 1)
  {
    Serial.println(i);
    motor.writeAll(i);
    delay(100);
  }
  
  delay(2000);
}

So, I am getting an error while trying to compile:

Motor.cpp:2:19: fatal error: Servo.h: No such file or directory
 #include <Servo.h>
                   ^
compilation terminated.
Error compiling.

I am using an Arduino DUE, I tested it before, the Servo library is there. I was able to run the motors. It’s just that when I put this in a class, it seems to be missing? How do I fix this?

Thanks

Include servo.h into your sketch (also).

For an explanation why, read the third bullet point of this article: http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

pYro_65: Include servo.h into your sketch (also).

For an explanation why, read the third bullet point of this article: http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html

Oh wow, that solved the problem. Thanks a lot! I tried searching online but I guess I was searching the wrong thing lol