import _thread
import time
from machine import Pin
from machine import ADC
def adc_thread(delay):
global buttonFlag
adc=ADC(Pin(34))
while True:
if buttonFlag==True:
raw=adc.read()
volts=3.3*raw/4095
print(volts)
time.sleep_ms(delay)
def button_thread(delay,T):
global buttonFlag
LED = 2
BUTTON=4
led_pin = Pin(LED, Pin.OUT)
button_pin = Pin(BUTTON, Pin.IN)
state=0
N=int(T/delay)
counter=0;
while True:
counter=(counter+1)%N;
if counter==0:
led_pin.value(not led_pin.value())
# FSM impmementation
if state==0:
if button_pin.value()==0:
print('Button pressed')
state=1
elif state==1:
if button_pin.value()==1:
state=0
print('Button released')
buttonFlag=not(buttonFlag)
time.sleep_ms(delay)
buttonFlag=True
_thread.start_new_thread(button_thread,(10,500))
_thread.start_new_thread(adc_thread,(1000,))