SINGLE Inputs code

Dependencies:   mbed reScale USBDevice

Revision:
7:0c2e51da8789
Parent:
6:40499bfc9ab2
--- a/main.cpp	Fri Mar 29 06:16:09 2019 +0000
+++ b/main.cpp	Sun Apr 07 15:16:14 2019 +0000
@@ -30,7 +30,7 @@
 int tw2 = 10;
 int tw3 = 15;
 
-int twReadMs = 10; // interval of twitch changes us.
+int twReadMs = 20; // interval of twitch changes us.
 
 
 DigitalOut dir(P0_15);
@@ -42,13 +42,13 @@
 
 // reScale servo1Scale(45,0,750,1800); //19850 is 1.0v from voltage divider at 10k.
 
-int maxServo = 2500;
-int minServo = 500;
+int maxServo = 500;
+int minServo = 2500;
 reScale servo1Scale(-90,90,minServo, maxServo);
 reScale servo2Scale(-90,90,minServo, maxServo);
 
- PwmOut servo1(P1_13); //Servo 1
- PwmOut servo2(P2_2); //Servo 2
+PwmOut servo1(P1_13); //Servo 1
+ //PwmOut servo2(P2_2); //Servo 2
  
 //char tens = '0';
 //char ones = '1';
@@ -70,7 +70,7 @@
   int lastServo1Pos = 10;
 int servo2Pos =0;
 int lastServo2Pos = 0;
-//DigitalOut servo2(P2_2);
+//DigitalOut servo2(P2_2); TESTING ONLY
 
            
     
@@ -94,11 +94,20 @@
 
 
       uint8_t c = 0; // for 485 link
+      
+      
+  //    tw.start(); /// start twitch timer
   
-    while(1) {
+    while(1) { 
         
 //************************************  Interrupt response **********************       
        
+       
+// if (tw.read() > 1200){              // timer can only hold 30 min, so reset after 20
+//tw.reset();
+//} 
+      
+       
         
         if (aux1 == 0 && aux1State == 0)
         {
@@ -223,6 +232,7 @@
                             currentNozzle = 2;
                             Hundreds = 0;
                             index = 3;
+                            //servo2=1;
                             }
                             
                          if (c == '4'){ //N2     
@@ -359,9 +369,9 @@
 if (currentNozzle == 1){                                     
 n1Pos = (((Hundreds *100) + (Tens *10) + Ones) - 90);
  twitchStart = 0; // trigger sweep from new angle                      
-//servo1Pos = servo1Scale.from(n1Pos);
+servo1Pos = servo1Scale.from(n1Pos);
 
-//servo1.pulsewidth_us(servo1Pos);              
+servo1.pulsewidth_us(servo1Pos);              
                          
 index=0;
 }
@@ -371,7 +381,9 @@
 twitchStart = 0; // trigger sweep from new angle                       
 //servo2Pos = servo1Scale.from(n2Pos);
 
-//servo2.pulsewidth_us(servo2Pos);              
+//servo2.pulsewidth_us(servo2Pos); 
+
+//servo2= 1;             
                          
 index=0;
 }
@@ -382,41 +394,42 @@
                  
 }
 
-
+/*
 if (twitch == 0){ 
-tw.stop();
+//tw.stop();
 twitchStart = 0;
 twitchDirection = 0;
 servo1Pos = servo1Scale.from(n1Pos);
 servo1.pulsewidth_us(servo1Pos); 
-servo2Pos = servo2Scale.from(n2Pos);
-servo2.pulsewidth_us(servo2Pos);
+//servo2Pos = servo2Scale.from(n2Pos);
+//servo2.pulsewidth_us(servo2Pos);
+//servo2.pulsewidth_us(servo1Pos); // TESTING 1POS on Servo 2!!!!
 }
 
 if (twitch > 0){
     if (twitchStart == 0){
-    tw.start();  // start timer for nozzle sweep
+  //  tw.start();  // start timer for nozzle sweep MOVED TO MAIN
     servo1Pos = servo1Scale.from(n1Pos);
     servo1.pulsewidth_us(servo1Pos); 
     
     lastTwitchMove = tw.read_ms(); // store time of last nozzle movement
     minTwitch = servo1Pos; // store original servo postion for bottom of sweep
     if (twitch == 1){
-    maxTwitch = servo1Pos + 150; // 11.11mS per degree of sweep, 55 = 5 degrees of sweep. This should be a viariable set on the display.
+    maxTwitch = servo1Pos - 150; // 11.11mS per degree of sweep, 55 = 5 degrees of sweep. This should be a viariable set on the display.
     }
     if (twitch == 2){
-    maxTwitch = servo1Pos + 200; 
+    maxTwitch = servo1Pos - 200; 
     }
     if (twitch == 3){
-    maxTwitch = servo1Pos + 250; 
+    maxTwitch = servo1Pos - 250; 
     }      
     twitchStart = 1;
 }
     if ((twitchStart == 1) && (tw.read_ms() > (lastTwitchMove + twReadMs))){
         if (twitchDirection == 0){ //going up
-        servo1Pos = servo1Pos + 25; // add variable amount of uS to the servo signal
+        servo1Pos = servo1Pos - 25; // add variable amount of uS to the servo signal
                 
-        if (servo1Pos > maxServo){ 
+        if (servo1Pos < maxServo){ 
             servo1Pos = maxServo;
             twitchDirection = 1; //reverse direction
             }
@@ -428,12 +441,12 @@
             }
             
         if (twitchDirection == 1){ // going down    
-               servo1Pos = servo1Pos - 25; // add variable amount of Ms to the servo signal
-        if (servo1Pos < minServo){ 
+               servo1Pos = servo1Pos + 25; // add variable amount of Ms to the servo signal
+        if (servo1Pos > minServo){ 
             servo1Pos = minServo;
             twitchDirection = 0; //reverse direction
             }
-         if (servo1Pos < minTwitch){
+         if (servo1Pos > minTwitch){
              servo1Pos = minTwitch;
              twitchDirection = 0; //reverse direction
              }
@@ -445,6 +458,15 @@
             }         
 }    
 }
+*/
+
+
+
+
+
+
+
+
 
 //********************************* Inputs serial *******************************