Added convert functions
This commit is contained in:
parent
4730cd6923
commit
a150509598
1 changed files with 18 additions and 1 deletions
19
raspicar.py
19
raspicar.py
|
@ -59,6 +59,16 @@ def init():
|
|||
pass
|
||||
|
||||
|
||||
# Convert value to volt
|
||||
def convertToVoltage(value):
|
||||
return value * ( 3.3 / 1024 ) * 5
|
||||
|
||||
|
||||
# Convert volt to celcius value
|
||||
def convertToCelcius(value):
|
||||
return ( ( 3300.0 / 1024.0 ) - 100.0 ) / 10.0 - 40.0
|
||||
|
||||
|
||||
#read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
|
||||
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
|
||||
if ((adcnum > 7) or (adcnum < 0)):
|
||||
|
@ -110,7 +120,14 @@ def main():
|
|||
else:
|
||||
# Get current sensor value
|
||||
ad_value = readadc(CHANNEL, CLK, MOSI, MISO, CS)
|
||||
value = ad_value*(3.3/1024)*5
|
||||
value = 0
|
||||
|
||||
# Convert value in readable Volt or Celcius
|
||||
if ( CHANNELL == 0 ):
|
||||
value = convertToVoltage(ad_value)
|
||||
else:
|
||||
value = convertToCelcius(ad_value)
|
||||
|
||||
# Format value with 2 decimals
|
||||
printableValue = f"{value:.02f}"
|
||||
|
||||
|
|
Loading…
Reference in a new issue