You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
837 B
33 lines
837 B
4 years ago
|
#remember to enable SPI in raspi-config > interfaces before you run this the first time
|
||
|
|
||
|
#How to wire the MCP3008:
|
||
|
#https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
|
||
|
|
||
|
#gpiozero about MCP3008:
|
||
|
#https://gpiozero.readthedocs.io/en/stable/api_spi.html
|
||
|
|
||
|
#gpiozero tools to modify values:
|
||
|
#https://gpiozero.readthedocs.io/en/stable/_modules/gpiozero/tools.html
|
||
|
|
||
|
from gpiozero import MCP3008, PWMLED
|
||
|
from gpiozero.tools import clamped, smoothed, scaled
|
||
|
from time import sleep
|
||
|
|
||
|
ldr = MCP3008(channel=0, device=0)
|
||
|
|
||
|
led = PWMLED(17)
|
||
|
|
||
|
while True:
|
||
|
reading = smoothed(ldr,3) #smooth samples
|
||
|
|
||
|
value = scaled(reading, 0,1.0,0.01,0.5) #scale samples
|
||
|
|
||
|
brightness = clamped(value,0,1.0) #clamp samples
|
||
|
|
||
|
for i in brightness:
|
||
|
if i < 0.15:
|
||
|
i = 0
|
||
|
print(i)
|
||
|
led.value = i
|
||
|
|