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.
#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 ;
}
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...