目录

Micro:bit 专题

参考资料

  • micro:bit 是由英国广播电视公司(BBC)为青少年编程教育设计,联合微软,三星,ARM,英国兰卡斯特大学等共同完成开发的一款微型电脑用途主要针对中小学生编程教育。可以用 MicroPython 来编程
  • Microbit 喵比特是小喵科技基于微软的游戏平台做的一款编程游戏机,上面集合了很多传感器资源,主要用于中小学生图形化编程教育。
  • uFlash 是一个专为 BBC micro:bit 开发板量身定制的命令行工具,通过简单的命令行操作完成烧录、辅助开发。

实验设计

LED显示屏

BBC micro:bit上的LED显示屏包含了25颗红色的LED灯。这些灯呈5×5的网格状排列(横排5颗LED灯,竖排5颗LED灯) 

熟悉坐标

坐标

方法使用

microbit.display.set_pixel(x, y, value)
microbit.display.show(image)
microbit.display.show(value, delay=400, *, wait=True, loop=False, clear=False)
microbit.display.scroll(value, delay=150, *, wait=True, loop=False, monospace=False)
microbit.display.clear()

显示Hello

掌握 from import display loop

from microbit import *
display.scroll("Hello")
display.show(520)
display.show('LOVE',loop=True)

循环显示

掌握 while sleep

from microbit import *
while True:
  display.show(Image.HEART_SMALL)
  sleep(300)
  display.show(Image.HEART)
  sleep(300)

计算器

x = None
var = None
y = None
 
import math
import random
from microbit import *
def qiuhe(x,y):
  return x+y
while True:
  if button_a.is_pressed():
    x = random.randint(1,9)
    display.scroll(x)
    y = random.randint(1,9)
    display.scroll(y)
  if button_b.is_pressed():
    var = qiuhe(x,y)
    display.scroll(var)

满天星(随机产生)

掌握 random:randint setpixel clear</wrap> <code bash> from microbit import * import random while True: display.setpixel(random.randint(0,4),random.randint(0,4),9) sleep(10) display.clear() </code> ==== 跑马灯效果一 ==== <code bash> from microbit import * def light(num, level=9): if num < 5: x = 0 direct = 1 else: x = 4 direct = -1 while x ⇐ num: display.set_pixel(x, num - x, level) x += direct if x == (num - 5): break while True: for i in range(9): light(i, 9) sleep(50) for i in range(9): light(i, 0) sleep(50) </code>

要用sudo来打开

 sudo open /Applications/Mu\ Editor.app/Contents/MacOS/Mu\ Editor   

===== 按钮 ===== 在micro:bit板子前面有2个按钮(标记了A和B)。按下这些按钮,则可以运行代码。你可以检测这些按钮被按下的时间。 按键 ==== 满天星(加减速控制) ==== 掌握 button sleep_time <code bash> sleeptime = 1000 from microbit import * import random while True: if buttona.waspressed(): sleeptime -= 200 if buttonb.waspressed(): sleeptime -= -200 display.setpixel(random.randint(0,4),random.randint(0,4),9) sleep(sleep_time) display.clear() </code> ==== 哭笑不得 ==== 掌握 if elif else <code bash> from microbit import * while True: if buttona.ispressed(): display.show(Image.HAPPY) elif buttonb.ispressed(): display.clear() else: display.show(Image.SAD) </code> ==== 播放音乐 ==== <code bash> import music from microbit import * while True: if buttona.waspressed(): music.play(music.ENTERTAINER) elif buttonb.waspressed(): music.play(music.CHASE) elif pin1.istouched(): music.play(music.BIRTHDAY) elif pin2.istouched(): music.play(music.WEDDING) else: pass </code> ===== 引脚 ===== 在micro:bit连接器的边缘有25个外部接口,我们把这些接口称作“引脚”。 它可以用来为电机,LED灯,或者其他带引脚的电子元器件编程,或者是连接外部传感器控制代码。 引脚 ==== 怕痒的机器人 ==== 掌握 pinX is_touched <code bash> from microbit import * while True: if pin0.is_touched(): display.show(Image.HAPPY) else: display.show(Image.SAD) </code> 原生网页版本的python <code bash> def onforever(): if input.pinispressed(TouchPin.P0): basic.showicon(IconNames.HAPPY) else: basic.showicon(IconNames.SAD) basic.forever(onforever) </code> ===== 基于radio的剪刀石头布 ===== * How to Use Radio Send and Receive Message Tutorial <code bash> number = 0 paper = “” paper = “hello” radio.set_group(111) def onbuttonpresseda(): radio.sendstring(paper) input.onbuttonpressed(Button.A, onbuttonpressed_a) def onbuttonpressedb(): radio.sendstring(paper) input.onbuttonpressed(Button.B, onbuttonpressed_b) def ongesturethreeg(): radio.sendstring(paper) input.ongesture(Gesture.THREEG, ongesturethree_g) def onreceivedstring(receivedString): global number number = randint(1, 3) if receivedString == paper: basic.showleds(“”“ . . . . . . . . . . . . . . . . . . . . . . . . . ”“”) basic.pause(100) if number == 1: basic.showleds(“”“ # . . . # # . . . # # . . . # . # # # . . . # . . ”“”) if number == 2: basic.showleds(“”“ . . . . . . # # # . . # # # . . # # # . . . . . . ”“”) if number == 3: basic.showleds(“”“ # # # # # # # # # # # # # # # # # # # # # # # # # ”“”) radio.onreceivedstring(onreceivedstring) </code> 源代码 ===== 光线传感器 ===== 通过反转LED屏幕,micro:bit进入输入模式。LED屏幕起到一个基础的光线传感器的作用,你可以用它来检测周围的光线。 光线传感器 ==== 随光舞动 ==== <code bash> from microbit import * while True: a = display.readlightlevel() % 10 # 0 ~ 9 for i in range(0,5): for j in range(0,5): display.set_pixel(i,j,a) </code> <code javascript> basic.forever(function () { led.setBrightness(255 - input.lightLevel()) basic.showIcon(IconNames.Heart) }) </code> ===== 温度传感器 ===== 温度计 <code python> from microbit import * while True: display.show(temperature()) </code> ===== 加速度传感器 ===== 加速度传感器可以测量micro:bit的加速度;也可以检测micro:bit的移动。例如:摇动,倾斜以及自由落体。 加速计 <code python> from microbit import * while True: reading = accelerometer.getx() reading2 = accelerometer.gety()
if reading > 20: display.show(Image.ARROWE) elif reading < -20: display.show(Image.ARROWW) elif reading2 > 20: display.show(Image.ARROWS) elif reading2 < -20: display.show(Image.ARROWN) else: display.show(“-”) </code> ===== 指南针 ===== 指南针是用于检测地球磁场,可以让你探测到micro:bit面对的方向。在使用之前,你需要校准指南针。 指南针 <code python> from microbit import * # Start calibrating compass.calibrate() # Try to keep the needle pointed in (roughly) the correct direction while True: sleep(100) needle = 1) 30) % 12 display.show(Image.ALL_CLOCKS[needle]) </code> ==== 磁力检测器 ==== <code python> from microbit import * import music, random, neopixel Red = (255, 0, 0) Orange = (255, 165, 0) Yellow = (255, 255, 0) Green = (0, 255, 0) Blue = (0, 0, 255) Dark = (148, 0, 211) White = (255, 255, 255) colors = (Red, Orange, Yellow, Green, Blue, Dark, White) np = neopixel.NeoPixel(pin12, 4) # 4表示有RGB数量 display.clear() item = 80 compass.calibrate() # 初始化磁力计 while True: np.clear() for i in range(4): np[i] = colors[random.randrange(0, len(colors))]
if buttona.waspressed(): item += 10 display.show(item) if buttonb.waspressed(): item -= 10 display.show(item) xmagnetic = compass.getfield_strength()/1000 music.play('C3:3') if xmagnetic >= item: for i in range(4): np[i] = (255, 0, 0) np.show() display.show(Image.HEART) sleep(2000) display.show(xmagnetic) else: np.show() display.show(Image.DIAMOND_SMALL) sleep(1000) display.show(Image.DIAMOND) </code> ===== 人体红外传感器 ===== HC-SR501是一款基于热释电效应的人体热释运动传感器,能检测到人体或者动物上发出的红外线。
这个传感器模块可以通过两个旋钮调节检测 3 ~ 7 米的范围,5秒至5分钟的延迟时间。 <code python> int Sensor_pin = 12; int ledpin = 13; void setup() { pinMode(Sensor_pin, INPUT);
设置人体红外接口为输入状态 pinMode(ledpin, OUTPUT);
Serial.begin(9600); Serial.flush();
Serial.println(“Ready”); } void loop() { int val = digitalRead(Sensor_pin); 定义参数存储人体红外传感器读到的状态 if (val == 1) 如果检测到有动物运动(在检测范围内),LED灯亮 { Serial.println(“There is sb moving”); digitalWrite(ledpin, HIGH); } else { Serial.println(“There isn't sb moving”); digitalWrite(ledpin, LOW); } delay(100); 延时100毫秒 } </code> ====== micro:bit全球挑战赛作品 ====== * micro:bit全球挑战赛,作品提交倒计时5天 * micro:bit全球青少年创意征集 * 暖得都变异了!大堡礁绿海龟绝大多数是雌性,种群面临灭绝 ===== 完整的解决方案包含内容 ===== 1、方案介绍:基本介绍和用途以及现实意义(作品创建的原因); 我做的电子小制作是一个根据检测外界温度,用风扇实现自动降温和用红外灯自动加温的装置,用于控制温度在一定的范围。 2、项目介绍:灵感来源以及它将如何解决相关问题; (方案介绍及项目介绍不超过500字) 据研究人员在《当代生物学》文章称,由于气候变化,大堡礁地区的绝大多数绿海龟现在都是雌性。
由于海龟常常将蛋埋在沙滩上,小海龟的性别由孵化时的周围环境温度决定。研究人员指出,气候变化影响着全球的物种和生态系统,这引起了科学家对其生存的担忧。
科学实验表明,如果将龟蛋放置在20~27℃下孵化,出生的小龟往往是雄性;如果放置在30~35℃下,出生的小龟以雌性为多。
我做的这个电子装置,可以通过温度传感器去感知外界的温度,偏高了就用风扇降温,偏低了就用红外灯加温,同时触发报警器, 把温度控制在合理的范围内,保证雌雄比例的正常。 此装置适用于海岛、自然保护区等等。
3、制作过程:项目运行原理、项目涉及的器材、制作过程记录; 用到的传感器:
micro:bit,扩展板,温度传感器,红外LED灯,电动风扇,蜂鸣报警器,导线若干
4、源代码(.hex文件)、项目演示视频或照片需另行打包上传至附件。 ====== Matrix:bit物美价廉的好玩板子 ====== 重点:售价才68 RMB,那是真的香 ===== 设计示意图 ===== 正面 背面 ===== 主流可编程板 ===== 主流可编程板子的对比图 ===== 一个结合声音,屏幕,光线,动画的例子 ===== Matrix实例

1)
15 - compass.heading(