How the Whisker Test Code Works

How the script whiskers_detect_test works

The main part of the program is contained within the while True: loop, this is so the program continuously checks whether or not the whiskers are being touched and therefore it knows whether the LEDs should be on or off.

while True:

 

The first thing that the script does once it enters the loop is checks the whisker states using the read_digital function and stores them as a boolean 0 (whisker pressed) or 1 (whisker not pressed) to the variables whisker_left for the P7 state, and whisker_right for the P9 state. 

whisker_left = bot(7).read_digital()
whisker_right = bot(9).read_digital()

 

The script then enters into the first if statement.  It checks to see if the value of whisker_left is 0, if it is 0, then the micro:bit module uses the display.set_pixel command to light up the LED located at (4,2) using the maximum brightness of 9.  Any other value, with the only other boolean option being 1, falls under the else statement.  The else statement turns off the pixel located at (4,2) with a brightness of 0.

if whisker_left == 0:
    display.set_pixel(4, 2, 9)
else:
    display.set_pixel(4, 2, 0)

 

The second if statement does the same process for the other whisker.  It checks to see if the value of whisker_right is 0, and if so, then the micro:bit module lights up the LED located at (0,2) with the maximum brightness of 9 using the display.set_pixel command.  Any other value, with the only other boolean option being 1, falls under the else statement which turns off the pixel at (0,2) with a brightness of 0.

if whisker_right == 0:
    display.set_pixel(0, 2, 9)
else:
    display.set_pixel(0, 2, 0)

 

After it runs through the two if statements, the while True: loop resets and starts over by checking the whisker states again.