Servo

Modified on Wed, 04 Jan 2023 at 06:42 PM

Our library lets you control your servos in HITIPanel with sub-degree resolution, while the standard library only permits degree resolution. This feature allows for finer positioning (the position resolution of a servo is around 0.1°).


The first thing you have to do is to attach a Servo to a pin during the setup() using HC_attachServo(). Once attached, you can control your servo in millidegrees or in microseconds.



Attaching a servo to a pin

When attaching a servo to a pin, several optional parameters can be set, namely the initial position where the servo must immediately go, and the servo min pulse and max pulse widths.


The servo variable can be created internally by the library or it can be supplied by you. You may need to supply your own servo variable if it must be shared and used by multiple libraries at the same time. In this case, add your servo variable as the last parameter.

Syntax :
  • HC_attachServo(pin)
  • HC_attachServo(pin, initial position)
  • HC_attachServo(pin, min pulse, max pulse)
  • HC_attachServo(pin, position, min pulse, max pulse)
  • HC_attachServo(…, &servo)
Parameters :
  • pin (byte): pin number 
  • initial position (unsigned long): initial position. Default : 90000 m° 
  • min pulse (unsigned int): min pulse width. Default : 544 us
  • max pulse (unsigned int): max pulse width. Default : 2400 us
  • servo (&Servo): Servo variable supplied by you



Detaching a servo from a pin

  • HC_detachServo(pin)


Example 1 : Servo variable internally created

Refer to this example : Servo.

#include <HITIComm.h>

// pins assignment
const int pin_Servo = 8;

void setup()
{
    // initialize library
    HC_begin();

    // attach Servo to the pin. Initial position is 53.7°.
    HC_attachServo(pin_Servo, 53700);
}

void loop()
{
    // communicate with HITIPanel
    HC_communicate();
}


Example 2 : Servo variable created and supplied by you

#include <HITIComm.h>

// pins assignment
const int pin_Servo = 8;

// Servo variable created by you
Servo servo;

void setup()
{
    // initialize library
    HC_begin();

    // attach Servo to the pin. Initial position is 90°.
    HC_attachServo(pin_Servo, &servo); // don’t forget the &
}

void loop()
{
    // communicate with HITIPanel
    HC_communicate();
}



Position control (in millidegrees)

  • HC_servoWrite(pin, setpoint)  => write a setpoint (unsigned long) to the servo
  • HC_servoRead(pin)                    => return the setpoint (unsigned long)


The meaning of the setpoint value depends on your servo type:

  • standard                      => setpoint = position:   0 (0°),       90000 (90°),            180000 (180°)
  • continuous rotation  => setpoint = speed:      0 (CCW),   90000 (stopped),   180000 (CW)


Example

Refer to this example : Digital Data (Switch).



Position control (in microseconds)

  • HC_servoWriteMicroseconds(pin, setpoint)  => write a setpoint (unsigned int) to the servo
  • HC_servoReadMicroseconds(pin)                    => return the setpoint (unsigned int)


The meaning of the setpoint value depends on your servo type:

  • standard                       => setpoint = position:   700-1000 (0°),   1500 (90°),   2000-2300 (180°)
  • continuous rotation  => setpoint = speed:   700-1000 (CCW),   1500 (stopped),   2000-2300 (CW)






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article