Created
April 4, 2023 00:01
-
-
Save caseyanderson/29006226abda4101fbef3df046219520 to your computer and use it in GitHub Desktop.
analogDigital.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
analogDigital.py | |
''' | |
from machine import ADC, Pin, PWM | |
from time import sleep_ms | |
led1Pin = Pin(27) | |
led2Pin = Pin(33, Pin.OUT) | |
adcPin = Pin(34) | |
adc = ADC(adcPin) | |
adc.atten(ADC.ATTN_11DB) | |
adc.width(ADC.WIDTH_10BIT) # 0 - 1023 | |
pwm = PWM(led1Pin, freq=20000, duty=0) | |
button = Pin(12, Pin.IN, Pin.PULL_UP) | |
prev_val = button.value() | |
limit = 2 | |
count = 0 | |
while True: | |
if not button.value() and button.value() != prev_val: | |
count += 1 | |
msg = ' '.join(['Button pressed', str(count), 'times!']) | |
print(msg) | |
if count == limit: | |
print('limit reached!') | |
pwm.duty(0) | |
count = 0 | |
if count == 0: | |
print("ready!") | |
led2Pin.value(1) | |
elif count == 1: | |
led2Pin.value(0) | |
sensor_val = adc.read() | |
print(sensor_val) | |
if sensor_val >= 512: | |
print('LED ON!') | |
pwm.duty(sensor_val) | |
else: | |
print('LED OFF!') | |
pwm.duty(0) | |
prev_val = button.value() | |
sleep_ms(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment