Created
June 3, 2011 04:38
-
-
Save hgdeoro/1005880 to your computer and use it in GitHub Desktop.
Javascript to read temperature with LM335, using PyArduinoProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************************** | |
* First example. Testing how to use LM335 to read temperature, | |
* using PyArduinoProxy. | |
***********************************************************************/ | |
var value = proxy.analogRead(0); | |
logMessage("analogRead(): " + value); | |
var volt = (5.0/1024.0) * value; | |
logMessage("volt: +" + volt +"V"); | |
var kelvin = volt * 100; // 0.01V -> 1º K -> ºK = volt / 0.01 | |
logMessage("K: " + volt * 100); | |
// C = K-273 | |
logMessage("C1: " + (kelvin-273)); | |
logMessage("C2: " + ((((5.0/1024.0)*proxy.analogRead(0))*100)-273)); | |
/*********************************************************************** | |
* Second exampe. Using javascript functions. | |
***********************************************************************/ | |
function readTemp(analogPort) { | |
var value = proxy.analogRead(analogPort); | |
var volt = (5.0/1024.0) * value; | |
var kelvin = volt * 100; | |
return (kelvin - 273); // return temp. in celcius | |
} | |
logMessage("Temp. on A0: " + readTemp(0)); | |
logMessage("Temp. on A2: " + readTemp(2)); | |
/*********************************************************************** | |
* Use 'toFixed()' to get a better formatting | |
***********************************************************************/ | |
logMessage("Temp. on A0: " + readTemp(0).toFixed(1) + "º C"); | |
logMessage("Temp. on A2: " + readTemp(2).toFixed(1) + "º C"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment