Difference between revisions of "Getting Started with Triode-Car"
(→采集巡线检测电路的电压模拟信号) |
(→Control Circuit) |
||
(25 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[zh:快速上手_香蕉派_Triode-Car]] | [[zh:快速上手_香蕉派_Triode-Car]] | ||
− | [[File:Triode-Car_With_Micro_Bit.png|thumb|BPI Triode-Car | + | [[File:Triode-Car_With_Micro_Bit.png|thumb|Apply BPI Triode-Car and micro:bit together]] |
− | [[File:Triode-Car_With_BPI_Bit.png|thumb|BPI Triode-Car | + | [[File:Triode-Car_With_BPI_Bit.png|thumb|Apply BPI Triode-Car and[[BPI-Bit STEAM 教育开发板 ]] together]] |
− | [[File:Triode-Car_With_BPI_AI.png|thumb|Triode-Car | + | [[File:Triode-Car_With_BPI_AI.png|thumb|Apply Triode-Car and BPI:AI AI together]] |
− | = | + | =Product Introduction= |
− | [[ | + | [[BPI_Triode-Car_kit]] |
=Control With LM393 Chip= | =Control With LM393 Chip= | ||
Line 15: | Line 15: | ||
There is a button to switch between two modes on the Triode-Car. One of which is using the built-in LM393 comparator chip, and the other is with the Micro: bit or BPI: bit. | There is a button to switch between two modes on the Triode-Car. One of which is using the built-in LM393 comparator chip, and the other is with the Micro: bit or BPI: bit. | ||
− | When using the built-in LM393 chip, it has only one function, to track dark lines, it consists of the Line | + | When using the built-in LM393 chip, it has only one function, to track dark lines, it consists of the Line Detecting Circuit, Voltage Comparing Circuit, and the Motor Circuit. |
− | ===Line | + | ===Line Detecting Circuit=== |
The Triode-Car uses two pairs of photoresistor and LED on the bottom side to enable its line tracking feature. When the LED light reflecs on bright and dark surfaces, the resistance of the photoresistor will change drastically. | The Triode-Car uses two pairs of photoresistor and LED on the bottom side to enable its line tracking feature. When the LED light reflecs on bright and dark surfaces, the resistance of the photoresistor will change drastically. | ||
Line 39: | Line 39: | ||
As the picture shown above, when the car shifts towards the left, the right photoresistor will be directly on top of the line, therefore, its resistance will increase. Conversely, the left resistor, will shift away from the line, its resistance will decrease. At this point, the voltage from LL will be lower than LR. The U1A comparator's positive voltage will be higher than reverse voltage, triggering pin 1 to pull high. Conversely, U1B's positive voltage will be lower than reverse voltage, pin 7 will pull low, making the left motor move. The car shifts right, R6 and D2 moves away from the dark line until the left sensors detect the dark line. The concept is similar as before, the car repeats the actions above to progress forward. | As the picture shown above, when the car shifts towards the left, the right photoresistor will be directly on top of the line, therefore, its resistance will increase. Conversely, the left resistor, will shift away from the line, its resistance will decrease. At this point, the voltage from LL will be lower than LR. The U1A comparator's positive voltage will be higher than reverse voltage, triggering pin 1 to pull high. Conversely, U1B's positive voltage will be lower than reverse voltage, pin 7 will pull low, making the left motor move. The car shifts right, R6 and D2 moves away from the dark line until the left sensors detect the dark line. The concept is similar as before, the car repeats the actions above to progress forward. | ||
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | | style="background: PaleTurquoise; color: black;text-align: center" colspan="14"| '''Line-Detecting Circuit Status Table (LM393 and related pins status)''' | ||
+ | |- | ||
+ | | style="background: #ffffcc; color:#e76700 " colspan="14"|''Note: Conditions are black lines on a white background.'' | ||
+ | |- | ||
+ | !Car lean Direction!!Left LDR(R5)!!Right LDR(R6)!!LL Potential!!LR Potential!!P3 Input!!P2 Input!!P1 Output!!P5 Input!!P6 Input!!P7 Output!!M1 Left Motor!!M2 Right Motor!!Car Move Direction | ||
+ | |-style="text-align: center" | ||
+ | | Left||Bright||Dark||0||1||1||0||1||0||1||0||1||0||Right | ||
+ | |-style="text-align: center" | ||
+ | | Right||Dark||Bright||1||0||0||1||0||1||0||1||0||1||Left | ||
+ | |} | ||
+ | |||
+ | ==Calibrating Line-Detecting Sensitivity== | ||
+ | |||
+ | To calibrate the Line-Detecting sensitivity under LM393 control, one would need to adjust the potentiometers physically. This process could help the Triode-Car to detect dark lines more. | ||
+ | |||
+ | Calibration process are as follows: | ||
+ | |||
+ | # Point a stable light source towards the photoresistors. | ||
+ | # Adjust either side of potentiometers until both motors are operating simultaneously. | ||
+ | # After adjustments, put the car on the track to test the results. | ||
+ | # If the car still runs off course, place the car on a white piece of paper, use a small object to lift up the back wheels slightly to prevent contact, repeat the steps 2~3 to adjust the potentiometers. | ||
+ | |||
+ | [[File:Triodecar_calibration_1.jpg|500px]] | ||
+ | |||
+ | [[File:Triodecar_calibration_2.jpg|500px]] | ||
=Use MakeCode to Control the Triode-Car= | =Use MakeCode to Control the Triode-Car= | ||
Line 57: | Line 85: | ||
# Press B to turn Left. | # Press B to turn Left. | ||
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_1.hex '''Example link on Github'''] | |
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | ||
Line 67: | Line 95: | ||
In the Triode-Car extensions, there are blocks dedicated to adjust the speed up to 10 levels. | In the Triode-Car extensions, there are blocks dedicated to adjust the speed up to 10 levels. | ||
− | Example Blocks: | + | '''Example Blocks:''' |
[[File:Triode-Car_motor_control_2.png|640px]] | [[File:Triode-Car_motor_control_2.png|640px]] | ||
Line 77: | Line 105: | ||
The code within "forever" block will loop forever after the code in "on start" finishes. In between each loop, it can perform other actions such as, on button A/B pressed. | The code within "forever" block will loop forever after the code in "on start" finishes. In between each loop, it can perform other actions such as, on button A/B pressed. | ||
− | + | Added an "if" statement block to make sure the "speed" variable does not go higher than 10, if it goes higher, it will be set to 0 to prevent error messages. | |
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_motor_control_2.hex '''Example link on Github'''] | |
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | ||
Line 85: | Line 113: | ||
---- | ---- | ||
− | === | + | ===Reading Analog Signals From Photoresistors=== |
− | For Hardware Design, please refer to (Line | + | For Hardware Design, please refer to (Line Detecting Circuit). |
The "Read (index) line tracking sensor" block in Triode-Car extensions can collect analog values from the photoresistors. | The "Read (index) line tracking sensor" block in Triode-Car extensions can collect analog values from the photoresistors. | ||
Line 93: | Line 121: | ||
Micro: bit pins' voltage can be set from 0~3.3v, and has 1024(10bit) precision levels, therefore, the "read left/right line tracking sensor" block will input an analog value to adjust its voltage ranging from 0~1023. | Micro: bit pins' voltage can be set from 0~3.3v, and has 1024(10bit) precision levels, therefore, the "read left/right line tracking sensor" block will input an analog value to adjust its voltage ranging from 0~1023. | ||
− | Example Blocks: | + | '''Example Blocks:''' |
[[File:Triode-car_read_LDR.png]] | [[File:Triode-car_read_LDR.png]] | ||
Line 99: | Line 127: | ||
In the program, every 100ms the two photoresistors will send its analog values through USB port to the computer. By opening the control panel in Makecode, we can see the exact values. | In the program, every 100ms the two photoresistors will send its analog values through USB port to the computer. By opening the control panel in Makecode, we can see the exact values. | ||
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_read_LDR.hex '''Example link on Github'''] | |
+ | |||
+ | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===Use Two Micro: bit to Control Eachother's Motors Through Radio=== | ||
+ | |||
+ | Micro: bit supports radio connection, enable the radio extension on micro: bit, then it is possible for two micro: bit to communicate with eachother. | ||
+ | |||
+ | '''Example Blocks:''' | ||
+ | |||
+ | [[File:Triode-Car_radio_control_1.png|480px]] | ||
+ | |||
+ | By flashing the program onto the Micro: bit, and installing onto the Triode-Car, we can control the other car by pressing A/B buttons. | ||
+ | |||
+ | #Pressing button A allows it to turn left | ||
+ | #Pressing button B to turn right | ||
+ | #Pressing button A+B to go forward | ||
+ | #Releasing all buttons to stop. | ||
+ | |||
+ | The four variables added are not superfluous. Although you can also fill in the values we need in the building blocks, using variable names to replace these specified values helps us to establish a clearer cognitive logic. The more complex procedures can reflect its value. | ||
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-Car_radio_control_1.hex '''Example link on Github'''] | |
+ | |||
+ | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | ||
---- | ---- | ||
− | === | + | ===Bluetooth Control=== |
− | micro: | + | The bluetooth feature can be enabled through MakeCode extensions, we can control the micro: bit with our smartphones through certain applications. |
− | + | * iOS users: [https://apps.apple.com/cn/app/micro-bit/id1092687276 '''Micro:bit App on App Store'''] | |
− | + | * Android users:[https://github.com/microbit-foundation/microbit-blue/raw/master/releases/ble_demo_v1_5_4.apk '''Microbit-blue GitHub link''']or:[https://play.google.com/store/apps/details?id=com.kitronik.blemove ''' Kitronik Move on Google Play Store'''] | |
− | Kitronik Move Google | ||
− | + | Note: There is an official release of the Micro: bit App on both the iOS App Store and Android's Google Play Store, however, the iOS version has support for bluetooth connection, whereas the Google Play store's does not. This is why we require third party apps on Android devices that supports bluetooth connection. | |
− | + | '''Bluetooth pairing process and notes:''' | |
− | + | #In the MakeCode editor, open the extensions on the bottom of advanced tab, click on the bluetooth extension icon to apply.Bluetooth and Radio extensions cannot coexist, choosing bluetooth will automatically remove radio extension.[[File:Makecode_bluetooth_extension.jpg|none|200px]] | |
+ | #When pairing the micro: bit with android third-party app, one could turn on "Passkey pairing: Paring requires 6 digit key to pair." option in Project settings. | ||
+ | #To enter pairing mode, connect micro: bit to power, press reset button once while holding A+B button. | ||
+ | Do NOT let go of A+B buttons while the progress bar is running, it would interrupt the pairing process and one would need to start over. | ||
+ | #Android devices need to turn on GPS and bluetooth to have a stable connection. | ||
+ | #On Android devices, navigate to bluetooth settings, find and pair with the micro: bit. Once the pairing process starts, micro: bit will hint to press button A once, then it would scroll a 6 digit pairing code. Enter the code on the Andriod device to complete the pairing process. | ||
+ | #Press the reset button on the Micro: bit after pairing to restart. | ||
+ | #iOS devices does not require to enter bluetooth settings. To pair with the micro: bit, please follow the pairing instructions in the official micro: bit app. | ||
− | + | '''Example Blocks:''' | |
− | + | [[File:TriodeCar_bluetooth_control_1.png|640px]] | |
− | + | When paired successfully with bluetooth, display a red heart, whereas a disconnection would display an "X" symbol. Press the buttons inside the app to control the Triode-Car wirelessly. Press button A to go forward, C to turn left, and D to turn right, release the buttons to stop. | |
− | + | The "on event from()with value()" block can be found in the Control tab in Makecode. | |
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_bluetooth_control_1.hex '''Example link on Github'''] | |
− | + | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | |
− | + | ==Advanced Tutorial== | |
− | |||
− | + | ===Calibrating the Line-Detecting Circuit=== | |
− | + | As every component are not made exactly the same, despite being made according to the datasheet, they would vary from eachother ever so slightly. Calibration of the Line-Following Circuit improves the reaction time of the Triode-Car's line tracking feature. Adjust the resistance of the potentiometers connected to the photoresistors to increase the sensitivity. This process makes the Triode-Car less likely to run off course and maintains a stable analog voltage output. | |
− | + | We can directly use the "Reading Analog Signals From Photoresistors" guide from beginners tutorial to read the analog signals from the computer, then manually adjust the potentiometer with a screwdriver. | |
+ | |||
+ | The Micro: bit can detect analog voltage values from 0~1023, the closer the value is to the middle, the reaction from changes of brightness detected by the photoresistor will become more sensitive. | ||
+ | |||
+ | Therefore, under a stable brightness environment, try to calibrate the resistance as close to 512 as possible. This would create more stable energy output and reduce their analog value difference. | ||
+ | |||
+ | The above steps mentioned requires constant connection with triode-car connected to micro: bit and then to the PC. In a real use case scenerio, one would best be using manual adjustments to increase the sensitivity. | ||
+ | |||
+ | |||
+ | We cannot be sure to have the micro: bit connected to the PC all the time, therefore, we need to write a program to guide us how to properly calibrate the sensitivity. | ||
− | + | Calibration Steps: | |
− | # | + | # Adjust the left potentiometers first, to make its analog value out close to the median. |
− | # | + | # Adjust the right potentiometer on the premise that the previous condition is met, so that the parallel photoresistor output voltage analog quantity on the corresponding pin of Micro:Bit is close to the other photoresistor. |
− | |||
− | |||
− | |||
− | |||
− | + | We can achieve this by creating a simple "if" statement program. For those people who adjust manually, we could add different image patterns to indicate if we have adjusted properly. | |
− | + | '''Example Blocks:''' | |
− | - | + | [[File:Triode-car_LDR_calibration_1.png|640px]] |
− | + | The code is a bit complex, detailed explaination are as follows: | |
− | + | # The whole calibration code is organized into a self-defined function, this could be easily called or maintained later on and would cause less confusion as many other functions would be added. | |
+ | # The code consists within a while loop block. By adding loop conditions, the loop would exit after the potentiometers have finished calibration. | ||
+ | # Before entering the while loop, the micro:bit would display a directional arrow, indicating which potentiometers requires adjustment. | ||
+ | # The code within the first while loop block is to calibrate the left photoresistor, the "if" statement conditions are set to make the left photoresistor's analog voltage value in the range of minimum of 450 to 550 max. | ||
+ | # When the "if" statement contitions are met in the 4th point, the micro:bit would display a "checked" symbol to give a visual feedback as its been calibrated. At this moment one should stop adjusting the left potentiometer. After 1 second, the code would run the "if" statement again to check if the values are disrupted due to slight tremor of the hand. When the code double checked if conditions are met, the loop will end. | ||
+ | # The second while loop contains blocks to adjust the right photoresistor. The "if" statement within is to make the analog voltage values differce of both potentiometers less than 25. This is achieved by subtracting the values of both left and right photoresistors. | ||
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_LDR_calibration_2.hex '''Example link on Github'''] | |
− | + | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. | |
− | + | ---- | |
− | + | ===Line Tracking=== | |
− | + | After "Calibrating the Line-Detecting Circuit", we can start to make effective use of the Line-Detecting Circuit. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | According to the principles described in the "Line-Detecting Circuit" chapter,line trackingrequires real-time light intensity detection by using the different reflectivity of dark lines and its two sides. | |
− | + | Read the voltage analog values of the left and right LDRs in the program and compare the values to determine whether the Triode-Car's travel route is offset and the direction of the offset, and then the start and stop of the left and right motors are controlled and corrected. The driving direction of Triode-Car achieves the purpose of driving along dark lines. | |
− | + | '''Example Blocks:''' | |
− | + | [[File:Triode-car_Line_Follower.png|640px]] | |
− | + | The code is a bit complex, detailed explaination are as follows: | |
− | + | # The "on button A pressed" block is used to control the start and stop of the tracking program. Each time button A is pressed, the variables set in it will change its state. | |
+ | # The "forever" block will repeatedly execute its internal program, and each time the loop ends or the "show" or "pause" block is executed in the loop, the thread will allow other "forever" blocks or event-based loops, so this Three "forever" blocks and one "on button A pressed" block can run together in the background, so that the system has the ability to execute multiple programs at the same time. This is generally called "multitasking". | ||
+ | # The program in the first "forever" block is used to cyclically read the voltage analog values of the left and right LDRs, and then judge in the multi-level "if" conditional block, and change the variable when the corresponding conditions are met. The variable is used to control the motor . | ||
+ | # The multi-level "if" condition block in the second "forever" block judges the variable value changed in the first "forever" block, and directly outputs control signals to control the start, stop and speed of the left and right motors. | ||
+ | # First, the loop condition in the two "forever" blocks is the variable controlled by the "on button A pressed" block. The loop will execute only when the variable is "true". | ||
+ | # The third "forever" block is a bit ingenious. Its internal loop will only be executed once when the variable used to control the motor changes, corresponding to the content that the LED should display currently. | ||
− | + | [https://github.com/Wind-stormger/Makecode/blob/master/microbit-Triode-car_Line_Follower.hex '''Example link on Github'''] | |
− | + | Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly. |
Latest revision as of 02:24, 14 September 2021
Contents
Product Introduction
Control With LM393 Chip
Hardware Introduction
There is a button to switch between two modes on the Triode-Car. One of which is using the built-in LM393 comparator chip, and the other is with the Micro: bit or BPI: bit.
When using the built-in LM393 chip, it has only one function, to track dark lines, it consists of the Line Detecting Circuit, Voltage Comparing Circuit, and the Motor Circuit.
Line Detecting Circuit
The Triode-Car uses two pairs of photoresistor and LED on the bottom side to enable its line tracking feature. When the LED light reflecs on bright and dark surfaces, the resistance of the photoresistor will change drastically.
The photoresistor used on Triode-Car is the GL5506, this photoresistor can have a resistance as high as 0.2MΩ in the dark(10Lux), while in the light(400~600Lux), it can have a resistance as low as a 2-5KΩ.
(Bright resistance: Tested after shining with 400~600Lux light for 2 hours, then shine it with a standard 10Lux, with a color temperature of 2854K light source.) (Dark resistance: Tested on the 10th second after shutting off the 10Lux light source.)
Voltage Comparing Circuit
The Voltage Comparing Circuit includes the LM393 chip and several resistors. The LM393 consists of two independent precision voltage comparators with a low offset voltage. Its use is to compare two input voltages, then changes the output voltage accordingly. When the positive voltage from the comparator is higher than reverse voltage, the output will output high signal. When the positive voltage is lower than reverse voltage, it will output low signal. The LM393's output is open collector type, so a pull high resistor is needed here.
Motor Circuit
The S8550 PNP triode plays an important role in the motor circuit. It uses a small base current and a negative base voltage to control a larger emitter-collector current. With a maximum collector current of 1.5A, it can be used as a low level switch.
Control Circuit
As the picture shown above, when the car shifts towards the left, the right photoresistor will be directly on top of the line, therefore, its resistance will increase. Conversely, the left resistor, will shift away from the line, its resistance will decrease. At this point, the voltage from LL will be lower than LR. The U1A comparator's positive voltage will be higher than reverse voltage, triggering pin 1 to pull high. Conversely, U1B's positive voltage will be lower than reverse voltage, pin 7 will pull low, making the left motor move. The car shifts right, R6 and D2 moves away from the dark line until the left sensors detect the dark line. The concept is similar as before, the car repeats the actions above to progress forward.
Line-Detecting Circuit Status Table (LM393 and related pins status) | |||||||||||||
Note: Conditions are black lines on a white background. | |||||||||||||
Car lean Direction | Left LDR(R5) | Right LDR(R6) | LL Potential | LR Potential | P3 Input | P2 Input | P1 Output | P5 Input | P6 Input | P7 Output | M1 Left Motor | M2 Right Motor | Car Move Direction |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Left | Bright | Dark | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | Right |
Right | Dark | Bright | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | Left |
Calibrating Line-Detecting Sensitivity
To calibrate the Line-Detecting sensitivity under LM393 control, one would need to adjust the potentiometers physically. This process could help the Triode-Car to detect dark lines more.
Calibration process are as follows:
- Point a stable light source towards the photoresistors.
- Adjust either side of potentiometers until both motors are operating simultaneously.
- After adjustments, put the car on the track to test the results.
- If the car still runs off course, place the car on a white piece of paper, use a small object to lift up the back wheels slightly to prevent contact, repeat the steps 2~3 to adjust the potentiometers.
Use MakeCode to Control the Triode-Car
This is a tutorial on how to control the Triode-Car with Micro:bit Makecode.
Beginner Tutorial
Adding the Triode-Car Extention to Control with Micro: bit
It is recommended for beginners to install the extension blocks by typing in the extensions search bar: " https://github.com/bpi-steam/pxt-triodecar" to add extentions. Click to import the Triode-Car extension pack.
- As the blocks shown, the car will stop after bootup or reset.
- Pressing down button A and B at the same time will make the car go forward.
- Press A to turn Right.
- Press B to turn Left.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Motor Speed Adjustment
In the Triode-Car extensions, there are blocks dedicated to adjust the speed up to 10 levels.
Example Blocks:
- When button A is pressed, the speed increases by 1 level
- Press button B once, the speed decreases by 1 level
- Micro: bit display current speed.
The code within "forever" block will loop forever after the code in "on start" finishes. In between each loop, it can perform other actions such as, on button A/B pressed.
Added an "if" statement block to make sure the "speed" variable does not go higher than 10, if it goes higher, it will be set to 0 to prevent error messages.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Reading Analog Signals From Photoresistors
For Hardware Design, please refer to (Line Detecting Circuit).
The "Read (index) line tracking sensor" block in Triode-Car extensions can collect analog values from the photoresistors.
Micro: bit pins' voltage can be set from 0~3.3v, and has 1024(10bit) precision levels, therefore, the "read left/right line tracking sensor" block will input an analog value to adjust its voltage ranging from 0~1023.
Example Blocks:
In the program, every 100ms the two photoresistors will send its analog values through USB port to the computer. By opening the control panel in Makecode, we can see the exact values.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Use Two Micro: bit to Control Eachother's Motors Through Radio
Micro: bit supports radio connection, enable the radio extension on micro: bit, then it is possible for two micro: bit to communicate with eachother.
Example Blocks:
By flashing the program onto the Micro: bit, and installing onto the Triode-Car, we can control the other car by pressing A/B buttons.
- Pressing button A allows it to turn left
- Pressing button B to turn right
- Pressing button A+B to go forward
- Releasing all buttons to stop.
The four variables added are not superfluous. Although you can also fill in the values we need in the building blocks, using variable names to replace these specified values helps us to establish a clearer cognitive logic. The more complex procedures can reflect its value.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Bluetooth Control
The bluetooth feature can be enabled through MakeCode extensions, we can control the micro: bit with our smartphones through certain applications.
- iOS users: Micro:bit App on App Store
- Android users:Microbit-blue GitHub linkor: Kitronik Move on Google Play Store
Note: There is an official release of the Micro: bit App on both the iOS App Store and Android's Google Play Store, however, the iOS version has support for bluetooth connection, whereas the Google Play store's does not. This is why we require third party apps on Android devices that supports bluetooth connection.
Bluetooth pairing process and notes:
- In the MakeCode editor, open the extensions on the bottom of advanced tab, click on the bluetooth extension icon to apply.Bluetooth and Radio extensions cannot coexist, choosing bluetooth will automatically remove radio extension.
- When pairing the micro: bit with android third-party app, one could turn on "Passkey pairing: Paring requires 6 digit key to pair." option in Project settings.
- To enter pairing mode, connect micro: bit to power, press reset button once while holding A+B button.
Do NOT let go of A+B buttons while the progress bar is running, it would interrupt the pairing process and one would need to start over.
- Android devices need to turn on GPS and bluetooth to have a stable connection.
- On Android devices, navigate to bluetooth settings, find and pair with the micro: bit. Once the pairing process starts, micro: bit will hint to press button A once, then it would scroll a 6 digit pairing code. Enter the code on the Andriod device to complete the pairing process.
- Press the reset button on the Micro: bit after pairing to restart.
- iOS devices does not require to enter bluetooth settings. To pair with the micro: bit, please follow the pairing instructions in the official micro: bit app.
Example Blocks:
When paired successfully with bluetooth, display a red heart, whereas a disconnection would display an "X" symbol. Press the buttons inside the app to control the Triode-Car wirelessly. Press button A to go forward, C to turn left, and D to turn right, release the buttons to stop.
The "on event from()with value()" block can be found in the Control tab in Makecode.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Advanced Tutorial
Calibrating the Line-Detecting Circuit
As every component are not made exactly the same, despite being made according to the datasheet, they would vary from eachother ever so slightly. Calibration of the Line-Following Circuit improves the reaction time of the Triode-Car's line tracking feature. Adjust the resistance of the potentiometers connected to the photoresistors to increase the sensitivity. This process makes the Triode-Car less likely to run off course and maintains a stable analog voltage output.
We can directly use the "Reading Analog Signals From Photoresistors" guide from beginners tutorial to read the analog signals from the computer, then manually adjust the potentiometer with a screwdriver.
The Micro: bit can detect analog voltage values from 0~1023, the closer the value is to the middle, the reaction from changes of brightness detected by the photoresistor will become more sensitive.
Therefore, under a stable brightness environment, try to calibrate the resistance as close to 512 as possible. This would create more stable energy output and reduce their analog value difference.
The above steps mentioned requires constant connection with triode-car connected to micro: bit and then to the PC. In a real use case scenerio, one would best be using manual adjustments to increase the sensitivity.
We cannot be sure to have the micro: bit connected to the PC all the time, therefore, we need to write a program to guide us how to properly calibrate the sensitivity.
Calibration Steps:
- Adjust the left potentiometers first, to make its analog value out close to the median.
- Adjust the right potentiometer on the premise that the previous condition is met, so that the parallel photoresistor output voltage analog quantity on the corresponding pin of Micro:Bit is close to the other photoresistor.
We can achieve this by creating a simple "if" statement program. For those people who adjust manually, we could add different image patterns to indicate if we have adjusted properly.
Example Blocks:
The code is a bit complex, detailed explaination are as follows:
- The whole calibration code is organized into a self-defined function, this could be easily called or maintained later on and would cause less confusion as many other functions would be added.
- The code consists within a while loop block. By adding loop conditions, the loop would exit after the potentiometers have finished calibration.
- Before entering the while loop, the micro:bit would display a directional arrow, indicating which potentiometers requires adjustment.
- The code within the first while loop block is to calibrate the left photoresistor, the "if" statement conditions are set to make the left photoresistor's analog voltage value in the range of minimum of 450 to 550 max.
- When the "if" statement contitions are met in the 4th point, the micro:bit would display a "checked" symbol to give a visual feedback as its been calibrated. At this moment one should stop adjusting the left potentiometer. After 1 second, the code would run the "if" statement again to check if the values are disrupted due to slight tremor of the hand. When the code double checked if conditions are met, the loop will end.
- The second while loop contains blocks to adjust the right photoresistor. The "if" statement within is to make the analog voltage values differce of both potentiometers less than 25. This is achieved by subtracting the values of both left and right photoresistors.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.
Line Tracking
After "Calibrating the Line-Detecting Circuit", we can start to make effective use of the Line-Detecting Circuit.
According to the principles described in the "Line-Detecting Circuit" chapter,line trackingrequires real-time light intensity detection by using the different reflectivity of dark lines and its two sides.
Read the voltage analog values of the left and right LDRs in the program and compare the values to determine whether the Triode-Car's travel route is offset and the direction of the offset, and then the start and stop of the left and right motors are controlled and corrected. The driving direction of Triode-Car achieves the purpose of driving along dark lines.
Example Blocks:
The code is a bit complex, detailed explaination are as follows:
- The "on button A pressed" block is used to control the start and stop of the tracking program. Each time button A is pressed, the variables set in it will change its state.
- The "forever" block will repeatedly execute its internal program, and each time the loop ends or the "show" or "pause" block is executed in the loop, the thread will allow other "forever" blocks or event-based loops, so this Three "forever" blocks and one "on button A pressed" block can run together in the background, so that the system has the ability to execute multiple programs at the same time. This is generally called "multitasking".
- The program in the first "forever" block is used to cyclically read the voltage analog values of the left and right LDRs, and then judge in the multi-level "if" conditional block, and change the variable when the corresponding conditions are met. The variable is used to control the motor .
- The multi-level "if" condition block in the second "forever" block judges the variable value changed in the first "forever" block, and directly outputs control signals to control the start, stop and speed of the left and right motors.
- First, the loop condition in the two "forever" blocks is the variable controlled by the "on button A pressed" block. The loop will execute only when the variable is "true".
- The third "forever" block is a bit ingenious. Its internal loop will only be executed once when the variable used to control the motor changes, corresponding to the content that the LED should display currently.
Project files can be downloaded and edit locally by importing to MakeCode, or flash it directly onto the Micro:bit via USB to run it directly.