You are going to display the IR data on Zumi’s screen. To get IR data you’ll need a Zumi object, and to display on the screen you need to create a Screen object.
Import libraries
In [ ]:
from zumi.zumi import Zumi
from zumi.util.screen import Screen
import time
zumi = Zumi()
screen = Screen()
What does the data mean?
IR sensors work by emitting an infrared pulse and measuring the infrared light that returns after it bounces off an object. This number will be between 0 and 255. A lower number indicates that something in the vicinity is reflecting the IR light back to the sensor.
Run each cell and watch the numbers on the screen change as you cover the sensors. The code will use a for loop to take 50 readings.
Front IR sensors
In [ ]:
for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
front_right_ir = ir_readings[0]
front_left_ir = ir_readings[5]
message = " IR readings "
message = message + str(front_right_ir) + ", " + str(front_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")
Back IR sensors
In [ ]:
for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
back_right_ir = ir_readings[2]
back_left_ir = ir_readings[4]
message = " IR readings "
message = message + str(back_right_ir) + ", " + str(back_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")
Bottom IR sensors
Bottom IR sensors are great for line following or detection. Use the Bottom IR worksheet to test values between light and dark.
In [ ]:
for i in range(0,50):
ir_readings = zumi.get_all_IR_data()
bottom_right_ir = ir_readings[1]
bottom_left_ir = ir_readings[3]
message = " IR readings "
message = message + str(bottom_right_ir) + ", " + str(bottom_left_ir)
screen.draw_text(message)
time.sleep(0.1)
screen.draw_text_center("Done!")