What colour is that? — Smart Car
This one is a two-step example, and that is on purpose. First the robot tells you what it sees. Then you type those numbers into the compare block and it recognises that colour whenever it comes back.
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
- Print to serial
"Colour sensor. Hold something coloured right against the sensor." - Print to serial
"STEP 1: read the four numbers below." - Print to serial
"STEP 2: type the first three into the compare block and send it again." - wait 2 seconds
- repeat forever
- Print to serial (create text with
"red, green, blue, brightness = "Read color sensor) - if (is Read color sensor the color: R 2000 G 900 B 800 ± 600 .)
- Set LED 2 ON
- else
- Set LED 2 OFF
- wait 0.5 seconds
- Print to serial (create text with
What you need
Section titled “What you need”- A Smart Car with the colour sensor fitted (underneath, or on the front, depending on your kit).
- A USB cable.
- Two or three pieces of coloured card, or coloured bricks. Strong flat colours work far better than shiny or patterned things.
How to run it — step 1, look
Section titled “How to run it — step 1, look”-
Open 08-colour-detect.mobot, plug in, press Send to Device.
-
Hold a piece of red card right against the sensor — almost touching.
-
Read the serial monitor:
[MOBOT] red, green, blue, brightness = (2143, 874, 792, 3654) -
Move the card away and watch all four numbers change. If they never change, the sensor is not connected — stop here.
How to run it — step 2, recognise
Section titled “How to run it — step 2, recognise”- Write down the first three numbers you saw for your red card.
- In MoBot, find the is … the color: R … G … B … ± … block and type your three numbers into R, G and B.
- Leave the ± at 600 to begin with.
- Press Send to Device again.
- Hold the red card against the sensor: LED 2 lights up. Hold a blue or green card: LED 2 goes out.
The test passes when LED 2 lights for your chosen colour and stays dark for a different one.
About those numbers
Section titled “About those numbers”They are not the 0-255 red/green/blue you may have met in a paint program. They are raw counts straight from the sensor and they can run into the thousands. They also change with the light in the room and with how close the card is. That is why this example makes you read them off your own robot instead of printing numbers here that would be wrong for you.
The ± is how fussy the robot is. Small (say 100) means “almost exactly this colour”. Large (say 1500) means “close enough”. If LED 2 never lights, make ± bigger. If it lights for everything, make it smaller.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_readcolor |
gives four numbers: red, green, blue and overall brightness. |
mobot_compcolor |
true when the reading is within ± of the R, G, B you typed. |
mobot_ledon |
the answer light. |
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.
# What colour is that? — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 08-colour-detect.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 08-colour-detect.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Colour sensor. Hold something coloured right against the sensor.')print("[MOBOT]", 'STEP 1: read the four numbers below.')print("[MOBOT]", 'STEP 2: type the first three into the compare block and send it again.')time.sleep(2)while True: print("[MOBOT]", ('red, green, blue, brightness = ' + str(robot.color_sensor.read()))) if robot.color_sensor.is_color([2000, 900, 800], robot.color_sensor.read(), tolerance=600): robot.led2.set(1) else: robot.led2.set(0) time.sleep(0.5)# What colour is that? — robotics## This is what MoBot puts on the board as main.py when you open# 08-colour-detect.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
print("[MOBOT]", 'Colour sensor. Hold something coloured right against the sensor.')print("[MOBOT]", 'STEP 1: read the four numbers below.')print("[MOBOT]", 'STEP 2: type the first three into the compare block and send it again.')time.sleep(2)while True: print("[MOBOT]", ('red, green, blue, brightness = ' + str(mobotROBOTICS.color_sensor.readColor()))) if mobotROBOTICS.color_sensor.compColor(mobotROBOTICS.color_sensor.readColor(), [2000, 900, 800], th=600): mobotROBOTICS.led2.setState(1) else: mobotROBOTICS.led2.setState(0) time.sleep(0.5)Try changing it
Section titled “Try changing it”- Add a second compare block for a different colour and light LED 1 instead — now the robot can tell two colours apart.
- Print the ± test as well as the numbers, by dropping the compare block into a
Print to serialblock. Now you can seeTrueandFalsescroll past. - Shine a torch on the card and watch every number jump. Sensors have opinions about lighting.