# Problem with class for servos

**URL:** <https://forum.arduino.cc/t/problem-with-class-for-servos/871594>\
**Category:** Programming\
**Created:** [June 7, 2021, 9:34am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594 "2021-06-07T09:34:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![philhermann](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@philhermann](https://forum.arduino.cc/u/philhermann)\
**Post date:** [June 7, 2021, 9:34am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/1 "2021-06-07T09:34:46Z")

</div>

Hey internet,

I've got a problem with a class for controlling a servo. In the constructor the servo should be initialized and set to 0 degrees. What actually happens: The Arduino (UNO R3) sends "Se" to serial, after that nothing happens. I don't think the program has any problems with memory, since there are not that many variables and I don't even work with pointers.

I'll try to include only as much code as needed, for comments you need to scroll right:

```cpp
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <Ramp.h>

#define SERVOMIN 460 //Minimum PWM for servo
#define SERVOMAX 1075 //Maximum PWM for servo
#define SERVOMINDEG 30 //Minimum servo rotation
#define SERVOMAXDEG 120 //Maximum servo rotation

class ServoMotor{ //New class: ServoMotor
  private:
    int servopwm; //declaring member for current pwm
    int servonum; //declaring member for servo number
    Adafruit_PWMServoDriver& driver; //declaring reference to the driver

  public:
    void drive(float deg);

    ServoMotor(int servonum, Adafruit_PWMServoDriver& driver): servonum(servonum), driver(driver){ //constructor
      Serial.begin(9600); //begin serial communication
      Serial.print("Servo is getting initialized. Servo number: "); //print message
      Serial.println(servonum); //print servo number
      driver.begin(); //begin I²C communivation with driver
      driver.setPWMFreq(125); //set driver pwm frequency to 125 Hz
      delay(10); //wait for 10 ms
      this->servopwm=SERVOMIN; //initialize current pwm with minimum value
      driver.setPWM(this->servonum, 0, this->servopwm); //set pwm for servo to initial position
      return;                                                                           
    }

    ~ServoMotor(){
      //empty destructor
    }
};

  Adafruit_PWMServoDriver ServoDriver = Adafruit_PWMServoDriver(); //driver object is initialized
  ServoMotor shoulder(0, ServoDriver); //servo object is initialized

void setup() {
  //nothing to do here
}

void loop() {
  shoulder.drive(90); //drive shoulder servo to 90 deg
  delay(2000);
  shoulder.drive(120);
  delay(2000);
  shoulder.drive(30);
  delay(2000);
}

```

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [June 7, 2021, 9:48am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/2 "2021-06-07T09:48:57Z")

</div>

don't put hardware related functions in the constructor.  
make a begin() method with all your hardware initialization and call it in setup().  
Additionally, reconsider, if Serial.begin() should really be called within your class.

Furthermore, consider to use const variables for your constants and not precompiler defines.

Will int servonum ever get negative? Is there any reason to use a signed integer for that?

Are you really using pin 0 for your servo and you try to use Serial in parallel?

---

<div class="post-metadata">

**Author:** ![philhermann](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@philhermann](https://forum.arduino.cc/u/philhermann)\
**Post date:** [June 7, 2021, 10:16am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/3 "2021-06-07T10:16:59Z")

</div>

Hi, thanks for your answer!

I'll take unsigned int or short into consideration!

The servo(s) are driven via a pca9685, so 0 refers to pwm pin 0 on the breakout board. That all works - I've tried it before without the class.

Removing all the hardware related stuff from the constructor worked - thanks a lot. Ok well, I'm now getting "Servo is getting initialized. Ser⸮rյ⸮⸮⸮⸮ 0" and gibberish from that point BUT besides that the program does what I want it to do.

---

<div class="post-metadata">

**Author:** ![philhermann](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@philhermann](https://forum.arduino.cc/u/philhermann)\
**Post date:** [June 7, 2021, 10:23am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/4 "2021-06-07T10:23:07Z")

</div>

Ok, I now took char for servonum and unsigned short for servopwm. I think I'll let the preprocessor do the work because I don't want to reserve those 2048 bytes of dynamic memory just with global variables.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [June 7, 2021, 10:29am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/5 "2021-06-07T10:29:44Z")

</div>

if done right, the **compiler** will optimize it anyway.  
Try it - and compare the used globals.  
By the way ... if you care about global / SRAM, read about [F-Makro](https://www.arduino.cc/reference/de/language/variables/utilities/progmem/) ... Serial.println(F(... will save you more global /SRAM than one wrong integer 😉

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 5, 2021, 10:30am UTC](https://forum.arduino.cc/t/problem-with-class-for-servos/871594/6 "2021-10-05T10:30:32Z")

</div>

Closed on October 5, 2021, 10:30am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
