19. Case18:Color-controlled TPBot

19.1. Purpose


  • The rainbow LED changes in accordance with the color of the cards and it executes the defined functions accordingly(move forward,;change the color of the headlights at random; obstacles avoidance; line-following)

19.2. Material

1 x TPBot

../../_images/TPBot_tianpeng_case_01_01.png

19.3. Hardware connection

Connect rainbow led to port 1 and color sensor to IIC port on TPBot.

../../_images/TPBot_tianpeng_case_18_03.png

19.4. Software


MicroSoftmakecode

19.5. Programming


Click “Advanced” to see more choices in the MakeCode drawer.

../../_images/TPBot_tianpeng_case_01_02.png

  • We need to add a package for programming. Click “Extensions” in the bottom of the drawer and search with “tpbot” in the dialogue box to download it.

../../_images/TPBot_tianpeng_case_01_03.png

  • We need to add a package for programming. Click “Extensions” in the bottom of the drawer and search with “PlanetX” in the dialogue box to download it.

../../_images/TPBot_tianpeng_case_15_03.png

19.6. Sample

  • In on start brick, drag the show icon brick into it and initialize the led strip and has it connected to J1 port.

../../_images/TPBot_tianpeng_case_18_04.png

  • In forever brick, set the rainbow led in white if the color sensor detects white, and set TPBot move forward at the speed of 30%.

../../_images/TPBot_tianpeng_case_18_05.png

  • Set the rainbow led in red if the color sensor detects red, set TPBot stop moving and the RGB value of the headlight a random number among 0~225.

../../_images/TPBot_tianpeng_case_18_06.png

  • Set the rainbow led in yellow if the color sensor detects yellow, and judge if the returned value from the sonar:bit is between 3~20. If yes, set TPBot turn left for 1s at the speed of 30% or move forward at the speed of 30%.

../../_images/TPBot_tianpeng_case_18_07.png

  • Set the rainbow led in green if the color sensor detects green, and judge the status of the two-way line-tracking sensor. If black is detected on the left, set the speed of the left wheel as 0 and right as 40; if it is detected on the right, set the speed of the left wheel as 40 and right as 0; if it is detected on both sides, it means the TPBot does not deviate from the black line, and set it move foreward at the speed of 25%.

../../_images/TPBot_tianpeng_case_18_08.png

  • Set the rainbow led in blue if the color sensor detectsblue, set the TPBot stop moving.

../../_images/TPBot_tianpeng_case_18_09.png

Code:

../../_images/TPBot_tianpeng_case_18_10.png

19.7. Exploration


19.8. FAQ


Q: While operating this case, why the car might not work properly?

A: It might be the low power of the batteries, please try adding the value of TPBot’s speed and test again.

19.9. Relevant File