this is the error I am gtting.
webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/temp4 $ sudo python temp4.py
86/1023 => 0.277 V => 27.7 °C
86/1023 => 0.277 V => 81.9 °F
Traceback (most recent call last):
File "temp4.py", line 55, in <module>
display_temp()
File "temp4.py", line 25, in display_temp
temp = int(temperaturef [0]) # F
TypeError: 'float' object has no attribute '__getitem__'
webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/temp4 $
this is my code,
#!/usr/bin/python
# -*- coding: utf-8 -*-
# mcp3008_lm35.py - read an LM35 on CH0 of an MCP3008 on a Raspberry Pi
# mostly nicked from
# http://jeremyblythe.blogspot.ca/2012/09/raspberry-pi-hardware-spi-analog-inputs.html
import spidev
import time
from Adafruit_7Segment import SevenSegment
segment = SevenSegment(address=0x70)
spi = spidev.SpiDev()
spi.open(0, 0)
def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
def display_temp():
segment.setColon(False)
temp = int(temperaturef [0]) # F
# temp = int(read_temp()[0]) # C
sign = (temp < 0)
temp = abs(temp)
digit_1 = temp % 10
temp = temp / 10
digit_2 = temp % 10
temp = temp / 10
digit_3 = temp % 10
if sign :
segment.writeDigitRaw(0, 0x40) # - sign
if digit_3 > 0 :
segment.writeDigit(0, digit_3) # Hundreds
else:
segment.writeDigitRaw(0, 0)
if digit_2 > 0 :
segment.writeDigit(1, digit_2) # Tens
else:
segment.writeDigitRaw(1, 0)
segment.writeDigit(3, digit_1) # Ones
segment.writeDigitRaw(4, 0x71) #F # Temp units letter
#segment.writeDigitRaw(4, 0x39) #C
while True:
value = readadc(0)
volts = (value * 3.3) / 1024
temperaturec = volts / (10.0 / 1000)
temperaturef = ( temperaturec * 9.0 / 5.0 ) + 32
print ("%4d/1023 => %5.3f V => %4.1f °C" % (value, volts,temperaturec))
print ("%4d/1023 => %5.3f V => %4.1f °F" % (value, volts,temperaturef))
display_temp()
time.sleep(0.5)