Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:83dfc83e48bf
diff -r 000000000000 -r 83dfc83e48bf main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Nov 06 07:31:40 2013 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX);
+AnalogIn tempPin(p15);
+
+float tempTotal;
+uint32_t tempDiv;
+
+#define MAX_TEMP_AVG 10
+
+void sampleTemperature();
+void sampleTemperatures();
+void resetTemperatureSamples();
+float getAvgTemperature();
+
+void sampleTemperature()
+{
+ tempTotal += tempPin; //we use 3.3V from VOUT
+ tempDiv++;
+}
+
+void sampleTemperatures()
+{
+ for(int i=0;i<MAX_TEMP_AVG;i++) {
+ sampleTemperature();
+ wait(0.1);
+ }
+}
+
+void resetTemperatureSamples()
+{
+ tempTotal = 0.0;
+ tempDiv = 0;
+}
+float getAvgTemperature()
+{
+ /* Temperature calculations is (VOut - V0)/Tc
+ * VOut = V out from sensor
+ * V0 = Voltage on 0 Celcius, 500mv for MCP9700
+ * Tc = Temperatur constant, 10mV/C
+ */
+ float tempAvg = (((tempTotal*3300)/(float)tempDiv)-500.0)/10.0;
+ return tempAvg;
+}
+
+int main() {
+tempDiv = 0;
+ pc.printf("MBED ready..\r\n");
+ sampleTemperatures();
+ pc.printf("Temp: %.2f.\r\n",getAvgTemperature());
+ while(1) {
+ sampleTemperature();
+
+ myled = 1;
+ wait(0.5);
+ myled = 0;
+ wait(0.5);
+
+ if (tempDiv > MAX_TEMP_AVG) {
+ pc.printf("Temp: %.2f.\r\n",getAvgTemperature());
+ resetTemperatureSamples();
+ }
+ }
+}