Now we will add code for using the SenseHat’s color sensor. Keep it up with the comments as we continue the project. Typing “#” tells the computer to ignore whatever is written on that line of code. We call this a comment. We can use multiple “#” to denote hierarchy, so in this project, “####” is a major section of the code and “#” is a subsection. Use whatever commenting convention works for you!
Just like your eyes adjust for how bright the surroundings are, we need to tell our color sensor how sensitive it needs to be. Gain is kinda like telling the color sensor whether it needs to squint or open its eyes as wide as possible. There are four possible values for gain: 1, 4, 16, and 60. If you have a SenseHat and Raspberry Pi, you can do experiments in different lighting scenarios to see the different results you get from different gain settings. We recommend setting gain to either 16 or 60.
#### Code for using the color sensor
# Set up the sensors
sense.color.gain = 60 # Set the sensitivity of the sensor
sense.color.integration_cycles = 64 # The interval at which the reading will be taken
The first thing we will do is grab a color from the color sensor and make that the background color. You can choose to have the color sensor affect any of the colors in your design. You could even have your color sensor only affect, one portion of the color, like just the amount of green.
We will also need to pause our program in between our original image and our sensor-based image. This requires that we import another library called sleep
at the start of the program. If we don’t use sleep()
then our first image will be displayed so quickly that we won’t see it, since the code will be read and run as quickly as the computer’s processor.
#### Housekeeping
# Libraries
from sense_hat import SenseHat
sense = SenseHat()
from time import sleep
#### Code that makes my original image on the LED matrix
# Declare my colors as RGB values and store them in variables
d = (255, 255, 255) # Cyan
f = (25, 25, 112) # MidnightBlue
g = (0, 191, 255) # DeepSkyBlue
b = (0, 0, 0) # Black
# Arrange my colors on an 8x8 matrix to make an image. This is a whale I made.
image = [
f, f, f, g, g, g, g, g,
f, f, g, g, f, g, g, f,
f, f, f, f, f, f, g, g,
f, g, g, g, g, g, g, g,
g, g, g, g, g, g, d, d,
g, b, g, g, g, d, d, f,
g, g, g, g, d, d, f, f,
f, g, g, d, d, f, f, f]
# Code telling the pixels to light up
sense.set_pixels(image)
# Pause the program to show this image
sleep(1) # numbers are in seconds
#### Code for using the color sensor
# Set up the sensors
sense.color.gain = 60 # Set the sensitivity of the sensor
sense.color.integration_cycles = 64 # The interval at which the reading will be taken
# Use the sensor
rgb = sense.color # get the colour from the sensor
f = (rgb.red, rgb.green, rgb.blue) # re-assign the variable "f" to be the sensed color
# Re-declare the contents of "image" now that "f" has a new definition.
image = [
f, f, f, g, g, g, g, g,
f, f, g, g, f, g, g, f,
f, f, f, f, f, f, g, g,
f, g, g, g, g, g, g, g,
g, g, g, g, g, g, d, d,
g, b, g, g, g, d, d, f,
g, g, g, g, d, d, f, f,
f, g, g, d, d, f, f, f]
# Code telling the pixels to light up, but with a new "f" value
sense.set_pixels(image)
Notice how, at first, f
is assigned the value of Midnight Blue, which has an RGB value of (25, 25, 112). Then, later in the code, we change f
to be whatever RGB values the color sensor collects. This is the beauty of variables—we can change their value at any time. What is important to notice here, though, is that if we did not restate image
after redefining f
, then there would be no change. You can see this if you delete the second image
array and run the code: your background will not change color. This is an example of how order of operations matters in code. Since image
was first defined when f
was Midnight Blue, that definition of image will continue throughout the program unless we re-declare image
after f
has a new identity.