from machine import ADC import time import GFX_LCD_0inch96 import seven_segment_ext as sseg import time adc_pin = ADC(28) level = 0.0 def measure(): global level start = time.ticks_us() count = 0 temp = 0.0 state = 0 fcounter = 0 hyst = 0.1 while time.ticks_diff(time.ticks_us(), start) < 1_000_000: adc_value = adc_pin.read_u16() # 16-bit read (0–65535) count += 1 temp += adc_value if state == 0: if adc_value > mean * (1 + hyst): state = 1 fcounter += 1 else: if adc_value < mean * (1 - hyst): state = 0 if count > 0: level = temp / count #print("count",count,"mean",mean,"fcounter",fcounter) return fcounter,level tft = GFX_LCD_0inch96.GFX_LCD_0inch96() tft.fill(0) def show(): FG = tft.GREEN FG2 = tft.RED f,level=measure() rpm=int(f/100*33) # 100Hz vom Plattenteller bei 33Hz tft.fill(0) sseg.draw_text(tft, str(rpm), 30, 10, w=25, h=50, color=FG) tft.text("RPM",100,20,FG); # signal level strlevel=str(int(level/65536*100)) tft.text(strlevel+"%",100,50,FG2); tft.display() import sys import select def loop_until_key(): print('Press any key to stop the loop...') counter=0 while True: show() if 0: tft.fill(0) a = adc_pin.read_u16() sseg.draw_text(tft, str(a), 30, 10, w=10, h=20, color=FG) tft.text(str(counter),100,20,FG) counter+=1 tft.display() time.sleep(1) if sys.stdin in select.select([sys.stdin], [], [], 0)[0]: key = sys.stdin.read(1) # Read 1 character print('Key pressed:', key) break loop_until_key()