Light Measurements Graphic Display

The micro:bit module’s display can be used to show which side of the cyber:bot is detecting brighter light.  The following program shows a visual display of where the brigher light is hitting, reflecting the value of the norm_diff_shade variable.  If the light is on the right side, the display will light up a pixel on the right side, if the light is in the middle it will light up a pixel in the middle, and so on.

ERROR FALSE ALARM! On line 11 of the script norm_diff_shade_display, you may see a compiler error warning "Operator "+" not supported for this combination of types."  This is just a false alarm.  The script actually works just fine, so go ahead and send it to the micro:bit.

Example script: norm_diff_shade_display

  • Put the cyber:bot board's power switch in position 1.
  • Enter, save, and flash the script norm_diff_shade_display.
# norm_diff_shade_display

from cyberbot import *
 
while True:                       
    bot(8).write_digital(1)        
    qt_left = bot(8).rc_time(1)   
    bot(6).write_digital(1)        
    qt_right = bot(6).rc_time(1)   
    
    norm_diff_shade = (qt_right / (qt_right + qt_left)) - 0.5
    display.clear()
    
    if norm_diff_shade > -0.5 and norm_diff_shade <= -0.35:
        display.set_pixel(0, 2, 9)
    elif norm_diff_shade > -0.35 and norm_diff_shade <= -0.1:
        display.set_pixel(1, 1, 9)
    elif norm_diff_shade > -0.1 and norm_diff_shade <= 0.1:
        display.set_pixel(2, 0, 9)
    elif norm_diff_shade > 0.1 and norm_diff_shade <= 0.35:
        display.set_pixel(3, 1, 9)
    elif norm_diff_shade > 0.35 and norm_diff_shade < 0.5:
        display.set_pixel(4, 2, 9)
    sleep(50)
  • Try casting different levels of shade over each light sensor, and watch how the pixel in the micro:bit module’s display responds. It should match the illustrations below.