RTES / Mbed 2 deprecated mbed_pwmLib

Dependencies:   mbed

Revision:
6:625384a34dd5
Parent:
5:7b4575bf205e
Child:
7:3bed3e005200
diff -r 7b4575bf205e -r 625384a34dd5 main.cpp
--- a/main.cpp	Wed Oct 14 04:08:41 2015 +0000
+++ b/main.cpp	Wed Oct 14 22:30:39 2015 +0000
@@ -17,12 +17,14 @@
 InterruptIn event(p7);  //Interrupt for rising and falling edge detection from IR
 
 Timer timer;            //Timer to read falling and rising edge time
+Timer timer2;
 Serial pc(USBTX, USBRX);//Serial Communication
 
 void openGate();
 void closeGate();
 void rising();
 void falling();
+void start_pwm2(int x);
 
 int getState(float adc_val);
 
@@ -50,11 +52,11 @@
     pwm2.write(0);
     
     while(1){
-        printf("Frequency:-%f\n", frequency);
-        //char c = pc.getc();
-        //int val = c - 48;
-        float adc_val = button.read();
-        int val = getState(adc_val);
+        //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);
@@ -66,16 +68,16 @@
             //Move Up
             dc1 = 0;
             dc2 = 1;
-            pwm3.write(0.5);
+            pwm3.write(0.1);
             //wait(2);
             pc.printf("Floor Frequency value:%d\n",fvalues[val]);
             while(1){
                 if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
                     break;
                 }
-                else{
-                    printf("current freq: %f\n",frequency);
-                }
+                //else{
+                    //printf("current freq: %f\n",frequency);
+                //}
             }
             //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
             pwm3.write(0);
@@ -85,7 +87,7 @@
             //Move Down
             dc1 = 1;
             dc2 = 0;
-            pwm3.write(0.5);
+            pwm3.write(0.1);
             //wait(2);
             pc.printf("Floor Frequency value:-%d\n",fvalues[val]);
             //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
@@ -93,9 +95,9 @@
                 if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){ 
                     break;
                 }
-                else{
-                    printf("current freq: %f\n",frequency);
-                }
+                //else{
+                //    printf("current freq: %f\n",frequency);
+                //}
             }
             pwm3.write(0);
             openGate();
@@ -106,20 +108,21 @@
 
 void openGate(){
     pwm1.write(0.0375); // 3.75% duty cycle - Open the gate
-    pwm2.write(0.1125); // 11.25% duty cycle - Open the gate
-    wait(2);            // 2 sec delay
+    pwm2.write(0.1125);
+    //start_pwm2(1); // 11.25% duty cycle - Open the gate
+    wait(2);
     pwm1.write(0);      // Stop
-    pwm2.write(0);      // Stop
-    //wait(2);  
+    pwm2.write(0);
 }
 
 void closeGate(){
     pwm1.write(0.1125); // 11.25% duty cycle - Close the gate
-    pwm2.write(0.0375); // 3.75% duty cycle - Close the gate
-    wait(2);            // 2 sec delay
+    pwm2.write(0.0375);
+    //start_pwm2(0); // 3.75% duty cycle - Close the gate
+    wait(2);
     pwm1.write(0);      // Stop
-    pwm2.write(0);      // Stop  
-    //wait(2);
+    pwm2.write(0); 
+    //start_pwm2(0); // 3.75% duty cycle - Close the gate    
 }
 
 int getState(float adc_val){
@@ -154,4 +157,36 @@
         end = 0;
         flag = false;
     }
-}
\ No newline at end of file
+}
+
+void start_pwm2(int x){
+    
+    int duty1, duty2 = 0;
+    
+    //printf("Reached startpwm2\ 1 n");
+    if(x == 1){
+    duty1 = 250; // .25ms on period in us
+    duty2 = 19750; // 19.75ms off period
+    }
+    else if(x == 0){
+    //printf("Reached startpwm2 2 \n");
+    duty1 = 2250;  // 2.25ms on period
+    duty2 = 17750; //17.75ms off period
+    }
+    
+    timer2.start();
+    int start1 = timer2.read_ms();
+    printf("%d\n", start1);
+    
+    while((timer2.read_ms() - start1) < 2000){
+    //printf("entered");
+    pwm2 = 1;
+    wait_us(duty1);
+    pwm2 = 0;
+    wait_us(duty2);
+    }
+    
+    pwm2=0;
+        
+        
+}