Playing with the nose of the unicorn

Playing with the nose of the unicorn Titelbild

Playing With The Nose Of The Unicorns

You may have already noticed the big orange-yellow nose of the Unicorn. This nose is not only looking really cool, but it is also a photoresistor. The difference between this and a normal resistor is that the resistance of the photoresistor is not fixed and actually varies. In a completely dark room the resistance will be close to a few Mega-Ω(Ohm) which is quite a lot. The brighter it is, the lower the resistance will be. All we need now is a capacitor. A capacitor can save current and emit it again. You can think of the capacitor like a small battery pack which can save and emit current. The difference is that a capacitor can save a lot less energy. The amount of energy that a capacitor can save is measured in Farad or for short, F. You can find the capacitor on the right ear of the Unicorn Shield.

Up to this point, we have worked a lot with digital values and learned to measure True or False condition evaluation. This is really practical if we want to measure the state of a button which is either pressed (True) or not pressed (False). These digital values are really easy for the Raspberry Pi to measure. It simply checks if the voltage at the time of measurement is higher than 2V on a gpio pin. There are also many other situations in which it is really practical to measure something between 1 and 0 and that is when measuring analogue values. To do this we are using the photoresistor in combination with the capacitor.

import unicornshield as unicorn
from time import sleep

while True:
if unicorn.nose() > 0.3:

After initially executing the program you should see no change. Once you cover the nose of the unicorn with your thumb or hand however, the magic happens. At that point you will see all of the LEDs on the Unicorn turned on for four seconds and then turned off again. This program will continue running until you stop it with Ctrl + C.

Step by step

The main new item in this program occurs in line 5. In line 5 you will find an if condition inside the while loop. This will be the first time in our Unicorn journey that we are not using the == equal operator. Instead, the >, or greater-than sign, is being utilized. We are using this sign to check the return value of unicorn.nose(). This line simply says, that if the return value of unicorn.nose() is greater than 0.3 then execute all of the intended lines of code.

In line 6 we are turning the Pixel LEDs on and assigning them the color purple. In line 7 a sleep() function which is used to halt the program so that we can see the beautiful Pixel LEDs and in line 8 they are turned off again.

You may need to make some changes to the value in line 5 depending on the brightness of your surrounding area. You may test your current brightness with print(unicorn.nose()) and adjust the condition in line 5 as needed.

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!