module main author unknown version 1 0 description '' script 96 53 { comment '## Distanzsensor HC-SR04 ## Anschluss am Calliope mit Grove-Kabel an A1 https://calliope.cc/calliope-mini/technische-daten Grove-Kabel scharz, rot, gelb, weiß verbinde: Trigger gelb an Pin 17, Echo weiß an Pin 16' digitalWriteOp 16 true digitalWriteOp 17 true forever { local 'Entfernung' ('distance (cm)' 17 16) sayIt Entfernung if (Entfernung < 400) { led_displayImage 'happy' } else { led_displayImage 'sad' } waitMillis 500 } } module 'Distance (HC-SR04)' Input author MicroBlocks version 1 5 tags sensor 'hc-sr04' distance ultrasound description 'Support for the HC-SR04 ultrasound distance sensor. Originally written by Joan Guillén & Josep Ferràndiz. Rewritten to use the new pulse capture mechanism.' variables _sr04_last _sr04_misses spec 'r' 'distance (cm)' 'distance (cm) trigger _ echo _' 'num num' 2 4 spec 'r' 'sr04_distanceOnePin' 'distance (cm) pin _' 'num' 0 spec 'r' '_sr04_getEchoData' '_getEchoData _ echo _' 'auto auto' 0 1 to '_sr04_getEchoData' trig echo { comment 'Output a 50 usec pulse on trigger pin to start distance measurement.' digitalWriteOp trig false waitMicros 2 digitalWriteOp trig true waitMicros 50 digitalWriteOp trig false comment 'Capture the distance pulse. Width of pulse is proportional to the distance.' '[sensors:captureStart]' echo local 'start' (millisOp) repeatUntil (('[sensors:captureCount]') >= 2) { if ((millisSince start) > 24) {exitLoop} waitMillis 1 } return ('[sensors:captureEnd]') } to 'distance (cm)' trig echo { comment 'Take a distance measurement. Pulses will be two element list if successful. If no echo detected it will have only one element. In rare error cases the second item will be negative.' local 'pulses' ('_sr04_getEchoData' trig echo) if (and ((size pulses) >= 2) ((at 2 pulses) > 0)) { _sr04_misses = 0 comment 'The divisor was determined empirically. It is less than the expected 2 * 343 meter/sec.' _sr04_last = ((10 * (at 2 pulses)) / 583) return _sr04_last } else { comment 'No echo, probably because target is out of range. To avoid short-term glitches, return the last distance unless the are N misses in a row, then return 400.' _sr04_misses += 1 return (ifExpression (_sr04_misses < 10) _sr04_last 400) } } to sr04_distanceOnePin pin { return ('distance (cm)' pin pin) } 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]' }