@@ -0,0 +1,2236 @@
+#!/usr/bin/python%0A%0Aimport spidev%0Aimport time%0Aimport os%0A%0Aclass MCP3008():%0A%0A    def __init__():%0A        # Open SPI bus%0A        spi = spidev.SpiDev()%0A        spi.open(0, 0)%0A%0A    # Function to read SPI data from MCP3008 chip%0A    # Channel must be an integer 0-7%0A    def ReadChannel(channel):%0A        adc = spi.xfer2(%5B1, (8 + channel) %3C%3C 4, 0%5D)%0A        data = ((adc%5B1%5D & 3) %3C%3C 8) + adc%5B2%5D%0A        return data%0A%0A    # Function to convert data to voltage level,%0A    # rounded to specified number of decimal places.%0A%0A%0A    def ConvertVolts(data, places):%0A        volts = (data * 3.3) / float(1023)%0A        volts = round(volts, places)%0A        return volts%0A%0A    # Function to calculate temperature from%0A    # TMP36 data, rounded to specified%0A    # number of decimal places.%0A%0A%0A    def ConvertTemp(data, places):%0A%0A      # ADC Value%0A      # (approx)  Temp  Volts%0A      #    0      -50    0.00%0A      #   78      -25    0.25%0A      #  155        0    0.50%0A      #  233       25    0.75%0A      #  310       50    1.00%0A      #  465      100    1.50%0A      #  775      200    2.50%0A      # 1023      280    3.30%0A%0A        temp = ((data * 330) / float(1023)) - 50%0A        temp = round(temp, places)%0A        return temp%0A%0A    # Define sensor channels%0A    light_channel = 5%0A    temp_channel = 6%0A%0A    # Define delay between readings%0A    delay = 1%0A%0A    while True:%0A        channels = list(range(6))%0A        # Read the light sensor data%0A        light_levels = %5B0 for i in range(6)%5D%0A        light_volts = %5B0 for i in range(6)%5D%0A        #light_level = ReadChannel(light_channel)%0A        #light_volts = ConvertVolts(light_level, 2)%0A        bin = %5B0 for i in range(6)%5D%0A        # Read the temperature sensor data%0A        #temp_level = ReadChannel(temp_channel)%0A        #temp_volts = ConvertVolts(temp_level, 2)%0A        #temp = ConvertTemp(temp_level, 2)%0A        for i, nb in enumerate(channels):%0A            light_levels%5Bi%5D = ReadChannel(nb)%0A            light_volts%5Bi%5D = ConvertVolts(light_levels%5Bi%5D,2)%0A            if light_volts%5Bi%5D %3E 2:%0A                bin%5Bi%5D = 1%0A    # Print out results%0A    #    print(%22Light: (%7B%7DV)%22.format(light_volts))%0A    #    print(%22Temp : (%7B%7DV)%22.format(temp_volts))%0A        print(*bin)%0A        # Wait before repeating loop%0A        time.sleep(delay)%0A

add de MCP3008 class