Reference no: EM133577545
LAB: Familiar with a Sense Hat
Overview:
In this lab, you will learn how to control the LEDs, display an image, character, message in the sense hat; monitor the temperature, pressure, and humidity; play with the joystick.
Exercise 1: Controlling LEDs
Control the color in a specific LED on Sense Hat. Run the following sample code that sets LED in row 1 and column 1 to red.
from sense_hat import SenseHat
sense = SenseHat() # create a sense hat object
sense.clear() # clear all LEDs red = (255, 0, 0)
sense.set_pixel(1, 1, red)
You should also try to set LED to different colors, such as green = (0,255,0) and blue=(0,0,255).
Question 1: Please write the code to draw a green diagonal line.
Exercise 2: Displaying an Image
Run the following code that displays a "smiling" face using LEDs.
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
e = (0, 0, 0)
w = (255, 255, 255)
image = [
e,e,w,w,w,w,e,e,
e,w,e,e,e,e,w,e,
w,e,w,e,e,w,e,w,
w,e,e,e,e,e,e,w,
w,e,w,e,e,w,e,w,
w,e,e,w,w,e,e,w,
e,w,e,e,e,e,w,e,
e,e,w,w,w,w,e,e
]
sense.set_pixels(image)
Question 2: Please write the code to display a "smiling" face when you are happy and "sad" face when you are sad.
Exercise 3: Displaying a Single Character
Run the following sample code that displays the letter "%" with red color.Run the following sample code that displays the letter "%" with red color.
from sense_hat import SenseHat
sense = SenseHat()
red = (255, 0, 0)
sense.show_letter("%", red)
You can also display any single digit (e.g., 5) using the same method.
Question 3: Please write the code to repeatedly display letters "E", "L" and "E" every 1 second.
Hint: you may want to use the sleep() function from the Python time module. For example, the following code shows that the program stops for one second.
from time import sleep
sleep(1)
Exercise 4: Displaying a MessageRun the following sample code that displays a message "ELE" with rolling speed of 0.1.
from sense_hat import SenseHat
sense = SenseHat()
blue = (0, 0, 255)
yellow = (255, 255, 0)
while True:
sense.show_message("ELE", text_colour=yellow,
back_colour=blue, scroll_speed=0.1)
Question 4: Please write the code to display the message "ELE 409" with "blue" text color and empty background color.
Exercise 5: Sensing the EnvironmentRun the following sample code that measures the pressure, temperature, and humidity.
from sense_hat import SenseHat
sense = SenseHat()
while True:
t = round(sense.get_temperature(),1) p = round(sense.get_pressure()/10,1)
h = round(sense.get_humidity(),1)
temp = "Temperature: " + str(t) + " C" + "\n" pres = "Pressure: " + str(p) + " kPa" + "\n"
humi = "Pressure: " + str(h) + " %"
message = temp + pres + humi print(message)
sleep(2)
Question 5: Please write the code to display temperature on Sense Hat and change the background color to "green" when the temperature is between 22 and 27 celsius, "red" when it is above 27 celsius, and "blue" when it is under 22 celsius.
Note: Due to the Sense Hat being close to the Raspberry Pi's CPU, it's temperature gradually increases.
Exercise 6: Playing with the Joystick
Run the following sample code that controls one LED light using the joystick.
from sense_hat import SenseHat
sense = SenseHat() sense.clear()
R = (255,0,0)
x = 3
y = 3 sense.set_pixel(x,y,R)
while True:
for event in sense.stick.get_events(): if event.action == "pressed":
if event.direction == "up" and y > 0: sense.clear()
y = y - 1
Question 6: Please write the code to implement the following additional function:
The LED light can freely move around the 2D space;
When the LED light touches the boundary, it first reaches the boundary, lasts for 1 second, disappears, and resets to initial position (3,3).
Bonus Question (1 point on the final grade for both ELE 408 and ELE 409): Please write the code to implement the Snake Game and use the joystick to control the snake. You have two weeks to complete this task.