Servo.Attach does not work in my Library!

Good morning,

I have a Motor.cpp and a Motor.h library files that are being called in my .ino file.

I instantiate a variable of type Motor, which contains a Servo typed variable.

When I try to write a different speed to the motor, I check if the pin is attached in a while loop.

The code never passes this while loop. Any thoughts?

My code is attached to this post.

Thanks in advance!

Motor.cpp (959 Bytes)

Motor.h (604 Bytes)

quad.ino (1.17 KB)

Why not post the code?

By all means, I just thought it would be simpler that way:

Motor.cpp:

/*
 Motor.cpp
 
 Library for changing each of the motor's specifications.
 
*/

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

Motor::Motor(int pin/*, bool clockwise*/)
{  
 setPin(pin);
 //setClockwise(clockwise);
 //setState(false);
 //setCurrentSpeed(0);
}

void Motor::setPin(int pin)
{
 
 motor.detach();
 myPin = pin;
 motor.attach(pin);
}

int Motor::getPin()
{
 return myPin;
}

void Motor::setCurrentSpeed(int currentSpeed)
{
 while(motor.attach(myPin) == 0)
   motor.attach(myPin);
   
 Serial.println("Changing speed to " + String(currentSpeed) + ".");
 myCurrentSpeed = currentSpeed;
 motor.write(myCurrentSpeed);
}

int Motor::getCurrentSpeed()
{
 return myCurrentSpeed;
}

Motor.h:

/*
 Motor.h
 
 Library for controlling each of the motors of the quadcopter
 
*/

#ifndef MOTOR_H
#define MOTOR_H

#include "Arduino.h"
#include "Servo.h"

class Motor 
{
 public:
   Motor(int pin/*, bool clockwise*/);

   void setPin(int pin);
   int getPin();
   
   void setCurrentSpeed(int currentSpeed);
   int getCurrentSpeed();
   /*
   void setState(bool state);
   bool getState();
   
   void setClockwise(bool clockwise);
   bool getClockwise();*/
   
 private:
   Servo motor;
   /*bool myClockwise;
   bool myState;*/
   int myPin;
   int myCurrentSpeed;
};

#endif

.ino:

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

#define  SERIAL_PORT_SPEED  19200

//Motors motors;

//Servo motors[4];

Motor teste(5);

//Servo motor;

void setup() {
 Serial.begin(SERIAL_PORT_SPEED);
 Serial.println("Setting things up.");

// motor.attach(5);
//  motor.write(0);
 /*motors[0].attach(6);
 motors[1].attach(9);
 motors[2].attach(10);
 motors[3].attach(11);

 motors[0].write(0);
 motors[1].write(0);
 motors[2].write(0);
 motors[3].write(0);*/
 
 Serial.println("Setting up completed.");
}

void loop() {
 /*Serial.print("Motor: "); 
 while (Serial.available()==0);
 int motor = Serial.parseInt();  
 */
 Serial.print("Speed (0 - 100): "); 
 while (Serial.available()==0);
 int speed = Serial.parseInt();
 Serial.println(speed); 
 Serial.println("Changing speed of motor 1 to " + String(speed) + ".");                 
 teste.setCurrentSpeed(speed);
// motor.write(speed);

 /*
 motors[motor - 1].write(map(speed, 0, 100, 0, 180));
 while (Serial.available()==0);
 int s = Serial.parseInt(); 

 if(s != 0)
 {
   teste.setA(s);
   Serial.println("New myA: " + String(teste.getA()));
 } else {
   teste.printRandom("Just some random shit!!");
 }*/
}

Is this relevant in any way?

I'd rather hoped you'd use code tags.

I'm so sorry, didn't realize those actually existed. I've edited the post.

I've gone through all that documentation and nothing :frowning:

I've changed the while loop to:

while(!motor.attached())
motor.attach(myPin);

However, it still does not output the speed value to the motor.

Managed to figure it out somehow.

I was calling the Motor constructor before the Arduino's setup. This won't work.

For making it work, just attach the motor to its pin in the Arduino's setup function and you are good to go.

Thanks for the help.

Static instances have their constructors called very early - before the chip's been initialized.

Use a begin() method for initializing anything other than memory.