In: Computer Science
Write a program for Sense HAT (or the emulator) to map X, Y, and
Z acceleration (or orientation) values into R, G, and B color
values on the LED matrix. That way, when you tilt the Sense HAT,
the LED matrix will display different colors to simulate a paint
mixer.
When you write your program, please consider using delay to avoid
the problem of changing color too fast to be perceived. Also, you
want to make sure that you map the X, Y, and Z values appropriately
to prevent the problem of color values changing very little.
Answer : Given data
Program for Sense HAT is
#Python implementation of SenseHat
from sense_hat import SenseHat
sense = SenseHat()
#set the values of the colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
while True:
#acceleration for each axis (x, y, and z) is extracted and rounded off to nearest integer
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x=round(x, 0)
y=round(y, 0)
z=round(z, 0)
print("x={0}, y={1}, z={2}".format(x, y, z))
# example using format (x, y,z, pixel)
sense.set_pixel(1, 0,0, red)
sense.set_pixel(0, 1, 0,green)
sense.set_pixel(0, 0,1, blue)
Snippet of code is attached below:
from sense_hat import SenseHat
sense = SenseHat()
#set the values of the colors
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
while True:
#acceleration for each axis (x, y, and z) is extracted and rounded off to nearest integer
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x=round(x, 0)
y=round(y, 0)
z=round(z, 0)
print("x={0}, y={1}, z={2}".format(x, y, z))
# example using format (x, y,z, pixel)
sense.set_pixel(1, 0,0, red)
sense.set_pixel(0, 1, 0,green)
sense.set_pixel(0, 0,1, blue)
_____________THE END_______________