Description: controlling the DC motor speed based on the temperature get from LM35 sensor.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Victordang
Date:
Mon Apr 02 05:46:58 2018 +0000
Commit message:
LM35 and DC Motor

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 7243db10fb59 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Apr 02 05:46:58 2018 +0000
@@ -0,0 +1,70 @@
+#include "mbed.h"
+#include "string"
+
+PwmOut motor(p21);
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); //enable serial port which links to USB 
+AnalogIn sensor(p15); //LM35 sensor
+float current_pulse = 0;
+float offset = 0;
+Ticker  in;
+void motor_speed(float offset)
+{
+    //current_pulse += offset;
+    motor = offset;
+} 
+float getTemperature(){
+        int i,N=400;
+        float tempC, avg, a[N]; 
+        myled = 1;
+        avg = 0;
+        
+        for(i=0;i<N;i++)
+        {
+            a[i]=sensor.read();
+            
+            wait(.01);
+        }
+        for(i=0;i<N;i++)
+        {
+            
+            avg=avg+(a[i]/10*1000); 
+            
+        }
+        tempC = (avg/N*10);
+        pc.printf("\n\rTemperature is %4.1f C, avg = %4.5f  \r\n",tempC, avg/N);
+
+    return tempC;
+}
+int main() {
+    
+    motor.period(0.01);
+    motor = 0;
+    float tempC, threshold = 15;
+    string err_str;
+    pc.printf("\r\nLM35 Temperature Program");
+    pc.printf("\r\n******************\r\n");
+    wait(1); // wait 1 second for device stable status
+    while (1) {
+        
+        tempC = getTemperature();
+        
+            
+            if(tempC >= threshold)
+            {
+                offset = tempC - threshold + 0.5;
+                motor_speed(offset);                  
+            }
+            else
+            {
+                offset = 0;
+                motor_speed(offset);
+            }
+            pc.printf("\n\r pulse width %f, offest = %f \r\n",motor.read(), offset);
+             
+        myled = 0;
+        wait(1);
+    }
+    
+}
+
diff -r 000000000000 -r 7243db10fb59 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Apr 02 05:46:58 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/994bdf8177cb
\ No newline at end of file