|  | 
|  | 1 | +Sensors tutorial | 
|  | 2 | +================ | 
|  | 3 | + | 
|  | 4 | +The GiggleBot comes with a couple of onboard sensors: | 
|  | 5 | + | 
|  | 6 | +#. two light sensors, near the front eyes LEDs, | 
|  | 7 | +#. two line sensors, on the underside of the line follower. | 
|  | 8 | + | 
|  | 9 | +Light Sensors | 
|  | 10 | +------------- | 
|  | 11 | + | 
|  | 12 | +The GiggleBot comes with two light sensors right in front of the LED on each eye. They are very small and easy to miss.  | 
|  | 13 | +However, they are more versatile than the Micro:Bit's light sensor as they can be read together to detect which side is receiving more light. | 
|  | 14 | + | 
|  | 15 | +The main method to query the light sensors is :py:meth:`~gigglebot.read_sensor()`.  The same method is used for both light sensors and line sensors.  | 
|  | 16 | + | 
|  | 17 | +Here's how to read both light sensors in one call: | 
|  | 18 | + | 
|  | 19 | +.. code:: | 
|  | 20 | +
 | 
|  | 21 | +   left, right = read_sensor(LIGHT_SENSOR, BOTH) | 
|  | 22 | +
 | 
|  | 23 | +And here's how to read just one side at a time: | 
|  | 24 | + | 
|  | 25 | +.. code:: | 
|  | 26 | +
 | 
|  | 27 | +   right = read_sensor(LIGHT_SENSOR, RIGHT) | 
|  | 28 | +
 | 
|  | 29 | +
 | 
|  | 30 | +Chase the Light | 
|  | 31 | +^^^^^^^^^^^^^^^ | 
|  | 32 | + | 
|  | 33 | +This tutorial will turn the GiggleBot into a cat, following a spotlight on the  | 
|  | 34 | +floor. Many cats do that when you shine a flashlight in front of them, they will  | 
|  | 35 | +try to hunt the light spot. When running this code, you will be able to guide  | 
|  | 36 | +your GiggleBot by using a flashlight.  | 
|  | 37 | + | 
|  | 38 | +This is how to use this project: | 
|  | 39 | + | 
|  | 40 | +#. start GiggleBot and wait for sleepy face to appear on the Micro:Bit, | 
|  | 41 | +#. press button A to start the Chase the Light game (heart will replace the sleepy face), | 
|  | 42 | +#. chase the light as long as you want, | 
|  | 43 | +#. press button B to stop the GiggleBot and display sleepy face again. | 
|  | 44 | + | 
|  | 45 | +First, a bit of explanation on the algorithm being used here. | 
|  | 46 | + | 
|  | 47 | +On starting the GiggleBot, the Micro:Bit will: | 
|  | 48 | + | 
|  | 49 | +#. assign a value to the `diff` variable (here it is using 10), | 
|  | 50 | +#. display a sleepy face image, | 
|  | 51 | +#. resets whatever readings from the buttons it might have had,  | 
|  | 52 | +#. then start a forever loop. | 
|  | 53 | + | 
|  | 54 | +This forever loop only waits for one thing:  | 
|  | 55 | +for the user to press button A, and that's when the light chasing begins. | 
|  | 56 | + | 
|  | 57 | +As soon as button A is pressed, the Micro:Bit will display a heart as it's  | 
|  | 58 | +quite happy to be active! And then it starts a second forever loop! This second  | 
|  | 59 | +forever loop is the actual Light Chasing game. It will end when button B is  | 
|  | 60 | +pressed by the user. | 
|  | 61 | + | 
|  | 62 | +How to chase a light: | 
|  | 63 | + | 
|  | 64 | +#. take reading from both light sensors, | 
|  | 65 | +#. compare the readings, using `diff` to allow for small variations. You will most likely get absolutely identical readings, even if the light is mostly equal. Using a differential value helps stabilize the behavior. You can adapt to your own lighting conditions by changing this value. | 
|  | 66 | +#. if the right sensor reads more than the left sensor plus the `diff` value, then we know it's brighter to the right. Turn right. | 
|  | 67 | +#. if the left sensor reads more than the right sensor plus the `diff` value, then it's brighter to the left. Turn left. | 
|  | 68 | +#. if there isn't that much of a difference between the two sensors, go straight.  | 
|  | 69 | +#. if button B gets pressed at any time, stop the robot, change sleepy face, and get out of this internal loop. The code will fall back to the first loop, ready for another game. | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | + | 
|  | 73 | +.. code:: | 
|  | 74 | +
 | 
|  | 75 | +   from gigglebot import * | 
|  | 76 | +   # value for the differential between the two sensors. | 
|  | 77 | +   # you can change this value to make it more or less sensitive. | 
|  | 78 | +   diff = 10 | 
|  | 79 | +   # display sleepy face | 
|  | 80 | +   microbit.display.show(microbit.Image.ASLEEP) | 
|  | 81 | +   # the following two lines resets the 'was_pressed' info  | 
|  | 82 | +   # and discards any previous presses | 
|  | 83 | +   microbit.button_a.was_pressed() | 
|  | 84 | +   microbit.button_b.was_pressed() | 
|  | 85 | +
 | 
|  | 86 | +   # start first loop, waiting for user input | 
|  | 87 | +   while True: | 
|  | 88 | +       # test for user input | 
|  | 89 | +       if microbit.button_a.was_pressed(): | 
|  | 90 | +           # game got started! Display much love | 
|  | 91 | +           microbit.display.show(microbit.Image.HEART) | 
|  | 92 | +
 | 
|  | 93 | +           # start game loop | 
|  | 94 | +           while True: | 
|  | 95 | +               # read both sensors | 
|  | 96 | +               left, right = read_sensor(LIGHT_SENSOR, BOTH) | 
|  | 97 | +
 | 
|  | 98 | +               # test if it's brighter to the right | 
|  | 99 | +               if right > left+diff: | 
|  | 100 | +                   turn(RIGHT) | 
|  | 101 | +
 | 
|  | 102 | +               # test if it's brighter to the left  | 
|  | 103 | +               elif left > right+diff: | 
|  | 104 | +                   turn(LEFT) | 
|  | 105 | +
 | 
|  | 106 | +               # both sides being equal, go straight | 
|  | 107 | +               else: | 
|  | 108 | +                   drive(FORWARD) | 
|  | 109 | +
 | 
|  | 110 | +               # oh no, the game got interrupted | 
|  | 111 | +               if microbit.button_b.is_pressed(): | 
|  | 112 | +                   stop() | 
|  | 113 | +                   microbit.display.show(microbit.Image.ASLEEP) | 
|  | 114 | +
 | 
|  | 115 | +                   # this line here gets us out of the game loop | 
|  | 116 | +                   break | 
|  | 117 | +
 | 
|  | 118 | +What else can be done with the light sensors? | 
|  | 119 | + | 
|  | 120 | +You could modify this code to turn the GiggleBot into a night insect? Those would  | 
|  | 121 | +avoid light instead of chasing it.  | 
|  | 122 | + | 
|  | 123 | +You could detect when it gets dark or bright. Imagine the GiggleBot inside your  | 
|  | 124 | +closet. When someone opens the door, the sudden light can be detected. The GiggleBot  | 
|  | 125 | +can let you know someone went through your things while you were away. | 
|  | 126 | + | 
0 commit comments