├── README.md ├── _locales └── zh │ └── sensors-strings.json ├── main.ts └── pxt.json /README.md: -------------------------------------------------------------------------------- 1 | # sensors 2 | 3 | 這是專為micro:bit提供的感應器積木 4 | 5 | ## License 6 | 7 | * MIT 8 | * The code of ultrasonic was coded by Microsoft 9 | https://github.com/Microsoft/pxt-sonar 10 | 11 | * The code of DHT11 was coded by MonadnockSystems 12 | https://github.com/MonadnockSystems/pxt-dht11 13 | 14 | 15 | ## Supported targets 16 | 17 | * for PXT/microbit 18 | (The metadata above is needed for package search.) 19 | 20 | ```package 21 | sensors=github:lioujj/pxt-sensors 22 | ``` 23 | -------------------------------------------------------------------------------- /_locales/zh/sensors-strings.json: -------------------------------------------------------------------------------- 1 | { 2 | "sensors|block": "感應器", 3 | "sensors.sensor_ping|block": "超音波感應器 trig腳位 %trig|echo腳位 %echo|取得距離,單位 %unit", 4 | "sensors.PingUnit.Centimeters|block":"公分", 5 | "sensors.PingUnit.Inches|block":"英吋", 6 | "sensors.PingUnit.MicroSeconds|block":"毫秒", 7 | "sensors.get_DHT11_value|block": "溫濕度感應器DHT11 腳位 %pin_arg|取得 %dhtResult", 8 | "sensors.Dht11Result.Celsius|block": "攝氏溫度", 9 | "sensors.Dht11Result.Fahrenheit|block": "華氏溫度", 10 | "sensors.Dht11Result.humidity|block": "濕度", 11 | "sensors.LEDType.cathode|block": "共陰極", 12 | "sensors.LEDType.anode|block": "共陽極", 13 | "sensors.RGBLight|block": "RGB LED 設定型態:%myType|紅色腳位 %RedPin|綠色腳位 %GreenPin|藍色腳位 %BluePin|紅色值(0~255) %RedValue|綠色值(0~255) %GreenValue|藍色值(0~255) %BlueValue" 14 | } 15 | -------------------------------------------------------------------------------- /main.ts: -------------------------------------------------------------------------------- 1 | //% weight=0 color=#3CB371 icon="\uf2db" block="sensors" 2 | namespace sensors { 3 | 4 | function signal_dht11(pin: DigitalPin): void { 5 | pins.digitalWritePin(pin, 0); 6 | basic.pause(18); 7 | let i = pins.digitalReadPin(pin); 8 | pins.setPull(pin, PinPullMode.PullUp); 9 | } 10 | 11 | function dht11_read(pin: DigitalPin): number { 12 | signal_dht11(pin); 13 | 14 | // Wait for response header to finish 15 | while (pins.digitalReadPin(pin) == 1); 16 | while (pins.digitalReadPin(pin) == 0); 17 | while (pins.digitalReadPin(pin) == 1); 18 | 19 | let value = 0; 20 | let counter = 0; 21 | 22 | for (let i = 0; i <= 32 - 1; i++) { 23 | while (pins.digitalReadPin(pin) == 0); 24 | counter = 0 25 | while (pins.digitalReadPin(pin) == 1) { 26 | counter += 1; 27 | } 28 | if (counter > 4) { 29 | value = value + (1 << (31 - i)); 30 | } 31 | } 32 | return value; 33 | } 34 | 35 | export enum Dht11Result { 36 | //% block="Celsius" 37 | Celsius, 38 | //% block="Fahrenheit" 39 | Fahrenheit, 40 | //% block="humidity" 41 | humidity 42 | } 43 | 44 | //% blockId=get_DHT11_value block="DHT11 set pin %pin_arg|get %dhtResult" blockExternalInputs=true 45 | //% pin_arg.fieldEditor="gridpicker" pin_arg.fieldOptions.columns=4 46 | //% pin_arg.fieldOptions.tooltips="false" pin_arg.fieldOptions.width="300" 47 | export function get_DHT11_value(pin_arg: DigitalPin, dhtResult: Dht11Result): number { 48 | switch (dhtResult) { 49 | case Dht11Result.Celsius: return (dht11_read(pin_arg) & 0x0000ff00) >> 8; 50 | case Dht11Result.Fahrenheit: return ((dht11_read(pin_arg) & 0x0000ff00) >> 8) * 9 / 5 + 32; 51 | case Dht11Result.humidity: return dht11_read(pin_arg) >> 24; 52 | default: return 0; 53 | } 54 | } 55 | 56 | export enum PingUnit { 57 | //% block="cm" 58 | Centimeters, 59 | //% block="inches" 60 | Inches, 61 | //% block="μs" 62 | MicroSeconds 63 | } 64 | 65 | export enum LEDType { 66 | //% block="cathode" 67 | cathode, 68 | //% block="anode" 69 | anode 70 | } 71 | 72 | //% blockId=sensor_ping block="ultrasonic trig %trig|echo %echo|get distance %unit" 73 | //% trig.fieldEditor="gridpicker" trig.fieldOptions.columns=4 74 | //% trig.fieldOptions.tooltips="false" trig.fieldOptions.width="300" 75 | //% echo.fieldEditor="gridpicker" echo.fieldOptions.columns=4 76 | //% echo.fieldOptions.tooltips="false" echo.fieldOptions.width="300" 77 | export function sensor_ping(trig: DigitalPin, echo: DigitalPin, unit: PingUnit, maxCmDistance = 500): number { 78 | // send pulse 79 | pins.setPull(trig, PinPullMode.PullNone); 80 | pins.digitalWritePin(trig, 0); 81 | control.waitMicros(2); 82 | pins.digitalWritePin(trig, 1); 83 | control.waitMicros(10); 84 | pins.digitalWritePin(trig, 0); 85 | 86 | // read pulse 87 | const d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 43); 88 | 89 | switch (unit) { 90 | case PingUnit.Centimeters: return d / 43; 91 | case PingUnit.Inches: return d / 110; 92 | default: return d; 93 | } 94 | } 95 | //% blockId=RGBLight block="set RGB type:common %myType|red pin %RedPin|green pin %GreenPin|blue pin %BluePin|value of red(0~255) %RedValue|value of green(0~255) %GreenValue|value of blue(0~255) %BlueValue" blockExternalInputs=false 96 | //% RedValue.min=0 RedValue.max=255 GreenValue.min=0 GreenValue.max=255 BlueValue.min=0 BlueValue.max=255 97 | //% RedPin.fieldEditor="gridpicker" RedPin.fieldOptions.columns=4 98 | //% RedPin.fieldOptions.tooltips="false" RedPin.fieldOptions.width="300" 99 | //% GreenPin.fieldEditor="gridpicker" GreenPin.fieldOptions.columns=4 100 | //% GreenPin.fieldOptions.tooltips="false" GreenPin.fieldOptions.width="300" 101 | //% BluePin.fieldEditor="gridpicker" BluePin.fieldOptions.columns=4 102 | //% BluePin.fieldOptions.tooltips="false" BluePin.fieldOptions.width="300" 103 | export function RGBLight(myType: LEDType, RedPin: AnalogPin, GreenPin: AnalogPin, BluePin: AnalogPin, RedValue: number, GreenValue: number, BlueValue: number): void { 104 | pins.analogWritePin(RedPin, pins.map((myType == LEDType.cathode ? RedValue : (255 - RedValue)), 0, 255, 0, 1023)); 105 | pins.analogWritePin(GreenPin, pins.map((myType == LEDType.cathode ? GreenValue : (255 - GreenValue)), 0, 255, 0, 1023)); 106 | pins.analogWritePin(BluePin, pins.map((myType == LEDType.cathode ? BlueValue : (255 - BlueValue)), 0, 255, 0, 1023)); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /pxt.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sensors", 3 | "version": "0.0.2", 4 | "description": "這是專為micro:bit提供的感應器積木", 5 | "license": "MIT", 6 | "dependencies": { 7 | "core": "*" 8 | }, 9 | "files": [ 10 | "README.md", 11 | "main.ts", 12 | "_locales/zh/sensors-strings.json" 13 | ], 14 | "public": true 15 | } --------------------------------------------------------------------------------