# step-1: importing modules
from machine import Pin, I2C
import ssd1306
import time
import math
# step-2: setup I2C for OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# center (the Sun)
cx = oled_width // 2
cy = oled_height // 2
# planets: (name, orbit radius, speed, size)
planets = [
("Mercury", 8, 8, 1),
("Venus", 14, 6, 2),
("Earth", 20, 4, 2),
("Mars", 26, 3, 2),
("Jupiter", 34, 2, 3),
("Saturn", 42, 2, 3),
("Uranus", 50, 1, 2),
("Neptune", 58, 1, 2)
]
angles = [0] * len(planets) # starting angles
while True:
try:
oled.fill(0)
# Draw the Sun
oled.fill_rect(cx-2, cy-2, 4, 4, 1)
# Draw planets
for i, (name, radius, speed, size) in enumerate(planets):
# Calculate position
x = int(cx + radius * math.cos(math.radians(angles[i])))
y = int(cy + radius * math.sin(math.radians(angles[i])))
# Draw orbit (dotted circle)
for a in range(0, 360, 45):
px = int(cx + radius * math.cos(math.radians(a)))
py = int(cy + radius * math.sin(math.radians(a)))
oled.pixel(px, py, 1)
# Draw planet
oled.fill_rect(x, y, size, size, 1)
# Update angle (movement)
angles[i] = (angles[i] + speed) % 360
oled.show()
time.sleep(0.1)
except OSError as e:
print("Error Data")