no thread

Dependents:   200_yotuba_21_uiChange

Revision:
0:d0a252247fec
Child:
1:fe97d826508d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IRsensor.cpp	Mon Jun 24 07:29:14 2019 +0000
@@ -0,0 +1,56 @@
+#include "IRsensor.h"
+
+IRsensor::IRsensor(PinName pin):a_in(pin)
+{
+}
+
+float IRsensor::getInputvoltage(){
+    float voltage = a_in.read()*3.3;
+    return voltage;
+}
+
+float IRsensor::getDistance(int n){
+    float distance_sum=0;
+    for(int i=0;i<n;i++){
+        distance_sum += changeVtoD(getInputvoltage());
+    }  
+    return distance_sum / n;    
+}
+
+float IRsensor::changeVtoD(float voltage)
+{
+    float distance;
+    if (voltage > 2.285) {
+        distance = 0;
+    } else if (voltage > 1.645) {
+        distance = -7.8125 * voltage + 27.8515625;
+    } else if (voltage > 1.305) {
+        distance = -14.7058823529412 * voltage + 39.1911764705882;
+    } else if (voltage > 1.08) {
+        distance = -22.2222222222222 * voltage + 49;
+    } else if (voltage > 0.928) {
+        distance = -32.8947368421053 * voltage + 60.5263157894737;
+    } else if (voltage > 0.835) {
+        distance = -53.763440860215 * voltage + 79.8924731182795;
+    } else if (voltage > 0.737) {
+        distance = -51.0204081632653 * voltage + 77.6020408163266;
+    } else if (voltage > 0.673) {
+        distance = -78.1250000000001 * voltage + 97.5781250000001;
+    } else if (voltage > 0.608) {
+        distance = -76.9230769230768 * voltage + 96.7692307692307;
+    } else if (voltage > 0.562) {
+        distance = -108.695652173913 * voltage + 116.086956521739;
+    } else if (voltage > 0.515) {
+        distance = -106.382978723404 * voltage + 114.787234042553;
+    } else if (voltage > 0.474) {
+        distance = -121.951219512195 * voltage + 122.80487804878;
+    } else if (voltage > 0.447) {
+        distance = -185.185185185185 * voltage + 152.777777777778;
+    } else if (voltage > 0.432) {
+        distance = -333.333333333333 * voltage + 219;
+    } else {
+        distance = 9999;
+    }
+ 
+    return distance;
+}