Currently, our code only polls the color sensor once, then updates f, then we update image. But we want to do this continuously while our program is running to give our creature life. Since Wonder will very likely be moving while your code is running, the light conditions will change from one moment to the next. We will need to use a loop to accomplish this. Specifically, we will use something called a while loop. Very literally, this loop will keep running while a condition is true. So, we could write while f == (25, 25, 112) and our loop would end once we change f to the value derived from the color sensor. For now, we want this loop to run “forever” or, more realistically, until we choose terminate the loop by clicking “stop” on our program. Therefore, we could write something like while 1 == 1 or while 2 > 1 since both of these mathematical statements will always be true (at least until the San-Ti break our science). Usually, though, we just skip coming up with a true expression and just write while True.


#### 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:
    # 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

    # Arrange my colors on an 8x8 matrix.
    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]

    rgb = sense.color # get the colour from the sensor
    f = (rgb.red, rgb.green, rgb.blue)

    # Code telling the pixels to light up
    sense.set_pixels(image)

If you run this code in Trinket, then you will need to select a new color from the “Colour” selection panel, which is how you manipulate the light that the emulated sensor is sensing. Every time you change this color, the background color of the whale should update to this new color.

Here is the full program at this point:

#### 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

while True:
    # 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

    # Arrange my colors on an 8x8 matrix.
    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]

    rgb = sense.color # get the colour from the sensor
    f = (rgb.red, rgb.green, rgb.blue)

    # Code telling the pixels to light up
    sense.set_pixels(image)