I have a mini circuit board computer called a "Raspberry Pi". Connected to it is a 16x16 matrix grid RGB LED display called a Unicorn Hat HD" - I logged into "ChatGPT" and asked it:
"Using a Unicorn Hat HD, write me a python script which creates a one pixel wide dot, which constantly cycles through the colours of the rainbow, and leaves a fading trail behind it" - with some minor tweaking (asking it "please modify {XYZ} behaviour to do this instead", it produced this code (in "python" programming language) and the resultant effect as seen in the video.
Here is ChatGPT giving a code description: • ChatGPT analyses the code it wrote…
Here’s the Python it made initially:
import time
import random
import colorsys
import unicornhathd as unicorn
def rainbow_color(step):
Generate rainbow colors (ROYGBIV) based on the step
hue = 1.0 / 360 * step
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
return r, g, b
def animate_dot():
unicorn.rotation(0) # Set the display rotation (0, 90, 180, or 270 degrees)
width, height = unicorn.get_shape()
Create a buffer to store the previous positions of the dot for the fading trail
trail_length = 20
trail = []
step = 0
dot_x, dot_y = width // 2, height // 2 # Start the dot at the center of the display
while True:
Clear the display
unicorn.clear()
Calculate the rainbow color based on the current step
r, g, b = rainbow_color(step)
Add the current dot position to the trail buffer
trail.insert(0, (dot_x, dot_y))
Limit the trail length
trail = trail[:trail_length]
Draw the fading trail
for i, (x, y) in enumerate(trail):
fade = (trail_length - i) / trail_length # Calculate the fade factor
fade_r, fade_g, fade_b = int(r * fade), int(g * fade), int(b * fade)
unicorn.set_pixel(x, y, fade_r, fade_g, fade_b)
Draw the dot at the current position
unicorn.set_pixel(dot_x, dot_y, r, g, b)
Show the current frame
unicorn.show()
Delay to control the speed of animation
time.sleep(0.05) # Adjust the delay here to change the speed (e.g., 0.03 for even faster movement)
Increment the step for the next color in the rainbow cycle
step = (step + 5) % 360
Calculate the new position of the dot (less doubling back and larger steps)
while True:
new_x, new_y = dot_x + random.choice([-1, 0, 1]), dot_y + random.choice([-1, 0, 1])
if (new_x, new_y) != (dot_x, dot_y):
dot_x, dot_y = new_x, new_y
break
Ensure the dot stays within the valid range (0 to 15) for both the x and y coordinates
dot_x = max(0, min(width - 1, dot_x))
dot_y = max(0, min(height - 1, dot_y))
if _name_ == "__main__":
try:
animate_dot()
except KeyboardInterrupt:
unicorn.off() # Turn off the LEDs in case of Ctrl+C interrupt