

You’ll notices from the code that the setAngle() member function is declared under the public: line. SmallServo->setAngle( 90) standardServo->setAngle( 90) So if you want an object, which function are you going to call? Assign the returned object to a variable ( smallServo and standardServo above).Call a function to instantiate the class and set up the internals of the instance, returning a reference to the created object.The syntax in the snippet above is actually short-hand for a two-step process:

So in order to provide the flexibility needed, a function is called to instantiate a class by setting up all the things needed for it to work as expected. This is because the attributes of an object are fancier than ints or chars - objects can have other objects as variables. If objects are like variables of a class, then why do we have brackets and pass in arguments like a function? Let’s look back to the objects and classes to variables and types analogy. The variables inside objects are not normally manipulated from ‘outside’ the object. Unlike variables, we work with objects by calling the methods (functions) defined by the class. In fact that’s one way to think about classes and objects - types and variables with fancier internals. The syntax to instantiate a class is very similar to creating variables of a certain type. SmallServo = new ServoMotor ( 9, 500, 2500) // initialize small servo (500us to 2500us) at pin 9 standardServo = new ServoMotor ( 10, 500, 2500) // initialize standard servo (0us to 2500us) at pin 10 import the Arduino Servo library #include #define ABSOLUTE_ANGLE_CHANGE 5 // variables to hold program data int incrementButton = 2 int decrementButton = 3 int currentAngle = 90 // class to control servo motors class ServoMotor Here’s what the circuit looks like fully assembled: Don’t worry if the servo rotates a bit when you power it on - this is expected. Connect the orange wire of small servo to pin 9 on the Arduino Dock, and the signal of the standard servo to pin 10.ġ. * Repeat for the second debounce circuit, but plug the jumper into pin 3 insteadġ. Connect the point in the first debounce circuit between the 5.1kΩ resistor and the capacitor to pin 2 of the Arduino Dock. Connect the negative power rail to a GND pin on the Arduino Dock.ġ. Now that our components are set up, we’ll connect them to the Arduino Dock:ġ. We’ll connect the orange (signal) wire in a bit. The small servo has 3 wires: connect the brown wire to the GND rail, the red wire to the Vcc rail. Instead of connecting 51kΩ resistor to an empty row, we’ll connect them to the Vcc rail.ġ. Set up two push buttons, each with their own debounce circuit similar to tutorial 4, don’t connect to Arduino Dock yet.ġ. Let’s first set up the two push buttons with their seperate debounce circuits same as previous tutorials:ġ. Now that you got everything, let’s build! Manually moving the servo arm is possible when unpowered, but it should not be done as it can damage the servo.

When the servo is unpowered and sent no signals, it won’t actively try to restore position. When the servo is receiving signals continuously, it will apply force to attempt to stay in the position that is being signalled. It will respond with appropriate increments when the pulse width is increased up until 2500us, then it will stop moving. The servo will rotate its arm to the 0° position in response - no matter which position the arm was in before. To put it all together, say we send a 500us width pulse to a servo accepting pulses between 500us and 2500us. They accept pulses within a fixed range commonly between 5us. Most servos fixedly rotate between 0° and 180° - starting and ending at fixed points relative to the motor. Servo motors usually have three wires: power, ground and the control signal. Servo motors are controlled by via a pulse width modulated (PWM) signal.
