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:
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!
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”.
With some help, I could customize my block to run a servo motor. However, I’ve got one more question. My block looks like:
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).
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)