7. Picoboard-Scratch
The mPython Board can simulate the PicoBoard sensor board in Scratch. To use the mPython Board to simulate the PicoBoard for programming in Scratch, you first need to flash the program that establishes the connection between the mPython Board and Scratch into the mPython Board.
7.1. Flashing the Program
# mPython Board simulating Scratch PicoBoard
#------------------------------------------------------
# Channel | PicoBoard | mPython |
#------------------------------------------------------
# 4 | resistance-A | press of the "A" button |
# 2 | resistance-B | accelerometer's x |
# 1 | resistance-C | accelerometer's y |
# 0 | resistance-D | ext |
# 3 | button | press of the "B" button |
# 5 | light | light |
# 6 | Sound | Sound |
# 7 | Slider | TouchPad |
# -----------------------------------------------------
# Operation instructions: Normal startup enters scratch mode by default; to return to repl mode, after pressing reset and button b at the same time,
# release the reset button after 2 seconds first, then release button b
from mpython import *
from machine import UART
from machine import Pin,ADC
scratchMode=True
ext = MPythonPin(3,PinMode.ANALOG)
#48*48
scratchlogo= bytearray([
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X02,0X00,0X00,
0X00,0X80,0X00,0X06,0X00,0X00,0X00,0XE0,0X00,0X0E,0X00,0X00,0X00,0XF0,0X00,0X1B,
0X00,0X00,0X00,0X5C,0X00,0X33,0X00,0X00,0X00,0X46,0X7F,0XFD,0X00,0X00,0X00,0X5B,
0XC0,0X7D,0X80,0X00,0X00,0X5C,0XFF,0XFB,0X80,0X00,0X00,0X5F,0X7F,0XF7,0XC0,0X00,
0X00,0X5E,0X4F,0XE8,0X60,0X00,0X00,0X7D,0XF3,0XF8,0X30,0X00,0X00,0X6B,0X1D,0XF8,
0X18,0X00,0X00,0X76,0X05,0XF8,0X0C,0X00,0X00,0XD4,0X02,0XE8,0X0C,0X00,0X00,0XB4,
0X03,0XEC,0X0E,0X00,0X01,0XB4,0X01,0X74,0X6E,0X00,0X01,0X7E,0X11,0X76,0X0A,0X00,
0X01,0X7A,0X11,0X03,0X01,0X00,0X23,0X7B,0X01,0X7E,0X60,0X02,0X0F,0XFD,0X82,0X3F,
0X00,0X28,0X03,0X7F,0X6C,0X1E,0X00,0X30,0X03,0X7C,0X00,0X0C,0X00,0X10,0X03,0X78,
0X00,0X00,0X00,0X10,0X3B,0X78,0X05,0X00,0X00,0X10,0X01,0X78,0X00,0X30,0X00,0X3C,
0X01,0XB8,0X00,0X01,0X80,0X38,0X00,0XB8,0X00,0X00,0X20,0X38,0X00,0XDC,0X04,0X00,
0X00,0X70,0X00,0X64,0X02,0X00,0X80,0XE0,0X00,0X3A,0X01,0X01,0X01,0XE0,0X00,0X1D,
0X00,0X38,0X07,0XC0,0X00,0X0F,0X80,0X00,0X1F,0X00,0X00,0X03,0XF0,0X00,0XFE,0X00,
0X00,0X01,0XFF,0XFF,0XF8,0X00,0X00,0X00,0X3F,0XFF,0XC0,0X00,0X00,0X00,0X00,0XE0,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
])
if button_b.value()==0: # Startup detection, press button b to enter repl
scratchMode=False
#print('replMode')
oled.DispChar('replMode',30,20)
oled.show()
# Touch button scanning
# When the 6 touch buttons are pressed, scratch feeds back 10, 20, 30, 40, 50, 60 in sequence
def ScanTouchpad():
if touchPad_P.read() <200:
return int(1023/10)
elif touchPad_Y.read()<200:
return int(1023/10*2)
elif touchPad_T.read()<200:
return int(1023/10*3)
elif touchPad_H.read()<200:
return int(1023/10*4)
elif touchPad_O.read()<200:
return int(1023/10*5)
elif touchPad_N.read()<200:
return int(1023/10*6)
else:
return 0
while scratchMode:
oled.bitmap(40,10,scratchlogo,48,48,1)
oled.show()
uart = UART(1, 38400, rx=3, tx=1)
# Create and send Scratch data packet
def convert(a, b):
sensor = bytearray(2)
upper = (b & 0x380) >> 7
sensor[1] = b & 0x7f
sensor[0] = (1 << 7) | a << 3 | upper
uart.write(sensor)
request = bytearray(1)
while True:
if uart.readinto(request) == 1 and request[0] == 0x01: # When the 0x01 byte sent by scratch is received
rgb.fill((0,20,0))
rgb.write()
convert(15, 0x04)
sleep_us(10)
extValue=int(ext.read_analog()/4) # Get ext
convert(0,extValue)
reading = accelerometer.get_y()*1000 # Get accelerometer's y
if reading >= 0:
reading = int(reading / 2) + 512
convert(1, reading)
else:
reading = 512 - abs(int(reading / 2))
convert(1, reading)
reading = accelerometer.get_x()*1000 # Get accelerometer's x
if reading >= 0:
reading = int(reading / 2) + 512
convert(2, reading)
else:
reading = 512 - abs(int(reading / 2))
convert(2, reading)
if button_b.value()==0: # Get button B state
convert(3, 0)
else:
convert(3, 1023)
if button_a.value()==0: # Get button A state
convert(4, 1023)
else:
convert(4, 0)
convert(5, 1023-light.read()) # Get light senser
convert(6, sound.read()) # Get Sound senser
convert(7, ScanTouchpad()) # Get TouchPad value
else:
rgb.fill((0,0,0))
rgb.write()
This program establishes a connection between the mPython Board and Scratch. After flashing this program, the mPython Board establishes a connection with Scratch PicoBoard, so that the mPython Board can simulate the sensors on the PicoBoard. The sensors on the PicoBoard correspond one-to-one with those on the mPython Board, as shown in the table below:
PicoBoard |
mPython Board |
|---|---|
Resistance A |
Button A |
Resistance B |
Button X |
Resistance C |
Button Y |
Resistance D |
ext(P3) |
Button |
Button B |
Light |
Light |
Sound |
Sound |
Slider |
Touch buttons |
Tip
When the mPython Board simulates the PicoBoard for use in Scratch, note that the sensor values of the mPython Board change. For example, in mPython, the light sensor detection value ranges from 0~4095, while in Scratch, its value range is 0~100. The method to view the mPython Board sensor parameters in Scratch is as follows: Under “More Blocks” in the script panel, click the corresponding block module. Take button A as an example. Under the two conditions of pressed and not pressed, click the “Resistance A sensor value” block. When not pressed, the value is 0; when pressed, the value is 100. The viewing method for other sensors is the same.
7.2. Connecting the mPython Board to Scratch
Flash the above program into the mPython Board and set it as the default run;
Open the Scratch software (version 2.0 is used here), click “Add Extension” in “More Blocks” of the script panel, and add the “PicoBroad” hardware;
After adding “PicoBroad”, the “PicoBoard” command module appears on the Scratch interface. When the yellow dot to the right of “PicoBoard” turns green, and the three RGB lights on the mPython Board flash at the same time, this indicates that the mPython Board is successfully connected to Scratch;
After a successful connection, you can make some interactive programs in Scratch.
7.3. Editing Graphical Programs with Scratch Software
Example download Scratch example .
7.4. Exiting Scratch and Returning to repl Mode
The mPython Board cannot read files or flash programs when in scratch mode. If you want to return to mPython2 to read files or flash programs, you must make the mPython Board exit scratch mode and enter repl mode. The method is as follows: * After pressing and holding the reset key and button b at the same time, release the reset button after 2 seconds first. When the OLED display shows “replMode”, release button b. At this point, you have successfully returned to repl mode, as shown in the figure: