module main author unknown version 1 0 description '' script 115 50 { whenStarted comment '## Steuerung für Winkelservo und Rotationsservo ## Anschlüsse siehe https://calliope.cc/calliope-mini/technische-daten Ein Servo hat einen dunklen Draht (GND, braun oder schwarz) einen roten Draht (5 Volt) und einen Signaldraht (Signal, gelb oder orange). Die Blöcke aus dem Calliope Servo Paket sprechen die Servos an den Pins C8, C9, C14 und C15 an. Klar würden auch andere Pins funktionieren, aber das Servoboard ist fest verdrahtet. Man kann das Servoboard nehmen oder die Kabel direkt stecken. Der graue Lego-Servo ist ein 270°-Winkelservo mit einem drehbereich von -135° bis 135°. Der grüne Lego-Servo ist ein Rotationsservo, der langsam dreht, aber verhältnismäßig stark ist. ' '[display:mbDisplay]' 15237440 } script 281 364 { csb_AngleServo '270°' 'C8' 0 } script 284 418 { csb_AngleServo '270°' 'C8' -90 } script 286 472 { csb_AngleServo '270°' 'C8' 90 } script 285 594 { csb_ContinousServo 'C9' 0 } script 288 653 { csb_ContinousServo 'C9' -100 } script 290 704 { csb_ContinousServo 'C9' 100 } script 656 939 { setServoAngle 8 135 } module 'Calliope Servoboard' Output author 'Ralf Krause' version 1 3 depends Servo choices csb_servoType '180°' '270°' '360°' choices csb_motorPort M0 M1 'M0+M1' choices csb_servoPort C8 C9 C14 C15 description 'Calliope Servoboard https://calliope.cc/en/calliope-mini/accessories/servoboard ServoPorts C8, C9, C14, C15 MotorPorts M0, M1 ' spec ' ' 'csb_AngleServo' 'Calliope set _ servo _ to angle _ °' 'menu.csb_servoType menu.csb_servoPort num' '180°' 'C8' 90 spec ' ' 'csb_ContinousServo' 'Calliope set continous servo _ to speed _ (-100~100)' 'menu.csb_servoPort num' 'C8' 100 spec ' ' 'csb_StopServo' 'Calliope stop servo _' 'menu.csb_servoPort' 'C8' space spec ' ' 'csb_SetMotor' 'Calliope set motor _ to speed _ (-100~100)' 'menu.csb_motorPort num' 'M0' 100 spec ' ' 'csb_StopMotor' 'Calliope stop motor _' 'menu.csb_motorPort' 'M0' space spec ' ' 'csb_StopAll' 'Calliope stop all motors and servos' to csb_AngleServo servoType servoPort angle { comment 'angle range maps from -servoType/2 to +servoType/2' local 'pin' 8 if ('C9' == servoPort) { pin = 9 } if ('C14' == servoPort) { pin = 14 } if ('C15' == servoPort) { pin = 15 } local 'angleRange' 90 if ('270°' == servoType) { angleRange = 135 } if ('360°' == servoType) { angleRange = 180 } setServoAngle pin ((angle * 90) / angleRange) } to csb_ContinousServo servoPort speed { local 'port' 8 if ('C9' == servoPort) { port = 9 } if ('C14' == servoPort) { port = 14 } if ('C15' == servoPort) { port = 15 } setServoSpeed port speed } to csb_SetMotor motorPort speed { local 'direction' (booleanConstant true) if (speed < 0) { direction = (booleanConstant false) } speed = (((absoluteValue speed) * 1023) / 100) digitalWriteOp 31 true if (motorPort == 'M0') { digitalWriteOp 32 direction analogWriteOp 33 speed } if (motorPort == 'M1') { digitalWriteOp 34 direction analogWriteOp 35 speed } if (motorPort == 'M0+M1') { digitalWriteOp 32 direction analogWriteOp 33 speed digitalWriteOp 34 direction analogWriteOp 35 speed } } to csb_StopAll { csb_StopServo 'C8' csb_StopServo 'C9' csb_StopServo 'C14' csb_StopServo 'C15' csb_StopMotor 'M0' csb_StopMotor 'M1' } to csb_StopMotor motorPort { csb_SetMotor motorPort 0 } to csb_StopServo servoPort { local 'port' 8 if ('C9' == servoPort) { port = 9 } if ('C14' == servoPort) { port = 14 } if ('C15' == servoPort) { port = 15 } stopServo port } module 'LED Display' Output author MicroBlocks version 1 14 choices led_imageMenu heart 'small heart' yes no happy sad confused angry asleep surprised silly fabulous meh 't-shirt' 'roller skate' duck house tortoise butterfly 'stick figure' ghost sword giraffe skull umbrella snake rabbit cow 'quarter note' 'eight note' pitchfork target triangle 'left triangle' 'chess board' diamond 'small diamond' square 'small square' scissors description 'Display primitives for the 5x5 LED display on the BBC micro:bit, Calliope mini and M5Atom Matrix. Boards with TFT displays (such as the Citilab ED1 or the M5Stack family) support these primitives with a simulated "fat pixel" display. ' variables _stop_scrolling_text spec ' ' '[display:mbDisplay]' 'display _' 'microbitDisplay' 15237440 spec ' ' 'led_displayImage' 'display image _ : x _ y _' 'menu.led_imageMenu num num' 'happy' 1 1 spec ' ' '[display:mbDisplayOff]' 'clear display' space spec ' ' '[display:mbPlot]' 'plot x _ y _' 'num num' 3 3 spec ' ' '[display:mbUnplot]' 'unplot x _ y _' 'num num' 3 3 space spec ' ' 'displayCharacter' 'display character _' 'str' 'A' spec ' ' 'scroll_text' 'scroll text _ : pausing _ ms' 'str num' 'HELLO ROSA!' 100 spec ' ' 'stopScrollingText' 'stop scrolling' advanced spec ' ' 'set display color' 'set display color _' 'color' spec 'r' 'led_image' 'LED image #BR# _' 'microbitDisplay' 15237440 space spec 'r' '_led_namedImage' '_led_namedImage _' 'menu.led_imageMenu' 'happy' spec 'r' '_led_imageData' '_led_imageData' to '_led_imageData' { return 'heart:4685802,small heart:145728,yes:2269696,no:18157905,happy:15237440,sad:18284864,confused:22348096,angry:23036241,asleep:459616,surprised:4526090,silly:25984017,fabulous:15008639,meh:2236443,t-shirt:15154043,roller skate:11534104,duck:489702,house:10976708,tortoise:359872,butterfly:29332475,stick figure:18158564,ghost:23068334,sword:4657284,giraffe:10946627,skull:15171246,umbrella:6460398,snake:469859,rabbit:16104613,cow:4685361,quarter note:7573636,eight note:7590276,pitchfork:4357813,target:4681156,triangle:1026176,left triangle:32805985,chess board:11184810,diamond:4539716,small diamond:141440,square:33080895,small square:469440,scissors:20287859,' } to '_led_namedImage' name { local 'data' ('_led_imageData') local 'i' ('[data:find]' name data) if (i == -1) { comment 'Name not found' return 0 } local 'start' (('[data:find]' ':' data i) + 1) local 'end' (('[data:find]' ',' data i) - 1) return ('[data:convertType]' ('[data:copyFromTo]' data start end) 'number') } to displayCharacter s { s = ('[data:join]' '' s) if ((size s) == 0) { '[display:mbDisplayOff]' return 0 } '[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s)) 1 1 } to led_displayImage imageName optionalX optionalY { local 'image' imageName if (isType image 'string') { image = ('_led_namedImage' imageName) } '[display:mbDrawShape]' image (argOrDefault 2 1) (argOrDefault 3 1) } to led_image twentyFiveBitInt { comment 'An LED image is a 25-bit integer' return twentyFiveBitInt } to scroll_text text optionalDelay { text = ('[data:join]' '' text) local 'delay' 100 if ((pushArgCount) > 1) { delay = optionalDelay } _stop_scrolling_text = (booleanConstant false) if ('Pico:ed' == (boardType)) { for position (((size text) * 6) + 18) { if _stop_scrolling_text {return 0} '[display:mbDisplayOff]' '[tft:text]' text (17 - position) 0 (colorSwatch 125 125 125 255) 1 true waitMillis (delay / 2) } } ('kidsIOT' == (boardType)) { for position (((size text) * 6) + 21) { if _stop_scrolling_text {return 0} '[tft:deferUpdates]' '[tft:clear]' '[tft:text]' text (128 - (6 * position)) 6 (colorSwatch 255 255 255 255) 6 false '[tft:resumeUpdates]' waitMillis (delay / 8) } } else { for position (((size text) * 6) + 6) { if _stop_scrolling_text {return 0} for i (size text) { '[display:mbDrawShape]' ('[display:mbShapeForLetter]' ('[data:unicodeAt]' i text)) (((i * 6) + 2) - position) 1 } waitMillis delay } } } to 'set display color' color { '[display:mbSetColor]' color } to stopScrollingText { _stop_scrolling_text = (booleanConstant true) waitMillis 10 '[display:mbDisplayOff]' } module Servo Output author MicroBlocks version 1 4 tags servo motor angle rotation position description 'Control both positional (angle) and rotational servo motors. ' variables _servoPin _servoPulseWidth spec ' ' 'setServoAngle' 'set servo _ to _ degrees (-90 to 90)' 'num num' 1 90 spec ' ' 'setServoSpeed' 'set servo _ to speed _ (-100 to 100)' 'num num' 1 100 spec ' ' 'stopServo' 'stop servo _' 'num' 1 spec 'r' '_servoIndex' '_servoIndex _' 'num' 1 spec ' ' '_servoPulse' '_servoPulse pin _ usecs _' 'num num' 1 1500 spec ' ' '_servoUpdateLoop' '_servoUpdateLoop' to '_servoIndex' which { if (_servoPin == 0) { _servoPin = ('[data:makeList]') _servoPulseWidth = ('[data:makeList]') sendBroadcast '_servoUpdateLoop' } local 'i' ('[data:find]' which _servoPin) if (i < 0) { comment 'Add new pin' '[data:addLast]' which _servoPin '[data:addLast]' 1500 _servoPulseWidth i = (size _servoPin) } return i } to '_servoPulse' pin usecs { if (usecs == 0) { comment 'Servo stopped; do nothing' return 0 } usecs = (maximum 500 (minimum usecs 2900)) comment 'Split wait into a long wait followed by a wait of <= 30 usecs for greater accuracy' local 'endTime' ((microsOp) + usecs) digitalWriteOp pin true waitMicros (usecs - 30) waitMicros (endTime - (microsOp)) digitalWriteOp pin false } to '_servoUpdateLoop' { forever { if (_servoPin != 0) { comment 'If the _servoPin list is not 0, update the servos' for i (size _servoPin) { local 'pin' (at i _servoPin) local 'usecs' (at i _servoPulseWidth) if (and (pin >= 0) (usecs != 0)) { '_servoPulse' pin usecs } } waitMillis 15 } } } to setServoAngle which degrees optionalReverse { local 'reversed' false if ((pushArgCount) > 2) { reversed = optionalReverse } if reversed { degrees = (0 - degrees) } local 'pulseWidth' (1500 - (10 * degrees)) if ('[io:hasServo]') { '[io:setServo]' which pulseWidth } else { atPut ('_servoIndex' which) _servoPulseWidth pulseWidth } } to setServoSpeed which speed optionalReverse { local 'reversed' false if ((pushArgCount) > 2) { reversed = optionalReverse } if reversed { speed = (0 - speed) } local 'pulseWidth' (1500 - (10 * speed)) if ((absoluteValue speed) < 2) { pulseWidth = 0 } if ('[io:hasServo]') { '[io:setServo]' which pulseWidth } else { atPut ('_servoIndex' which) _servoPulseWidth pulseWidth } } to stopServo which { if ('[io:hasServo]') { '[io:setServo]' which 0 } else { atPut ('_servoIndex' which) _servoPulseWidth 0 } }