Microsoft MakeCode

Servo-write block creation

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
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
Any help would be great!

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)
}
}

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

When trying to build:

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

Thanks!

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

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 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!

For which board is your extension?

For the Adafruit Feather M4 express.

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)

}

}

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

1 Like

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
And it works really well. The code behind it is:

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

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)

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

1 Like