Drive it from the phone — Smart Car
Four Bluetooth blocks, in one program. The robot renames itself, shows with an LED whether your phone is connected, drives when you press the D-pad, and prints where the joystick is. You try it with the Game Pad in the MoBot app.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Change MoBot’s name:
"MoBot-Test" - Print to serial
"I am now called MoBot-Test. Look for me in the MoBot app." - wait 1 seconds
- repeat forever
- if Is Bluetooth connected to the app ?
- Set LED 1 ON
- else
- Set LED 1 OFF
- Print to serial (create text with
"joystick X = "Read from joystick the value of X) - Print to serial (create text with
"joystick Y = "Read from joystick the value of Y) - if Is button Up pressed ?
- set Motor 1 to Direction 2 with speed 45 %
- set Motor 2 to Direction 2 with speed 45 %
- else
- set Motor 1 to Direction 2 with speed 0 %
- set Motor 2 to Direction 2 with speed 0 %
- if Is button Down pressed ?
- set Motor 1 to Direction 1 with speed 45 %
- set Motor 2 to Direction 1 with speed 45 %
- if Is button A pressed ?
- Set LED 2 ON
- if Is button B pressed ?
- Set LED 2 OFF
- wait 0.15 seconds
- if Is Bluetooth connected to the app ?
Read this first — the robot must be in Programmable
Section titled “Read this first — the robot must be in Programmable”Your program only runs while the robot’s mode is Programmable. The Game Pad does not change the mode: opening it never switches the robot to anything else. Only a mode tile in the app does that. So the order is:
- Set the robot to Programmable.
- Then open the Game Pad. Your program receives the stick and the buttons.
Do not tap the Manual driving tile. It sets the robot to Manual driving, and your program stops. To get it back, tap the Programmable tile: your program starts again from its first line.
An old version of the MoBot app switched the robot to Manual driving as soon as you opened the Game Pad, and that stopped your program. If your app has no Programmable tile, update the app first.
What you need
Section titled “What you need”- A Smart Car, running on its battery. Plug in the USB cable as well only if you want to read the serial monitor while you play.
- A phone or tablet with the MoBot app.
- A book to stand the robot on — the wheels run when you press the up arrow.
How to run it
Section titled “How to run it”- Open 10-bluetooth-gamepad.mobot, plug in, press Send to Device. If the robot is in another mode, MoBot asks: “The robot is in Manual driving. Switch it to Programmable to run your program?” Answer Switch to Programmable.
- Stand the robot on the book and switch it on with its battery.
- Open the MoBot app and scan. The robot should now be listed as
MoBot-Test, not by its old name. That is the first test, and it happens before you connect to anything. - Connect. On the home screen, under Activities, the app says Robot mode and the mode’s name. If it does not say Robot mode: Programmable, tap the Programmable tile. The app sets the robot to Programmable and opens the Programmable screen. Press the back arrow to return to the home screen.
- Tap Open Game Pad.
What you should see
Section titled “What you should see”In the card in the middle of the Game Pad, the app says Your program reads this pad while your program is running. (A robot with the oldest firmware, 2.0.0, shows no line there.) If it says No program on the robot yet, send the program again. If it says Your program stopped because of an error, the serial monitor in MoBot shows the error.
| what you do | what should happen |
|---|---|
| scan | the robot appears as MoBot-Test |
| connect | LED 1 comes on and stays on |
| tap the Switch Robot button at the top of the home screen, or walk away | LED 1 goes out within a moment |
| hold the up arrow of the D-pad | both wheels drive forward; the serial monitor says joystick Y = 127; let go and the wheels stop |
| hold the down arrow | both wheels drive backwards; joystick Y = -128 |
| hold the left or right arrow | no wheel moves; joystick X = -128 (left) or joystick X = 127 (right) |
| tap A | LED 2 lights and stays on |
| tap B | LED 2 goes out |
| switch the pad to Joystick and move the stick | joystick X and joystick Y follow the stick, from about −128 to 127, and the wheels stay still: this program drives from the D-pad arrows, and the Joystick setting does not send them |
When you hold an arrow, the robot reads it as the stick pushed all the way that way. That is why the arrows show 127 and −128.
The test passes if the name changed in the scan list, if LED 1 tracks the connection, and if every button does its own thing.
Put the robot back before the next example
Section titled “Put the robot back before the next example”If the robot was put in another mode while it was on the desk — a mode tile tapped in the app — no example in this folder will run until you hand it back. Your program only runs while the robot’s mode is Programmable. The Game Pad itself never changes the mode.
Press Send to Device. If the robot is in another mode, MoBot asks: “The robot is in Manual driving. Switch it to Programmable to run your program?” Answer Switch to Programmable. Or open the MoBot app, connect to the robot and tap the Programmable tile: the home screen then says Robot mode: Programmable. If the app has no Programmable tile, update the app.
If the name did not change
Section titled “If the name did not change”The new name is stored on the robot and used the next time Bluetooth starts. Switch the robot off and on again, then scan once more. If the app still shows the old name, that is worth reporting.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_blename |
renames the robot. Run it once, at the top — not inside the loop. |
mobot_bleconnected |
true while a phone is connected. |
mobot_blebuttonpressed |
true while one of A B X Y up down left right is held. |
mobot_blejoystick |
the joystick’s X or Y, about −128 to 127. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Drive it from the phone — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 10-bluetooth-gamepad.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 10-bluetooth-gamepad.main.py.
from machine import Pinimport time
robot.set_name('MoBot-Test')print("[MOBOT]", 'I am now called MoBot-Test. Look for me in the MoBot app.')time.sleep(1)while True: if robot.bluetooth.is_connected(): robot.led1.set(1) else: robot.led1.set(0) print("[MOBOT]", ('joystick X = ' + str(robot.gamepad.x))) print("[MOBOT]", ('joystick Y = ' + str(robot.gamepad.y))) if robot.gamepad.is_pressed("up"): robot.left_motor.forward(45) robot.right_motor.forward(45) else: robot.left_motor.forward(0) robot.right_motor.forward(0) if robot.gamepad.is_pressed("down"): robot.left_motor.backward(45) robot.right_motor.backward(45) if robot.gamepad.is_pressed("a"): robot.led2.set(1) if robot.gamepad.is_pressed("b"): robot.led2.set(0) time.sleep(0.15)# Drive it from the phone — robotics## This is what MoBot puts on the board as main.py when you open# 10-bluetooth-gamepad.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
mobotROBOTICS.ble.changeName('MoBot-Test')print("[MOBOT]", 'I am now called MoBot-Test. Look for me in the MoBot app.')time.sleep(1)while True: if mobotROBOTICS.ble.is_connected: mobotROBOTICS.led1.setState(1) else: mobotROBOTICS.led1.setState(0) print("[MOBOT]", ('joystick X = ' + str(mobotROBOTICS.ble.x))) print("[MOBOT]", ('joystick Y = ' + str(mobotROBOTICS.ble.y))) if mobotROBOTICS.ble.isPressed['up']: mobotROBOTICS.left_motor.setDirectionAndSpeed(1, 45) mobotROBOTICS.right_motor.setDirectionAndSpeed(1, 45) else: mobotROBOTICS.left_motor.setDirectionAndSpeed(1, 0) mobotROBOTICS.right_motor.setDirectionAndSpeed(1, 0) if mobotROBOTICS.ble.isPressed['down']: mobotROBOTICS.left_motor.setDirectionAndSpeed(0, 45) mobotROBOTICS.right_motor.setDirectionAndSpeed(0, 45) if mobotROBOTICS.ble.isPressed['a']: mobotROBOTICS.led2.setState(1) if mobotROBOTICS.ble.isPressed['b']: mobotROBOTICS.led2.setState(0) time.sleep(0.15)Try changing it
Section titled “Try changing it”- Use left and right on the D-pad to spin the robot, by driving the two wheels opposite ways.
- Make X play a sound and Y stop it.
- Print
Is Bluetooth connecteditself into the serial monitor and watchTrueandFalsescroll past as you walk out of range.