ok

Dependencies:   mbed

Revision:
0:adbbe24964e9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 27 23:08:26 2017 +0000
@@ -0,0 +1,113 @@
+#include "mbed.h"
+
+
+PwmOut PWM0(p21); //PWM out
+ AnalogIn   ain0(p19); //Analog input A0
+ AnalogIn   ain1(p18); //Analog input A0
+ DigitalOut ledasim(LED1);
+ DigitalOut ledasim2(LED2);
+ Serial pc(USBTX, USBRX); // tx, rx
+ Serial uart(p28, p27);
+ Timer t; //initialize timer
+
+ int main() {
+    float test=0;
+    float test2 = 0;
+    float pretest = 0;
+    char str[125]="";
+    float receive = 0;
+    int rflag = 0;
+    float rpm; //declare rpm variable
+    
+    PWM0.period(0.0025f);      // 0.0025 second period
+    PWM0.write(0.8f);      // 80% duty cycle, relative to period
+    ledasim=!ledasim;
+    
+    wait(30);
+    pretest = (ain0*3.3)/0.178;
+    while(1){
+    wait(0.25);
+    test=(ain0*3.3)/0.178;
+    wait(0.25);
+    test2 = (ain0*3.3)/0.178;
+    if((test <5.3 || test >4.7) && (test2 <5.3 || test2 > 4.7)){
+       test=test;
+        
+       }
+         if ((test >5.3 || test <4.7) && (test2 <5.3 || test2 >4.7)) {
+            test = test2;
+          
+           }
+            if ((test2 > 5.3 || test2 < 4.7) && (test < 5.3 || test > 4.7)){
+               test = test;
+                
+                }
+                if((test >5.3 || test <4.7) && (test2 >5.3 || test2 < 4.7)){
+                    test = test;
+                    }
+                    
+    if((test <5.3 || test >4.7) && (pretest <5.3 || pretest > 4.7)){
+    test=test;
+    pretest = test;
+   
+        }
+        if ((test >5.3 || test <4.7) && (pretest <5.3 || pretest >4.7)) {
+           test = pretest;
+            
+            }
+            if ((pretest > 5.3 || pretest < 4.7) && (test < 5.3 || test > 4.7)){
+                test = test;
+                
+               pretest = test;
+                }
+                 if((test >5.3 || test <4.7) && (pretest >5.3 || pretest < 4.7)){
+                    test = test;
+                    pretest = test;
+                    }
+                    
+    if (ain1 <0.9){  //senses beam crossing
+    t.start(); //start timer}
+    while(ain1<0.9){}
+    while(ain1>0.9){}
+    t.stop(); //stop timer
+    rpm = (1/t.read())*60;  //obtain value of rpm
+    
+    t = 0;
+   }
+ 
+    //pc.printf("%f\n\r",test);
+    pc.printf("%f\n\r",rpm);
+    if(test > 5.3 || test < 4.7) {
+    PWM0.period(0.0f);      // 0second period
+    PWM0.write(0.0f);
+   
+    }
+            if(uart.readable() && rflag == 0) {
+            char message[125];
+            uart.scanf("%s",message);
+            receive = atof(message);
+        }
+          
+        if(receive == 1 && rflag == 0){
+          sprintf(str, "%f", test);
+          if(uart.writeable())
+           {
+           uart.printf("%s\n",str);
+           rflag = rflag + 1;
+           rflag = 0;
+           }
+        }
+      }
+    
+     
+     
+  }
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
\ No newline at end of file