With Crickit support in MakeCode, you should have a green CRICKIT block group available. Clicking on that group pops out various blocks for Crickit capabilities including those related to motor control as shown below.
We want to turn the servo horn on command from one side (0 degrees) to the other (180 degrees).
This program will move a servo to the zero degree point when the Circuit Playground Express button A is pressed. It will move the servo to 180 degrees when button B is pressed.
This trips up everyone the first time they use a servo - while the servos are often talked about in terms of 0 to 180 degree motion, there can be variation from servo to servo. Some metal gear servos only move 90 degrees. Others may need longer 'pulse lengths' and we won't go into that there. There's nothing wrong with your servo just because it doesn't move a full 180 degrees! Visit https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit/makecode-servos#precise-pulses for more details on how to customize the pulse lengths!
The code below creates a stepped movement from 0 to 180 and back again. The timing is variable with the pause blocks. This is a common use in terms of things like wings flapping or other movements.
If you find the movement a bit jerky, you can increase the
for index from 0 to and set it to
180 then take out the divide by
10 in the
crickit set servo 1 angle to block. You might have to play with the
pause time then if you feel the motion is too slow.