RTES / Mbed 2 deprecated mbed_pwmLib

Dependencies:   mbed

Revision:
4:acb62dee5ba9
Parent:
3:66a52943c525
Child:
5:7b4575bf205e
--- a/main.cpp	Fri Oct 09 00:56:25 2015 +0000
+++ b/main.cpp	Sat Oct 10 01:11:59 2015 +0000
@@ -14,19 +14,31 @@
 PwmOut pwm3(p23);       //DC motor PWM channel
 
 AnalogIn button(p15);   //Analog input from the floor buttons
+InterruptIn event(p7);  //Interrupt for rising and falling edge detection from IR
 
-Serial pc(USBTX, USBRX);
+Timer timer;            //Timer to read falling and rising edge time
+Serial pc(USBTX, USBRX);//Serial Communication
 
 void openGate();
 void closeGate();
+void rising();
+void falling();
+
 int getState(float adc_val);
 
 int currentState = 1;
+int begin,end = 0;
+float frequency = 0;
+bool flag = false;
+int fvalues[] = {0,100,250,500,700,1000};
 
 int main() {
+    timer.start();
+    event.rise(&rising);
+    event.fall(&falling);
     //Setting dc1 to high and dc2 to low initially
-    dc1 = 1;
-    dc2 = 0;
+    dc1 = 0;
+    dc2 = 1;
     pwm3.period_ms(20);
     pwm3.write(0);
     
@@ -37,33 +49,57 @@
     pwm2.write(0);
     
     while(1){
-        //char c = pc.getc();
-        //int val = c - 48;
-        float adc_val = button.read();
-        int val = getState(adc_val);
-        //pc.printf("Floor-%d\n",val);
-        //wait(1);
+        printf("Frequency:-%f\n", frequency);
+        char c = pc.getc();
+        int val = c - 48;
+        //float adc_val = button.read();
+        //int val = getState(adc_val);
+        pc.printf("Floor-%d\n",val);
+        pc.printf("CurrentState-%d\n",currentState);
+        wait(1);
         if(val==currentState){
             pwm3.write(0);
         }
         else if(val > currentState){
+            closeGate(); //Close gate
             //Move Up
+            dc1 = 0;
+            dc2 = 1;
+            pwm3.write(0.5);
+            //wait(2);
+            pc.printf("Floor Frequency value:%d\n",fvalues[val]);
+            while(1){
+                if(((frequency > (fvalues[val] - 10)) && (frequency < (fvalues[val] + 10)))){
+                    break;
+                }
+                else{
+                    printf("current freq: %f\n",frequency);
+                }
+            }
+            //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
+            pwm3.write(0);
+            openGate();
+        }else{
+            closeGate(); //Close gate
+            //Move Down
             dc1 = 1;
             dc2 = 0;
-            pwm3.write(0.1);
-            wait(2);
+            pwm3.write(0.5);
+            //wait(2);
+            pc.printf("Floor Frequency value:-%d\n",fvalues[val]);
+            //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
+            while(1){
+                if(((frequency > (fvalues[val] - 10)) && (frequency < (fvalues[val] + 10)))){ 
+                    break;
+                }
+                else{
+                    printf("current freq: %f\n",frequency);
+                }
+            }
             pwm3.write(0);
-        }else{
-            //Move Down
-            dc1 = 0;
-            dc2 = 1;
-            pwm3.write(0.1);
-            wait(2);
-            pwm3.write(0);
+            openGate();
         }
         currentState = val;
-        //openGate();
-        //closeGate();
     }
 }
 
@@ -103,4 +139,18 @@
         state = 5;
     }
     return state;           
+}
+
+void rising(){
+    begin = timer.read_us();
+    flag = true;
+}
+void falling(){
+    if(flag == true){
+        end = timer.read_us();
+        frequency = 500000/(end-begin);
+        begin = 0;
+        end = 0;
+        flag = false;
+    }
 }
\ No newline at end of file