import time
from machine import I2C,SoftI2C,Pin,RTC
time.sleep(0.1) # Wait for USB to become ready
#print("Hello, Pi Pico!")
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Function to convert a number to binary and light up LEDs accordingly
def light_leds(number):
binary = bin(number)[2:] # Convert number to binary string
length = len(binary)
for i in range(8):
if i < length and binary[length - 1 - i] == '1':
# Light up LED corresponding to 1 bit
# Assuming LED 0 is the leftmost LED
# You may need to adjust the pin numbers based on your hardware setup
led_pin[i].on()
else:
led_pin[i].off()
# Set up LED pins
# Replace these pin numbers with the actual pins connected to your LEDs
led_pin = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT), Pin(3, Pin.OUT),
Pin(7, Pin.OUT), Pin(9, Pin.OUT), Pin(10, Pin.OUT), Pin(11, Pin.OUT)]
# Main loop to scan through numbers and detect primes
for num in range(2, 62):
if is_prime(num):
light_leds(num)
time.sleep(1) # Optional delay to see the LEDs lighting up
#make sure to del comments