@aiconnection
import M5
from M5 import *
from machine import ADC, Pin
import time
ADC_PIN = 35
label_raw = None
def setup():
"""Initialize the system."""
global label_raw
try:
log("INFO", "Initializing system")
M5.begin()
Widgets.fillScreen(0xffffff) # White background
label_raw = Widgets.Label("Raw ADC:", 5, 40, 1.0, 0x000000, 0xffffff, Widgets.FONTS.DejaVu18)
adc = ADC(Pin(ADC_PIN))
adc.atten(ADC.ATTN_11DB) # Set ADC range to 0-3.3V
adc.width(ADC.WIDTH_12BIT) # Set 12-bit resolution
log("INFO", "System initialized successfully")
return adc
except Exception as e:
log("ERROR", f"Setup failed: {str(e)}")
time.sleep(5)
reset()
def log(level, message):
"""Log messages with a timestamp."""
ts = time.localtime()
print(f"[{level}][{ts[3]:02d}:{ts[4]:02d}:{ts[5]:02d}] {message}")
def loop(adc):
"""Main loop to read and display raw ADC values."""
try:
while True:
# Read raw ADC value
raw_value = adc.read()
# Update display
label_raw.setText(f"Raw ADC: {raw_value}")
# Log raw value
log("DEBUG", f"Raw ADC: {raw_value}")
# Wait before next reading
time.sleep(5)
except Exception as e:
log("ERROR", f"Loop error: {str(e)}")
time.sleep(5)
reset()
if name == 'main':
try:
# Initialize system and ADC
adc = setup()
# Start the main loop
loop(adc)
except Exception as e:
log("ERROR", f"Fatal error: {str(e)}")
time.sleep(5)
reset()