We will next use the ship’s movement to control your image’s orientation. But first, let’s control our image’s orientation ourselves. In order to focus exclusively on movement for a moment, we are going to comment out all of the code we just wrote for the color sensor using three quotation marks '''
. This has the same effect as #
but allows us to turn multiple lines of code into a comment instead of just one. Place three quotes on the line before our color sensor section begins and three quotes at the end of our code, which will comment out all lines between these two sets of quotation marks. We will add our next section after these three quotes.
We have a few commands for controlling the image orientation. We can change the “floor” that the image sits on by using the command set_rotation()
. By default, the rotation is set to 0°, which has the floor of the SenseHat agree with the writing/labels on the SenseHat board: the GPIO pins are on top and the joystick is on the bottom. This is the orientation of the Ocean Pi array on Wonder with the added detail that the Raspberry Pi is not sitting flat on a horizontal surface the way it might on your desk. Instead, it is mounted vertically, so that the HDMI port(s) are facing the deck.
With this in mind, running set_rotation(180)
will flip our creature upside down, but it will do so as if it were rotated. In the case of our whale, this means it will now be facing the USB ports while it is upside down instead of facing the microSD slot like it was at set_rotation(0)
. If we want to flip our creature on its head like we are flipping a page on a calendar instead of rotating 180°, we can use flip_v()
to flip the image vertically. Similarly, we can make our creature look to the left or the right by using flip_h()
to flip the image horizontally.
To practice this rotation, let’s write some code to make our creature swing back and forth like it is a skateboarder on a half pipe. Since the whale starts out facing to the left, we can have it rotate up the left-side wall using set_rotation(90)
. Then, it will turn and face the other direction using flip_h()
come back down to the original 0° position, continue to the right-side wall using set_rotation(270)
, and turn once more using flip_h()
. We can make this loop over and over again as a test.
#### Rotate our creature
while True:
sense.set_rotation(90)
sense.flip_h()
sense.set_rotation(0)
sense.set_rotation(270)
sense.flip_h()
Hmmmm, that does not work as expected. The whale rotates, but gets stuck at one of the angles. Actually, the code is working, but it is looping so quickly that we do not see it happening. We need to slow our code down at each step, so we can add a 1 second pause using sleep(1)
.
#### Rotate our creature
while True:
sense.set_rotation(90)
sleep(1)
sense.flip_h()
sleep(1)
sense.set_rotation(0)
sleep(1)
sense.set_rotation(270)
sleep(1)
sense.flip_h()
sleep(1)
One last error, we forgot to add the set_rotation(0)
after the final flip_h()
. Since this is a loop, we can add it to the beginning or the end. To ensure that the animation always begins in the starting position, I will add it to the beginning with another sleep(1)
. Here is a full snapshot of our entire program at this stage:
#### 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, g, g, f,
f, g, g, g, g, g, g, f,
g, g, g, g, g, g, d, f,
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)
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
while True:
rgb = sense.color # get the colour from the sensor
# Store the current sensed values for comparison with the previous colors
current_colors = (rgb.red, rgb.green, rgb.blue) #
changes = [current_colors[0]-previous_colors[0], current_colors[1]-previous_colors[1],current_colors[1]-previous_colors[1]]
increase = min(changes)
decrease = max(changes)
increase_position = changes.index(increase)
decrease_position = changes.index(decrease)
modifier = 40
current_colors_modified = list(current_colors)
current_colors_modified[increase_position] = current_colors_modified[increase_position] + modifier
current_colors_modified[decrease_position] = current_colors_modified[decrease_position] - modifier
# Check that color values are within correct ranges
for i in range(3):
if current_colors_modified[i] > 255:
current_colors_modified[i] = 255
if current_colors_modified[i] < 0:
current_colors_modified[i] = 0
# Convert color values into proper (R, G, B) tuple format from [R, G, B] array format
f = tuple(current_colors_modified)
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)
'''
#### Rotate our creature
# Testing rotation
while True:
sense.set_rotation(0)
sleep(1)
sense.set_rotation(90)
sleep(1)
sense.flip_h()
sleep(1)
sense.set_rotation(0)
sleep(1)
sense.set_rotation(270)
sleep(1)
sense.flip_h()
sleep(1)