with optic sensor

Dependencies:   Stepper mbed PinDetect

Revision:
5:1b451520ed5f
Parent:
4:82cd644fef51
Child:
6:7cd69300073f
--- a/main.cpp	Wed Sep 04 20:21:21 2019 +0000
+++ b/main.cpp	Wed Sep 04 20:54:45 2019 +0000
@@ -10,74 +10,63 @@
 DigitalOut myled3(LED3);
 DigitalOut myled4(LED4);
 int main(){
+     en = 1;
     int count = 0;
     int old_optic=0;
     int new_optic;
     optic.mode(PullDown);
     wait(0.01);
-//int positionnum = 0;
-//int slot = 0;
-//en = 1; // stepper motor off
+    int slot = 0;
+ 
     while(1) {
-        new_optic = optic;
-        //if (new_optic!= old_optic){ 
-        pc.printf("\new optic:%ld",new_optic);
-        wait(.5);
-        if ((new_optic==1) && (old_optic==0)){
-        myled4 = count & 0x01;
-        myled3 = (count & 0x02)>>1;
-        myled2 = (count & 0x04)>>2;
-        myled = (count & 0x08)>>3;
-        count++;
-        pc.printf("\ncount:%ld",count);
-        }
-          
-        old_optic = new_optic;
-        wait(.5);
-        pc.printf("\ncount:%ld",count);
-    
-   }
-}
-
-/*while(1)
-  {
+        mot.stop();
+        en = 1;
+        
         pc.printf("\n\n\nEnter desired position: ");
         pc.scanf("%d",&slot); 
-        
-        new_optic = optic;
-        while (slot!=positionnum){
+      
+
+        while (slot>count){
         
-        if (slot>positionnum){
         en = 0;
-        mot.setSpeed(500);
+        wait(.001);
+        mot.setSpeed(300);
+        wait(.001);
         mot.rotate(1);
-        }
+        new_optic = optic;
         
+        if ((new_optic==1) && (old_optic==0)){
+                myled4 = count & 0x01;
+                 myled3 = (count & 0x02)>>1;
+              myled2 = (count & 0x04)>>2;
+                    myled = (count & 0x08)>>3;
+                    count++;
+                    pc.printf("\ncount:%ld",count);
+                }
+        old_optic = new_optic;
         
-        //while(optic == 0)
-        //{
-        //wait(1);
-        //mot.goesTo(steps+25); //go to absolute step position
-        //while(!mot.stopped());
-        //steps = mot.getPosition();
-        //pc.printf("\nNot in Position", steps);
-        //}
-        pc.printf("\nIn Position");
-        en = 1; //disable the motor and driver
-        //if (tickcount<steps)
-        //{
-        //mot.goesTo(steps+25);
-        //while(!mot.stopped());
-        //steps = mot.getPosition();
-        //pc.printf("\nNot in Position", steps);
-        //}
-        //if (tickcount>steps)
-        //{
-        //mot.goesTo(steps-25);
-        //while(!mot.stopped());
-        //steps = mot.getPosition();
-        //pc.printf("\nNot in Position", steps);
-        //}
-    }
-}*/
-
+        }
+     
+        while (slot<count){
+            
+        en = 0;
+        wait(.001);
+        mot.setSpeed(300);
+        wait(.001);
+        mot.rotate(0);
+        new_optic = optic;
+        
+             if ((new_optic==1) && (old_optic==0)){
+                myled4 = count & 0x01;
+                myled3 = (count & 0x02)>>1;
+                myled2 = (count & 0x04)>>2;
+                myled = (count & 0x08)>>3;
+                count++;
+                pc.printf("\ncount:%ld",count);
+                }
+        
+        old_optic = new_optic;
+        }
+     
+   }
+}