# Servo-write block creation

**URL:** <https://forum.makecode.com/t/servo-write-block-creation/3024>\
**Category:** Maker\
**Created:** [August 5, 2020, 11:14am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024 "2020-08-05T11:14:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 5, 2020, 11:14am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/1 "2020-08-05T11:14:07Z")

</div>

Hello, guys I need some help. I’m trying to create an extension that contains a block similar to the existing servo write block, located in the default editor, “pins” category:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/51d42be18c7c5458c6c278ae82fc6580a804b960.png)  
I found the C++ code of it:  
/\*\*  
\* Write a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with `0` being full-speed in one direction, `180` being full speed in the other, and a value near `90` being no movement).  
\* @param name pin to write to, eg: AnalogPin.P0  
\* @param value angle or rotation speed, eg:180,90,0  
\*/  
//% help=pins/servo-write-pin weight=20  
//% blockId=device\_set\_servo\_pin block=“servo write|pin %name|to %value” blockGap=8  
//% parts=microservo trackArgs=0  
//% value.min=0 value.max=180  
//% name.fieldEditor=“gridpicker” name.fieldOptions.columns=4  
//% name.fieldOptions.tooltips=“false” name.fieldOptions.width=“250”  
void servoWritePin(AnalogPin name, int value) {  
PINOP(setServoValue(value));  
}

But I’m unable to convert it into a block on my extension, I got some errors when trying to build.  
My extension is: [https://github.com/Dwagga/pxt-centrifuge2](https://github.com/Dwagga/pxt-centrifuge2)  
Any help would be great!

---

<div class="post-metadata">

**Author:** ![MKleinSB](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mkleinsb/32/593_2.png) [@MKleinSB](https://forum.makecode.com/u/MKleinSB)\
**Post date:** [August 6, 2020, 7:39am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/2 "2020-08-06T07:39:47Z")

</div>

What about using Javascript?

//% help=pins/servo-write-pin weight=20  
//% blockId=device\_set\_servo\_pin block=“servo write|pin %name|to %value” blockGap=8  
//% parts=microservo trackArgs=0  
//% value.min=0 value.max=180  
//% name.fieldEditor=“gridpicker” name.fieldOptions.columns=4  
//% name.fieldOptions.tooltips=“false” name.fieldOptions.width=“250”  
//% weight=100 color=#0fbc11  
export function servo(name:number ,value: number): void {  
pins.servoWritePin(name, value)  
}  
}

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 6, 2020, 6:43pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/3 "2020-08-06T18:43:32Z")

</div>

@MKleinSB Thank you so much! It worked 😊  
Can I ask you something else? I still got some errors in my extension: [https://github.com/Dwagga/pxt-centrifuge2](https://github.com/Dwagga/pxt-centrifuge2)

When trying to build:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/a/a8ce5417f5b58d000db1b38055ec05b15c18635f.png)

I have only the dependency “core”, should I add some other dependencies? I don’t really know if that would fix the error.

Thanks!

---

<div class="post-metadata">

**Author:** ![MKleinSB](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mkleinsb/32/593_2.png) [@MKleinSB](https://forum.makecode.com/u/MKleinSB)\
**Post date:** [August 6, 2020, 6:56pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/4 "2020-08-06T18:56:04Z")

</div>

Sorry, I make all my extensions with the github integration of MakeCode.

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 6, 2020, 7:11pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/5 "2020-08-06T19:11:30Z")

</div>

Sorry @MKleinSB I didn’t understand what you mean. I’m sorry for my lack of experience, I’m a beginner. I can also use github integration of MakeCode. For example: [https://github.com/Dwagga/github-integration](https://github.com/Dwagga/github-integration) I just created this extension, from a MakeCode project.  
I also added the code you sent me and everything seems to be okay since the block shows up in the editor. However I got the same error: “servoWritePin” does not exist on type “typeof pins”.

Any idea worth to try?

Thank you!

---

<div class="post-metadata">

**Author:** ![MKleinSB](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mkleinsb/32/593_2.png) [@MKleinSB](https://forum.makecode.com/u/MKleinSB)\
**Post date:** [August 6, 2020, 7:53pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/6 "2020-08-06T19:53:13Z")

</div>

For which board is your extension?

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 6, 2020, 8:07pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/7 "2020-08-06T20:07:43Z")

</div>

For the Adafruit Feather M4 express.

---

<div class="post-metadata">

**Author:** ![MKleinSB](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mkleinsb/32/593_2.png) [@MKleinSB](https://forum.makecode.com/u/MKleinSB)\
**Post date:** [August 7, 2020, 7:39am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/8 "2020-08-07T07:39:14Z")

</div>

Does this lead to anything?

/\*\*

- Own Blocks

\*/

//% weight=100 color=#0fbc11 icon=“”

namespace centrifuge {

//% block

export function servo(mypin:PwmOnlyPin ,value: number): void

{

mypin.servoWrite(value)

}

}

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 7, 2020, 10:04am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/9 "2020-08-07T10:04:52Z")

</div>

@MKleinSB It worked! You are so great!! Thank you so much 😊 😊 😊 wish I could help you as well.

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 11, 2020, 11:59pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/10 "2020-08-11T23:59:52Z")

</div>

Hello guys!

With some help, I could customize my block to run a servo motor. However, I’ve got one more question. My block looks like: ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/6de83905e129ab42a565807bb5bda58b13aeb119.png)  
And it works really well. The code behind it is:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b60451f3812e406856b82b96bc42f93e8befae3e.png)

I would like not to let the user select PIN (in this case A1). I would like always to execute the motor on the pin D13, where it is installed. So the user will only need to choose the power (160, 100, 90, etc).

I’ve tried things like:

- D13.servoWrite(value)  
or
- var D13: PwmOnlyPin;  
D13.servoWrite(value)

But none of these worked. Does anyone know how to do it? @peli @MKleinSB @mmoskal @richard

Thank you!

David

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [August 12, 2020, 12:12am UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/11 "2020-08-12T00:12:03Z")

</div>

In that case you should get rid of the mypin argument to the function and only take in the power. then, just use pins.D13 in your implementation instead of mypin! (also remember to remove %mypin from the block definition)

---

<div class="post-metadata">

**Author:** ![Dwagga](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dwagga/32/987_2.png) [@Dwagga](https://forum.makecode.com/u/Dwagga)\
**Post date:** [August 14, 2020, 6:43pm UTC](https://forum.makecode.com/t/servo-write-block-creation/3024/12 "2020-08-14T18:43:00Z")

</div>

@richard Thank you so so much! It worked!  
My project is almost finished 😊
