| 
 | 1 | +from machine import Pin # Allows us to use "Pin" to use code to interface with the pins on our board  | 
 | 2 | + | 
 | 3 | +from machine import ADC # Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin  | 
 | 4 | + | 
 | 5 | +from machine import PWM # Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED  | 
 | 6 | + | 
 | 7 | +import time # Import the time module to use sleep for delays  | 
 | 8 | + | 
 | 9 | +pwmBlue  = PWM(Pin(32), freq=1000, duty_u16=0) # Create a PWM object on pin 28 with a frequency of 1000Hz and an initial "on time" of 0 (off)  | 
 | 10 | +pwmGreen = PWM(Pin(30), freq=1000, duty_u16=0) # Create a PWM object on pin 27 with a frequency of 1000Hz and an initial "on time" of 0 (off)  | 
 | 11 | +pwmRed   = PWM(Pin(28), freq=1000, duty_u16=0) # Create a PWM object on pin 26 with a frequency of 1000Hz and an initial "on time" of 0 (off)  | 
 | 12 | + | 
 | 13 | +# Let's create functions for various colors that we can call later  | 
 | 14 | + | 
 | 15 | +# Since our PWM "on time" or duty cycle is 16 bits, it is a value between 0 and 65535.  | 
 | 16 | +# It's useful to store a variable for maximum brightness so we can use percentages of it easily.  | 
 | 17 | +kMaximumBrightness = 65535  # Maximum brightness value for PWM  | 
 | 18 | + | 
 | 19 | +# These are "functions" that we can "call" to set the color of the LED.  | 
 | 20 | +# Notice the "def" keyword, which is used to define a function in Python.  | 
 | 21 | +# Now, we can call these later by just typing their names like `red()`, `green()`, etc.  | 
 | 22 | +# And all the code inside the function will run.  | 
 | 23 | + | 
 | 24 | +def red():  | 
 | 25 | +    # The "duty_u16" method sets the duty cycle or "on time" for the PWM pin.  | 
 | 26 | +    pwmRed.duty_u16(kMaximumBrightness)  # Set the red LED to full brightness  | 
 | 27 | +    pwmGreen.duty_u16(0)    # Turn off the green LED  | 
 | 28 | +    pwmBlue.duty_u16(0)     # Turn off the blue LED  | 
 | 29 | + | 
 | 30 | +def orange():  | 
 | 31 | +    pwmRed.duty_u16(kMaximumBrightness)  # Set the red LED to full brightness  | 
 | 32 | +    pwmGreen.duty_u16(int(kMaximumBrightness * 0.25))  # Set the green LED to quarter brightness (by multiplying by 0.5)  | 
 | 33 | +    pwmBlue.duty_u16(0)     # Turn off the blue LED  | 
 | 34 | + | 
 | 35 | +def yellow():  | 
 | 36 | +    pwmRed.duty_u16(kMaximumBrightness)  # Set the red LED to full brightness  | 
 | 37 | +    pwmGreen.duty_u16(kMaximumBrightness)  # Set the green LED to full brightness  | 
 | 38 | +    pwmBlue.duty_u16(0)     # Turn off the blue LED  | 
 | 39 | + | 
 | 40 | +def green():  | 
 | 41 | +    pwmRed.duty_u16(0)      # Turn off the red LED  | 
 | 42 | +    pwmGreen.duty_u16(kMaximumBrightness)  # Set the green LED to full brightness  | 
 | 43 | +    pwmBlue.duty_u16(0)     # Turn off the blue LED  | 
 | 44 | + | 
 | 45 | +def cyan():  | 
 | 46 | +    pwmRed.duty_u16(0)      # Turn off the red LED  | 
 | 47 | +    pwmGreen.duty_u16(kMaximumBrightness)  # Set the green LED to full brightness  | 
 | 48 | +    pwmBlue.duty_u16(kMaximumBrightness)  # Set the blue LED to full brightness  | 
 | 49 | + | 
 | 50 | +def blue():  | 
 | 51 | +    pwmRed.duty_u16(0)      # Turn off the red LED  | 
 | 52 | +    pwmGreen.duty_u16(0)    # Turn off the green LED  | 
 | 53 | +    pwmBlue.duty_u16(kMaximumBrightness)  # Set the blue LED to full brightness  | 
 | 54 | + | 
 | 55 | +def magenta():  | 
 | 56 | +    pwmRed.duty_u16(kMaximumBrightness)  # Set the red LED to full brightness  | 
 | 57 | +    pwmGreen.duty_u16(0)    # Turn off the green LED  | 
 | 58 | +    pwmBlue.duty_u16(kMaximumBrightness)  # Set the blue LED to full brightness  | 
 | 59 | + | 
 | 60 | +def turnOff():  | 
 | 61 | +    pwmRed.duty_u16(0)      # Turn off the red LED  | 
 | 62 | +    pwmGreen.duty_u16(0)    # Turn off the green LED  | 
 | 63 | +    pwmBlue.duty_u16(0)     # Turn off the blue LED  | 
 | 64 | + | 
 | 65 | + | 
 | 66 | +# Remember, we've already defined our RGB pins and functions above.   | 
 | 67 | +# Make sure you have run the cells above this one first!!!  | 
 | 68 | +photoresistor = ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0  | 
 | 69 | +potentiometer = ADC(Pin.board.A1) # Create an ADC variable for reading the potentiometer value from analog pin A1  | 
 | 70 | + | 
 | 71 | +# We'll set our photo-resistor threshold to a quarter of the maximum value of the ADC reading (65535)  | 
 | 72 | +threshold = 65535 / 4  | 
 | 73 | +potentiometerMax = 65535  # Maximum value for the potentiometer reading   | 
 | 74 | + | 
 | 75 | +# Infinite loop to continously read the photoresistor and potentiometer values  | 
 | 76 | +while True:  | 
 | 77 | +    photoValue = photoresistor.read_u16()  # Read the photoresistor value (0 to 65535)  | 
 | 78 | +    potPosition = potentiometer.read_u16()  # Read the potentiometer value (0 to 65535)  | 
 | 79 | +    # Print the values to the console  | 
 | 80 | +    print(f"Photoresistor Value: {photoValue: 5}, Potentiometer Value: {potPosition : 5}", end='\r') # Print our readings (don't mind the fanciness of this line it just makes the print format nicely)  | 
 | 81 | + | 
 | 82 | +    # Check if the photoresistor value is below the threshold  | 
 | 83 | +    if photoValue < threshold:  | 
 | 84 | +        # If the photoresistor value is below the threshold, set the LED color based on the potentiometer position  | 
 | 85 | +        # Let's split the range 0 - 65535 into 7 equal(ish) parts for different colors  | 
 | 86 | +        if potPosition > 0 and potPosition < 9362:   | 
 | 87 | +            red()  | 
 | 88 | +        if potPosition >= 9362 and potPosition < 18725:   | 
 | 89 | +            orange()  | 
 | 90 | +        if potPosition >= 18725 and potPosition < 28087:  | 
 | 91 | +            yellow()  | 
 | 92 | +        if potPosition >= 28087 and potPosition < 37450:  | 
 | 93 | +            green()  | 
 | 94 | +        if potPosition >= 37450 and potPosition < 46812:  | 
 | 95 | +            cyan()  | 
 | 96 | +        if potPosition >= 46812 and potPosition < 56175:  | 
 | 97 | +            blue()  | 
 | 98 | +        if potPosition >= 56175 and potPosition <= 65535:  | 
 | 99 | +            magenta()  | 
 | 100 | + | 
 | 101 | +    else: # Note that this "else" aligns with the photovalue < Threshold condition above.  | 
 | 102 | +        turnOff()  # Turn off the LED if the photoresistor value is above the threshold  | 
 | 103 | +      | 
 | 104 | +    time.sleep(0.250)  # Sleep for 0.25 seconds to avoid flooding the console with prints  | 
0 commit comments