MakeCode JoyCar Demo-Programs
Here you can find complete demo programs, for starting your Joy-Car.
With these two example programs you can remote control your JoyCar with the help of a second micro:bit. For this purpose the accelerometer and the BT radio connection is used.
The program RC-JoyCar has to be uploaded to the micro:bit inside the JoyCar, the program RC-Remote to the micro:bit you want to use as remote control.
The RC-Remote program uses the accelerometer to detect how the micro:bit is held. If the remote control is tilted to one side, the JoyCar should move in the corresponding direction. The X- and Y-axis values of the accelerometer range from -1000 to +1000, where 0 is the center point when the micro:bit is held straight. To prevent the JoyCar from starting at the slightest movement of the remote control, we have added the threshold value of -300 and +300 respectively. This value must be reached when tilting the remote control for the JoyCar to start moving. Also you can activate the horn with button A and the headlights with button B. The data is passed in the form of a string with two characters. The first character indicates the driving direction and the second character indicates the status of the buttons.
The RC-JoyCar program receives the transmitted data of the remote control via the BT radio link and evaluates them. The received string is split into the two contained characters and evaluated.
The first character in the string indicates the driving direction, the motors of the JoyCar are controlled by this data.
The second character indicates the status of the buttons, with this data the horn and the headlights are controlled.
The Linefinder sensors send out an infrared signal which is reflected when it hits a light background and thus returns a high signal (1). If the infrared signal hits a dark background, such as a painted black line, the signal is not reflected and the sensor returns a low signal (0).
This allows the JoyCar with its three linefinder sensors on the bottom side to follow a painted black line on its own. We have prepared a small demo program for this purpose.
With the button A on your micro:bit you can switch the line-tracking mode on and off.
For a better overview of which action has to be performed at which sensor values, we have created a truth table here.
|Linefinder left||Linefinder middle||Linefinder right||Action|
At the front of the JoyCar, two infrared obstacle sensors are installed. These sensors emit an infrared signal to the front, which, if it hits an obstacle, is reflected by it and recognized by the infrared receiver built into the sensor. Thereby the sensor sends a high signal (1) back to the micro:bit.
If there is no obstacle in front of the sensor, the IR signal is not reflected and the sensor returns a low signal (0). This allows the JoyCar to detect obstacles and to avoid them automatically.
In this example program exactly this function is used.
If one of the obstacle sensors detects an obstacle, the motors of the JoyCar will be stopped, the JoyCar will reverse a little bit in a small curve and can then pass the obstacle in forward gear.
The obstacle detection can be switched on and off in this program, with button A.
Ultrasonic obstacle detection
The ultrasonic obstacle detection has 3 modes. Mode 0 (which is also activated at startup) is the standby mode. This can be recognized by the fact that the warning lights are on and an x is displayed on the LED matrix of the micro:bit. The standby mode can be activated by pressing the buttons A and B at the same time. If button A is pressed, mode 1 is activated and an arrow in the direction of the rear of the joy car appears on the LED matrix. If an object comes closer than 20 cm from the front of the Joy-Car, the Joy-Car moves back. Pressing button B activates mode 2 and an arrow appears on the LED matrix in the direction of travel. Now the Joy-Car drives forward until an obstacle comes within 40 cm of the Joy-Car.