import spidev
import time

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000

def read_adc(channel):
    assert 0 <= channel <= 7, "ADC channel must be between 0 and 7"
    adc = spi.xfer2([1, (8 + channel) << 4, 0])
    data = ((adc[1] & 3) << 8) | adc[2]
    return data

try:
    while True:
        value = read_adc(0)  # Read potentiometer value from channel 0
        print(f"Potentiometer Value: {value}")
        time.sleep(0.5)
except KeyboardInterrupt:
    print("Measurement stopped by user")
    spi.close()
