using a library in a class

This is standard OOP but I forgot how to do it the right way :

I want to use the servo library inside my own class, how do I do this?

This is what I tried :

#include <Servo.h>

class Robot
{
  public:
    // Constructor
    Robot() ;

    // Functions for servo
    void setupServo(int iServoPin) ;  // Setup the servo on a certain input
    void turnServo(int iDegrees) ;    // Turn the servo to a certain position

  private:
    // Variables for Servo
    Servo _MyServo ;   // in here??????
    int _iServoPin ;      // Input on which servo is connected
    int _iDegrees ;       // To which position must the servo turn
} ;

Robot Myrobot ;

void setup()
{
  Myrobot.setupServo(4) ;
}

void loop()
{
  Myrobot.turnServo(60) ;
  delay(3000) ;

  Myrobot.turnServo(120) ;
  delay(3000) ;
}

Robot::Robot()
{
  //Servo _MyServo ;  // in here?????
}

void Robot::setupServo(int iServoPin)
{
  //Servo _MyServo ;  // or here?????
  _iServoPin = iServoPin ;
  _MyServo.attach(_iServoPin) ;
}

void Robot::turnServo(int iDegrees)
{
  _iDegrees = iDegrees ;
  _myServo.write(_iDegrees) ;
}

It gives me the following error : _myServo was not declared in this scope

I understand why he gives me the error, but what is the correct way? Where and how do I declare the servo object?

What does your sketch look like? The IDE copies files to be copied to a build directory. It ONLY scans the sketch to learn what files to copy. If the sketch does not include Servo.h, the Servo.h and Servo.cpp files will not be copied.

The morons on the Arduino team have decided that the failure to find an include file is not a fatal error, or even something you should be told about. One of the stupidest decisions they have made, in my opinion.

This is my whole sketch :

#include <arduino.h>
#include <Servo.h>

class Robot
{
  public:
    // Constructor
    Robot() ;

    // Functions for Ultrasonic sensor
    void setupUltrasonicSensor(int iTrigPin, int iEchoPin, long lDuration) ;  // Setup the sensor
    long checkDistance() ;            // Check the distance to an object

    // Functions for servo
    void setupServo(int iServoPin) ;  // Setup the servo on a certain input
    void turnServo(int iDegrees) ;    // Turn the servo to a certain position

  private:
    // Variables for Ultrasonic sensor
    int _iTrigPin ;       // Input on which we trigger the sensor
    int _iEchoPin ;       // Output on which we check the echo
    long _lDistance ;     // Variable for the distance to an object
    long _lDuration ;     // Variable to hold how long we want the trigger pulse must last

    // Variables for Servo
    Servo _MyServo ;
    int _iServoPin ;      // Input on which servo is connected
    int _iDegrees ;       // To which position must the servo turn
} ;

Robot Myrobot ;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600) ;

  //Myrobot.setupUltrasonicSensor(7, 8, 10) ;
  Myrobot.setupServo(4) ;
}

void loop()
{
  // put your main code here, to run repeatedly:
  //long lDistance = Myrobot.checkDistance() ;

  //Serial.println(lDistance) ;

  Myrobot.turnServo(60) ;
  delay(3000) ;

  Myrobot.turnServo(90) ;
  delay(3000) ;

  Myrobot.turnServo(120) ;
  delay(3000) ;
}

Robot::Robot()
{
  this->_MyServo = new Servo() ;
}

void Robot::setupServo(int iServoPin)
{
  _iServoPin = iServoPin ;
  _MyServo.attach(_iServoPin) ;
}

void Robot::turnServo(int iDegrees)
{
  _iDegrees = iDegrees ;
  _myServo.write(_iDegrees) ;
}

void Robot::setupUltrasonicSensor(int iTrigPin, int iEchoPin, long lDuration)
{
  _iTrigPin = iTrigPin ;
  _iEchoPin = iEchoPin ;
  _lDuration = lDuration ;

  pinMode(_iTrigPin, OUTPUT) ;
  pinMode(_iEchoPin, INPUT) ;
}

long Robot::checkDistance()
{
  digitalWrite(_iTrigPin, LOW) ;
  delayMicroseconds(2) ;
  digitalWrite(_iTrigPin, HIGH) ;
  delayMicroseconds(10) ;
  digitalWrite(_iTrigPin, LOW) ;

  _lDuration = pulseIn(_iEchoPin, HIGH) ;

  _lDistance = (_lDuration / 2) / 29.1 ;

  return _lDistance ;
}

Errors include line numbers.

Arduino: 1.6.5 (Windows 7), Board: “Arduino Duemilanove or Diecimila, ATmega168”

Using library Servo in folder: C:\Program Files (x86)\Arduino\libraries\Servo

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=10605 -DARDUINO_AVR_DUEMILANOVE -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\libraries\Servo\src C:\Users\STEVEN~1.VET\AppData\Local\Temp\build2596975517652838384.tmp\robot.cpp -o C:\Users\STEVEN~1.VET\AppData\Local\Temp\build2596975517652838384.tmp\robot.cpp.o

robot.ino: In constructor ‘Robot::Robot()’:
robot.ino:77:12: error: no match for ‘operator=’ (operand types are ‘Servo’ and ‘Servo*’)
robot.ino:77:12: note: candidate is:
In file included from robot.ino:2:0:
C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:92:7: note: Servo& Servo::operator=(const Servo&)
class Servo
^
C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:92:7: note: no known conversion for argument 1 from ‘Servo*’ to ‘const Servo&’
robot.ino: In member function ‘void Robot::turnServo(int)’:
robot.ino:89:3: error: ‘_myServo’ was not declared in this scope
no match for ‘operator=’ (operand types are ‘Servo’ and ‘Servo*’)

on this sketch :

#include <arduino.h>
#include <Servo.h>

class Robot
{
  public:
    // Constructor
    Robot() ;

    // Functions for Ultrasonic sensor
    void setupUltrasonicSensor(int iTrigPin, int iEchoPin, long lDuration) ;  // Setup the sensor
    long checkDistance() ;            // Check the distance to an object

    // Functions for servo
    void setupServo(int iServoPin) ;  // Setup the servo on a certain input
    void turnServo(int iDegrees) ;    // Turn the servo to a certain position

    /*
    // Functions for motors
    void setupMotors() ;
    void forward(int iSpeed) ;
    void backward(int iSpeed) ;
    void turnRight(int iSpeed) ;
    void turnLeft(int iSpeed) ;
    void turnRightFast(int iSpeed) ;
    void turnLeftFast(int iSpeed) ;
    */
    
  private:
    // Variables for Ultrasonic sensor
    int _iTrigPin ;       // Input on which we trigger the sensor
    int _iEchoPin ;       // Output on which we check the echo
    long _lDistance ;     // Variable for the distance to an object
    long _lDuration ;     // Variable to hold how long we want the trigger pulse must last

    // Variables for Servo
    Servo _MyServo ;
    int _iServoPin ;      // Input on which servo is connected
    int _iDegrees ;       // To which position must the servo turn

    /*
    // Variables for motors
    int _iSpeed ;
    */ 
} ;

Robot Myrobot ;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600) ;

  //Myrobot.setupUltrasonicSensor(7, 8, 10) ;
  Myrobot.setupServo(4) ;
}

void loop()
{
  // put your main code here, to run repeatedly:
  //long lDistance = Myrobot.checkDistance() ;

  //Serial.println(lDistance) ;

  Myrobot.turnServo(60) ;
  delay(3000) ;

  Myrobot.turnServo(90) ;
  delay(3000) ;

  Myrobot.turnServo(120) ;
  delay(3000) ;
}

Robot::Robot()
{
  _MyServo = new Servo() ;
}

void Robot::setupServo(int iServoPin)
{
  _iServoPin = iServoPin ;
  _MyServo.attach(_iServoPin) ;
}

void Robot::turnServo(int iDegrees)
{
  _iDegrees = iDegrees ;
  _myServo.write(_iDegrees) ;
}

void Robot::setupUltrasonicSensor(int iTrigPin, int iEchoPin, long lDuration)
{
  _iTrigPin = iTrigPin ;
  _iEchoPin = iEchoPin ;
  _lDuration = lDuration ;

  pinMode(_iTrigPin, OUTPUT) ;
  pinMode(_iEchoPin, INPUT) ;
}

long Robot::checkDistance()
{
  digitalWrite(_iTrigPin, LOW) ;
  delayMicroseconds(2) ;
  digitalWrite(_iTrigPin, HIGH) ;
  delayMicroseconds(10) ;
  digitalWrite(_iTrigPin, LOW) ;

  _lDuration = pulseIn(_iEchoPin, HIGH) ;

  _lDistance = (_lDuration / 2) / 29.1 ;

  return _lDistance ;
}

In my constructor I tried this now :

Robot::Robot()
{
  _MyServo = new Servo() ;
}

I just need to know how to declare a new object inside a class, I’m still missing something…

_myServo

vs

_MyServo

:)

And I don't think you need the new in the constructor.

Found the solution and for all the others who have the same problem this is the solution :

#include <Servo.h>

class Robot
{
  public:
    // Constructor
    Robot() ;

    // Functions for servo
    void setupServo(int iServoPin) ;  // Setup the servo on a certain input
    void turnServo(int iDegrees) ;    // Turn the servo to a certain position

  private:
    // Variables for Servo
    Servo _myServo ;   
    int _iServoPin ;      // Input on which servo is connected
    int _iDegrees ;       // To which position must the servo turn
} ;

Robot Myrobot ;

void setup()
{
  Myrobot.setupServo(4) ;
}

void loop()
{
  Myrobot.turnServo(60) ;
  delay(3000) ;

  Myrobot.turnServo(120) ;
  delay(3000) ;
}

Robot::Robot()
{
  _myServo = Servo();  
}

void Robot::setupServo(int iServoPin)
{
  _iServoPin = iServoPin ;
  _myServo.attach(_iServoPin) ;
}

void Robot::turnServo(int iDegrees)
{
  _iDegrees = iDegrees ;
  _myServo.write(_iDegrees) ;
}

Delete this statement:

_myServo = Servo() ;

Make all your references to your private variable consistent. Is it "_myServo" or "_MyServo"?

Declare that variable in the class declaration:

Servo _MyServo;

EDIT ...

Our posts crossed :)

damn,

so that means I was correct in the first solution, I only missed because I had a capital letter ???

:(

thanks for the help!