足上げセンサ八個版仮

Dependencies:   mbed

Fork of 4LegUpDown_8sense by Bチーム

Revision:
6:6664c6aa67d3
Parent:
5:aba9f5e97e03
Child:
7:583ad8798bf1
--- a/main.cpp	Thu Sep 11 02:42:46 2014 +0000
+++ b/main.cpp	Thu Sep 11 22:00:53 2014 +0000
@@ -1,15 +1,15 @@
 #include "mbed.h"
 
 #define MOTOR_NUM 8
-#define MOVE_TIME 0.060
-#define MOVE_DOWN_TIME 0.075
-#define TEMPRA_TIME 1.0 
+#define MOVE_TIME 0.080
+#define MOVE_DOWN_TIME 0.090
+#define TEMPRA_TIME 5.0
 #define PWM_LEVEL 1.0
 
 Serial pc(USBTX,USBRX);
 
 InterruptIn Photo[8] = {p5,p6,p7,p8,p9,p10,p11,p12};
-InterruptIn Sw(p30);
+//InterruptIn Sw(NC);
 
 
 DigitalOut Led[4] = {LED1, LED2, LED3, LED4 };
@@ -18,13 +18,15 @@
 PwmOut Pwm[4] = {p21, p22, p23, p24 }; 
 
 Timeout StopTimer[8];
+Timeout StopTempra;
 //Timeout StopTempra;
 
 
 volatile bool MotorState[4] = {0},Flag[4] = {0},FlagAdd[4] = {0},And[4] = {0};
 volatile uint8_t Way = 0,UpCt = 0;
+volatile bool TempraFlag = 0;
 
-/*motorstate 上がっているか下がっている
+/*motorstate 上がっているか下がっているか
 
 
 */
@@ -76,14 +78,13 @@
     Motor = Motor & 0xFD;
 
 }
-
 void MotorStop7(){
     
     Motor = Motor & 0xFE;
     
 }
 void TempraStop(){
-    Motor = Tempra & 0x02;   
+    Tempra = 0x00;   
     
 }
 void FallPhoto0(){//zero
@@ -102,7 +103,7 @@
         }else{
             Flag[0] = 1;
         }
-    }else if(Way == 2){
+    }/*else if(Way == 2){
         
         if(Flag[0]&&!FlagAdd[0]){
             FlagAdd[0] = 1;
@@ -115,16 +116,16 @@
             And[0] = 1;
             
         }
-    }
+    }*/
 }
 
 void FallPhoto1(){//one
     //Wayの決定
-    if(Way ==0){
+    /*if(Way ==0){
         Way = 2;
-    }
+    }*/
     
-    if(Way == 2){
+    /*if(Way == 2){
         if(!MotorState[0]){
             
             StopTimer[0].attach(&MotorStop0,MOVE_TIME);
@@ -134,7 +135,8 @@
         }else{
             Flag[0] = 1;
         }
-    }else if(Way == 1){
+    }else */
+    if(Way == 1){
         
         if(Flag[0]&&!FlagAdd[0]){
             FlagAdd[0] = 1;
@@ -143,6 +145,7 @@
             Motor = Motor | 0x40;//0x40 = 01000000
             MotorState[0] = 0;
             Flag[0] = 0;
+            FlagAdd[0] = 0;
             Led[0] = 0;
             And[0] = 1;
             
@@ -163,7 +166,7 @@
         }else{
             Flag[1] = 1;
         }
-    }else if(Way == 2){
+    }/*else if(Way == 2){
         
         if(Flag[1]&&!FlagAdd[1]){
             FlagAdd[1] = 1;
@@ -175,13 +178,13 @@
             Led[1] = 0;
             And[1] = 1;
         }
-   } 
+   } */
 
 }
 void FallPhoto3(){ //three
     //Way
     
-    if(Way == 2){
+    /*if(Way == 2){
         if(!MotorState[1]){
             
             StopTimer[2].attach(&MotorStop2,MOVE_TIME);
@@ -191,7 +194,8 @@
         }else{
             Flag[1] = 1;
         }
-    }else if(Way == 1){
+    }else */
+    if(Way == 1){
         
         if(Flag[1]&&!FlagAdd[1]){
             FlagAdd[1] = 1;
@@ -200,6 +204,7 @@
             Motor = Motor | 0x04;
             MotorState[1] = 0;
             Flag[1] = 0;
+            FlagAdd[1] = 0;
             Led[1] = 0;
             And[1] = 1;
             
@@ -220,7 +225,7 @@
         }else{
             Flag[2] = 1;
         }
-    }else if(Way == 2){
+    }/*else if(Way == 2){
         
         if(Flag[2]&&!FlagAdd[2]){
             FlagAdd[2] = 1;
@@ -233,15 +238,15 @@
             And[2] = 1;
             
         }
-   } 
+   } */
 
 }
 void FallPhoto5(){//five
-    if(Way ==0){
+    /*if(Way ==0){
         Way = 2;
-    }
+    }*/
     
-    if(Way == 2){
+    /*if(Way == 2){
         if(!MotorState[2]){
             StopTimer[4].attach(&MotorStop4,MOVE_TIME);
             Motor = Motor | 0x20;//00100000  
@@ -250,7 +255,8 @@
         }else{
             Flag[2] = 1;
         }
-    }else if(Way == 1){
+    }else */
+    if(Way == 1){
         
         if(Flag[2]&&!FlagAdd[2]){
             FlagAdd[2] = 1;
@@ -259,6 +265,7 @@
             Motor = Motor | 0x10;
             MotorState[2] = 0;
             Flag[2] = 0;
+            FlagAdd[2] = 0;
             Led[2] = 0;
             And[2] = 1;
         }
@@ -276,7 +283,7 @@
         }else{
             Flag[3] = 1;
         }
-    }else if(Way == 2){
+    }/*else if(Way == 2){
         
         if(Flag[3]&&!FlagAdd[3]){
             FlagAdd[3] = 1;
@@ -288,13 +295,13 @@
             Led[3] = 0;
             And[3] = 1;
         }
-   }             
+   }   */          
 }
 void FallPhoto7(){//seven
     
     
     
-    if(Way == 2){
+    /*if(Way == 2){
         if(!MotorState[3]){
             StopTimer[6].attach(&MotorStop6,MOVE_TIME);
             Motor = Motor | 0x02;  
@@ -303,7 +310,8 @@
         }else{
             Flag[3] = 1;
         }
-    }else if(Way == 1){
+    }else */
+    if(Way == 1){
         
         if(Flag[3]&&!FlagAdd[3]){
             FlagAdd[3] = 1;
@@ -312,6 +320,7 @@
             Motor = Motor | 0x01;
             MotorState[3] = 0;
             Flag[3] = 0;
+            FlagAdd[3] = 0;
             Led[3] = 0;
             And[3] = 1;
         }
@@ -351,33 +360,36 @@
 }
 
 void NumCount(){//足を上げた回数を記録
-
-    bool AndCk = And[0]&And[1]&And[2]&And[3];
-    if(Way == 1){
-        Tempra = 0x0A;
-    }
-    if(AndCk){
-        UpCt ++;
-        And[0] = And[1] = And[2] = And[3] = 0 ;
-        if(UpCt > 4){
-            Tempra = 0;
-            UpCt = 0;
-            Way = 0;
-            
-        }
+    UpCt ++;
+    And[0] = 0 ;
+    And[1] = 0 ;
+    And[2] = 0 ;
+    And[3] = 0 ;
+    TempraFlag = 0;
+    if(UpCt > 3){
+        UpCt = 0;
+        Way = 0;
     }
 }
 
 void SetUp(){
-    int i;   
+    //int i;   
     
     Pwm[0] = PWM_LEVEL ;
     Pwm[1] = PWM_LEVEL ;
     Pwm[2] = PWM_LEVEL ;
     Pwm[3] = PWM_LEVEL ;
-    for(i = 0 ; i > MOTOR_NUM ;i++ ){
-        Photo[i].mode(PullUp);
-    }
+    //for(i = 0 ; i > MOTOR_NUM ; i++ ){
+        Photo[0].mode(PullUp);
+        Photo[1].mode(PullUp);
+        Photo[2].mode(PullUp);
+        Photo[3].mode(PullUp);
+        Photo[4].mode(PullUp);
+        Photo[5].mode(PullUp);
+        Photo[6].mode(PullUp);
+        Photo[7].mode(PullUp);
+    
+    //}
     Photo[0].fall(FallPhoto0);
     Photo[1].fall(FallPhoto1);
     Photo[2].fall(FallPhoto2);
@@ -387,8 +399,8 @@
     Photo[6].fall(FallPhoto6);
     Photo[7].fall(FallPhoto7);
     
-    Sw.mode(PullUp);                                   
-    Sw.fall(Reset);
+  //  Sw.mode(PullUp);                                   
+  //  Sw.fall(Reset);
  
 
 }
@@ -399,7 +411,18 @@
     wait(0.5);  
     
     while(1){
-        NumCount();
-    
+        if(MotorState[0]||MotorState[1]||MotorState[2]||MotorState[3]){
+            TempraFlag = 1;
+            Tempra = 0x0A;
+        }else if(TempraFlag){
+            StopTempra.attach(&TempraStop,TEMPRA_TIME);
+            TempraFlag = 0;
+        }
+            
+        if(And[0]&&And[1]&&And[2]&&And[3]){
+            NumCount();
+        }
+        
+        //printf("%d\t%d\n",UpCt,Way);
     }
 }