Picoboard-Scratch ============================== The mPython Board can be used to emulate the PicoBoard sensor board in Scratch. To use the mPython Board to emulate the PicoBoard for programming in Scratch, you first need to flash into the mPython Board the program that establishes the connection between the mPython Board and Scratch. Flashing the Program +++++++++++++++++++++ :: # mPython Board emulates 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: a normal startup enters scratch mode by default; to return to repl mode, # press and hold both reset and button b, release the reset button first, wait 2 seconds, 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 scan # When the 6 touch buttons are pressed, scratch feeds back 10, 20, 30, 40, 50, 60 in turn 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 the connection between the mPython Board and Scratch. After flashing this program, the mPython Board is connected to the Scratch PicoBoard, so the mPython Board can be used to emulate 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 button ============== ================ .. admonition:: Tip When the mPython Board emulates the PicoBoard for use in Scratch, note that the sensor values on the mPython Board change. For example, in mPython, the light sensor detection value ranges from 0 to 4095, while in Scratch the value ranges from 0 to 100. To view the mPython Board sensor parameters in Scratch: under "More Blocks" in the script pane, click the corresponding block module. Taking button A as an example, under both pressed and unpressed conditions, click the "value of resistance-A sensor" block; the unpressed value is 0 and the pressed value is 100. The viewing method for other sensors is the same. Connecting the mPython Board to Scratch ++++++++++++++++++++++++++++++++++++++++++++++ * 1. Flash the above program into the mPython Board and set it as the default run; * 2. Open the Scratch software (version 2.0 is used here), click "Add an Extension" under "More Blocks" in the script pane, and add the "PicoBroad" hardware; * 3. After adding "PicoBroad", the "PicoBoard" instruction module will appear 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 has successfully connected to Scratch; * 4. After a successful connection, you can make some interactive programs in Scratch. .. image:: /_static/products/mpython-s3/_static/image/classic/scratch.jpg :scale: 50% :align: center Editing Graphical Programs in Scratch ++++++++++++++++++++++++++++++++++++++++++++++ Download the example: Scratch example. .. image:: /_static/products/mpython-s3/_static/image/classic/scratch.gif :scale: 50% :align: center Exiting Scratch and Returning to repl Mode +++++++++++++++++++++++++++++++++++++++++++++++ When the mPython Board is in scratch mode, it cannot read files or flash programs. 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: * Press and hold both the reset key and button b, then release the reset key first and wait 2 seconds; when the OLED screen displays "replMode", release button b. At this point you have successfully returned to repl mode, as shown in the figure: .. image:: /_static/products/mpython-s3/_static/image/classic/replmode.jpg :scale: 35% :align: center