pxt-calliope/docs/reference/pins/servo-write-pin.md

49 lines
1.2 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Servo Write Pin
2016-07-01 22:03:53 +02:00
Write a value to the servo on the specified [pin](/device/pins)
and control the shaft.
2016-03-26 00:47:20 +01:00
2016-07-01 22:03:53 +02:00
This function will move the shaft of a standard servo to the specified
angle, or set the speed of a continuous rotation servo. (`0` specifies
full speed in one direction, `180` specifies full speed in the other,
and approximately `90` specifies no movement.)
2016-03-26 00:47:20 +01:00
```sig
pins.servoWritePin(AnalogPin.P0, 180)
```
### Parameters
2016-07-19 00:51:28 +02:00
* ``name``: a [string](/reference/types/string) that specifies the pin name (`P0` through `P4`, or `P10`)
* ``value``: a [number](/reference/types/number) from `0` through `180`
2016-03-26 00:47:20 +01:00
### Examples
2016-07-01 22:03:53 +02:00
#### Setting the shaft angle to midpoint on a servo
2016-03-26 00:47:20 +01:00
```blocks
pins.servoWritePin(AnalogPin.P0, 90)
```
2016-07-01 22:03:53 +02:00
#### Controlling the shaft by using the tilt information of the accelerometer
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
2016-08-10 22:46:11 +02:00
let millig = input.acceleration(Dimension.X)
2016-03-26 00:47:20 +01:00
// map accelerometer readings to angle
let angle = pins.map(millig, -1023, 1023, 0, 180)
pins.servoWritePin(AnalogPin.P0, angle)
})
```
2016-07-01 22:03:53 +02:00
#### Setting the full speed on a continuous servo
2016-03-26 00:47:20 +01:00
```blocks
pins.servoWritePin(AnalogPin.P0, 0)
```
### See also
2016-04-13 17:27:45 +02:00
[BBC micro:bit pins](/device/pins), [servo set pulse](/reference/pins/servo-set-pulse)
2016-03-26 00:47:20 +01:00