You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# You should see the LED blink faster or slower in accordance with your potentiometer. The delay between each flash will change based on the position of the knob.
5
+
#
6
+
# For more information, check out the guide here: TODO: guide URL
7
+
1
8
frommachineimportPin# Allows us to use "Pin" to use code to interface with the pins on our board
2
9
3
10
frommachineimportADC# Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
4
11
5
12
led_pin=Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
6
13
potentiometer=ADC(Pin.board.A0) # Create an ADC variable for reading the potentiometer value from analog pin A0
7
14
8
-
# Try moving the potentiometer and re-running this cell and you should see this value change.
9
-
print(potentiometer.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
15
+
# Try moving the potentiometer and running the below line repeatedly and you should see the value change
16
+
# print(potentiometer.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
10
17
11
18
# Now, let's blink the LED with different speeds based on the potentiometer input
12
19
importtime# Allows us to use "time.sleep()" to delay for a certain number of seconds
# The program stores the light level in a variable. Then, using an if/else statement, the program checks to see what it should do with the LED.
5
+
# If the variable is above the threshold (it’s bright), turn the LED off. If the variable is below the threshold (it’s dark), turn the LED on.
6
+
# You now have just built your own night-light!
7
+
#
8
+
# For more information, check out the guide here: TODO: guide URL
9
+
1
10
frommachineimportPin# Allows us to use "Pin" to use code to interface with the pins on our board
2
11
3
12
frommachineimportADC# Allows us to use "ADC" (analog-to-digital conversion) to read from our analog pin
4
13
5
14
led_pin=Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)
6
15
photoresistor=ADC(Pin.board.A0) # Create an ADC variable for reading the photoresistor value from analog pin A0
7
16
8
-
# Try moving the potentiometer and re-running this cell and you should see this value change.
9
-
print(photoresistor.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
17
+
# Try moving the potentiometer and running the below line repeatedly and you should see the value change
18
+
# print(photoresistor.read_u16()) # Use the "read_u16" method to read the value of our potentiometer.
10
19
11
20
# Now, let's turn the LED on and off based on the photoresistor value.
12
21
importtime# Allows us to use "time.sleep()" to delay for a certain number of seconds
13
22
14
-
# We'll set our threshold to half of the maximum value of the ADC reading (65535)
15
-
threshold=65535/2
23
+
# We'll set our threshold to a bit below the top of the range of the photoresistor (65535)
24
+
# you can change this as you want to match the lighting in your room
25
+
threshold=55000
16
26
17
27
# Infinite loop so this cell keeps running until we stop it.
18
28
whileTrue:
19
29
photoValue=photoresistor.read_u16() # Get the new photoresistor value (0 - 65535)
20
30
21
31
print(f"Photoresistor Value: {photoValue : 5}", end='\r') # Print our Photoresistor reading (don't mind the fanciness of this line it just makes the print format nicely)
22
32
23
-
# Turn on the LED but only if the photoresistor value is above the threshold
24
-
ifphotoValue>threshold:
33
+
# Turn on the LED but only if the photoresistor value is below the threshold
34
+
ifphotoValue<threshold:
25
35
led_pin.high()
26
36
else:
27
37
led_pin.low()
28
38
29
39
# A short delay to make the printout easier to read
Copy file name to clipboardExpand all lines: source_only_examples/project2_c.py
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,12 @@
1
+
# MicroPython SparkFun Inventor's Kit Circuit 2C: Simon Says Game
2
+
#
3
+
# What you should see:
4
+
# The circuit will flash all of the LEDs and play a melody. After a few seconds, it will flash the first light in the pattern.
5
+
# If you repeat the pattern correctly by pressing the corresponding colored button, then the game will move to the next round and add another color to the pattern sequence.
6
+
# If you make a mistake, the loss melody will play. If you get to round 10, the win melody will play. Press any button to start a new game.
7
+
#
8
+
# For more information, check out the guide here: TODO: guide URL
9
+
1
10
frommachineimportPin# Allows us to use "Pin" to use code to interface with the pins on our board
2
11
frommachineimportPWM# Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
# Move your hand or a large, flat object closer and farther away from the distance sensor. As the object approaches, the light will change from green to yellow to red.
5
+
#
6
+
# For more information, check out the guide here: TODO: guide URL
7
+
1
8
frommachineimportPin# Allows us to use "Pin" to use code to interface with the pins on our board
2
9
frommachineimportPWM# Allows us to use "PWM" (pulse-width modulation) to control the brightness of our LED
0 commit comments