Display code

Dependencies:   mbed reScale RotaryEncoder RPG Adafruit_ST7735 Adafruit_GFX PinDetect Ee24xx08 mRotaryEncoder

Revision:
6:84ea3ec0660d
Parent:
5:69155b34efd6
Child:
8:4ad111112853
--- a/main.cpp	Sun Feb 24 21:30:05 2019 +0000
+++ b/main.cpp	Fri Mar 29 06:13:59 2019 +0000
@@ -3,6 +3,7 @@
 #include "RPG.h"
 #include "reScale.h"
 #include "Ee24xx08.h"
+#include "mRotaryEncoder.h"
 
 
 // DISPLAY INIT:
@@ -16,9 +17,24 @@
 #define YELLOW   0xFFE0 
 #define WHITE    0xFFFF
 
+/* EEPROM Memory map:
+
+1 - 50: N1 angles 
+51 - 100: Settings
+101- 150: N2 angles
+151- 200: Twitch settings
+200- 250: unused
+*/ 
+
 #define currentToolAddress 51
 #define numToolsAddress 52
 #define numNozAddress 53
+#define versionAddress 54 // 0= basic, 1=pro
+#define syncAddress 55
+#define n1DirAddress 56
+#define n2DirAddress 57
+
+
 
 
 //DigitalOut myled(P1_13);
@@ -34,7 +50,9 @@
 
 Adafruit_ST7735 tft(P0_9, P0_8, P0_6, P0_2, P0_22, P0_7); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
 
-reScale servo1Scale(-45,0,750,1800); //19850 is 1.0v from voltage divider at 10k.
+int maxServo = 2500;
+int minServo = 500;
+reScale servo1Scale(-90,90,minServo,maxServo); //
 
  PwmOut myServo(P1_13); //display board
 //PwmOut myServo(D4);
@@ -44,12 +62,16 @@
 DigitalIn encoderFalseButton(P0_23); // FALSE!!! push button of knob
 DigitalIn encoderButton(P0_20); // push button of knob
 DigitalIn backButton(P0_15); // back button
+DigitalIn aButton(P0_1); // ISP and A button
 
 Timer t;
+Timer tw;
 int lastBackButton;
 int lastDownButton;
 int lastEncoderButton;
 
+
+
 int dirt = 0;
 
 int main() {
@@ -61,7 +83,7 @@
   tft.setRotation(3);
   tft.fillScreen(BLACK);
   tft.setCursor(30,120);
-  tft.setTextColor(WHITE);
+  tft.setTextColor(BLUE);
   tft.setTextWrap(false);
   tft.printf("AccuBlast v0");
   
@@ -83,38 +105,87 @@
         tft.setCursor(4, 50);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
-        tft.printf("Nozzle");   
+        tft.printf("Twitch:");   
         
         tft.setCursor(4, 65);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
-        tft.printf("Res."); 
+        tft.printf("Sync:"); 
         
         tft.setCursor(4, 80);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
-        tft.printf("Res.");     
+        tft.printf("AUX 1:");   
         
         tft.setCursor(4, 95);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
+        tft.printf("AUX 2:"); 
+        
+        tft.setCursor(4, 110);
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
+        tft.printf("AUX 3:"); 
+          
+        tft.setCursor(75, 95);
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
         tft.printf("Num Tls");     
         
-        tft.setCursor(4, 110);
+        tft.setCursor(75, 110);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
         tft.printf("Num Noz");   
         
+        tft.setCursor(75, 50);
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
+        tft.printf("N1 dir:");       
+        
+        tft.setCursor(75, 65);
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
+        tft.printf("N2 dir:"); 
+
+        tft.setCursor(75, 80);
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
+        tft.printf("version:"); 
+        
  // **************************************************************************************************************       
-  
-int received = 9;
-int serialCounter = 0;
-int index = 0;
+ int version = 0; // 0= basic, 1=pro 
+ int lastVersion = 99;
+ 
+ int sync = 0;  // sync setting for N1 and N1
+ int lastSync = 99;
+ int syncSpread = 0;
+ 
+ int n1Dir = 0; // direction of knob for N1
+ int lastN1Dir = 99; 
+ 
+ int n2Dir = 0;  // direction of knob for N2
+ int lastN2Dir = 99;
+
+ int index = 0;
   
   int count = 0;
-  int maxCount = 7; //max number of menu items
+  int maxCount = 12; //max number of menu items
   int lastCount = 99;
   
+  int twitch = 0;
+  int lastTwitch = 300;
+  int lastTwitchMove = 0;
+  int twitchStart = 0; // has twitching begun?
+  int twitchDirection = 0; // 0 is up, 1 is down.
+  int maxTwitch = 0;
+  int minTwitch = 0;
+
+int tw1 = 5; // stages of servo write to control speed of sweep. 11.11 Ms = 1 degree
+int tw2 = 10;
+int tw3 = 15;
+
+int twReadMs = 100; // interval of twitch changes us.
+  
   //************* Pull previous setting from eeprom *******************
   
   char tempToolNo = 0;                  // last tool in spindle
@@ -129,10 +200,50 @@
   eeprom.read(numNozAddress, &tempNumNoz);
   int numNoz = (int)tempNumNoz;
   
-  char tempLastAngle = 0;                  // last angle of last tool in spindle
+  char tempLastAngle = 0;                  // last angle of last tool in spindle N1
   eeprom.read(currentToolNo, &tempLastAngle);
   int lastAngle = (int)tempLastAngle;
-  lastAngle = lastAngle - 45;              // Not storing signed int, so signed value is 45 less than what was stored to memory.
+  lastAngle = lastAngle - 90;              // Not storing signed int, so signed value is 45 less than what was stored to memory.
+  
+   char tempLastAngleN2 = 0;                  // last angle of last tool in spindle
+  eeprom.read(currentToolNo+100, &tempLastAngleN2);
+  int lastAngleN2 = (int)tempLastAngleN2;
+  lastAngleN2 = lastAngleN2 - 90;              // Not storing signed int, so signed value is 45 less than what was stored to memory.
+  
+  char tempLastTwitch = 0;                  // last Twitch setting for the current tool in spindle
+  eeprom.read(currentToolNo + 150, &tempLastTwitch);
+  twitch = (int)tempLastTwitch;     // should be 1, 2 or 3.
+   
+  char tempLastVersion = 0;   // version of system. 0=basic, 1=pro
+  eeprom.read(versionAddress, &tempLastVersion);
+  version = (int)tempLastVersion;
+  
+  char tempLastSync = 0;   // previous sync setting
+  eeprom.read(syncAddress, &tempLastSync);
+  sync = (int)tempLastSync;
+    
+   char tempLastN1Dir = 0;   // previous N1 knob direction setting
+  eeprom.read(n1DirAddress, &tempLastN1Dir);
+  n1Dir = (int)tempLastN1Dir;
+ 
+    char tempLastN2Dir = 0;   // previous N2 knob direction setting
+  eeprom.read(n2DirAddress, &tempLastN2Dir);
+  n2Dir = (int)tempLastN2Dir;
+    
+    
+  /*EEprom test over 2 digits:
+  int test1 = 150;
+  eeprom.write(199, test1);
+  wait(1);
+char tempN2char = 0;  
+eeprom.read(199, &tempN2char);
+int tempN2 = (int)tempN2char;
+        tft.setCursor(130,120);
+        tft.setTextColor(RED);
+        tft.setTextSize(1);
+        tft.printf("%d", tempN2);
+ */ 
+
   
   //******************** Button debounce *********************************
   
@@ -142,6 +253,7 @@
  //tDownButton.mode(PullUp);     // button pull up   
  encoderButton.mode(PullUp); 
  encoderFalseButton.mode(PullUp);  
+ aButton.mode(PullUp);
     
 t.start();                   // starts timer 
 lastBackButton = t.read_ms(); 
@@ -158,9 +270,14 @@
   int n1Pos = lastAngle;                 //Set servo to last known postition prior to power off
   int lastN1Pos = 150;
   
+  int n2Pos = lastAngleN2;
+  int lastN2Pos = 150;
+  
   int servo1Pos = 0;
   
-   RPG rpg1(P2_2,P1_20,P0_23);  //  RPG (PinName pA, PinName pB, PinName pPB)P0_23 is unconnected!! ;
+ //  RPG rpg1(P2_2,P1_20,P0_23);  //  RPG (PinName pA, PinName pB, PinName pPB)P0_23 is unconnected!! ;
+ //mRotaryEncoder Enc(P2_2,P1_20,P0_23, PullUp, 500);
+  mRotaryEncoder Enc(P1_20,P2_2,P0_23, PullUp, 1000);
   
   myServo.period_ms(20);
 
@@ -195,6 +312,8 @@
     
     while(1) {
         
+            
+        
        /* 
      while (device.readable())
             {
@@ -262,12 +381,14 @@
 lastEncoderButton = t.read_ms();
 } 
 
+
+
 if (backButton == 0){  //Back button was pressed so back to default selection
      count = 0;
      }    
      
 if (encoderButton == 0){
-if (t.read_ms() > (lastEncoderButton + 200)){
+if (t.read_ms() > (lastEncoderButton + 50)){ // from 200
 count = count + 1;  //Reset count if PB pressed
 }
  if (count > maxCount){
@@ -279,118 +400,374 @@
         count = 0;
         }  
         }    
+
+// ************* Rectangles for identifying active selection *****************************
+
+if (count != lastCount){
+ 
+ if (count ==0){
+  Enc.Set(n1Pos);
+  }
+  
+ else if (count == 1){
+   Enc.Set(n2Pos);
+  }       
+ 
+  else if (count == 2){
+   Enc.Set(currentToolNo);
+  } 
+ 
+  else if (count == 3){
+   Enc.Set(twitch);
+  } 
+ 
+   else if (count == 4){
+   Enc.Set(sync);
+  } 
+ 
+    else if (count == 8){
+   Enc.Set(n1Dir);
+  } 
+ 
+    else if (count == 9){
+   Enc.Set(n2Dir);
+  }  
+ 
+    else if (count == 10){
+   Enc.Set(version);
+  }  
+ 
+    else if (count == 11){
+   Enc.Set(numTools);
+  } 
+
+    else if (count == 12){
+   Enc.Set(numNoz);
+  } 
+
+     
+ 
+ // clear old boxes (white or black)
+  if (lastCount == 0){ 
+tft.drawRect(2,2,74,18,WHITE);
+} 
+ 
+else if ((lastCount == 1) && (numNoz == 2)){ 
+tft.drawRect(78,2,74,18,WHITE);
+} 
+ 
+else if (lastCount == 2){ 
+tft.drawRect(20,26,28,18,BLACK);
+}
+ 
+else if (lastCount == 3){
+tft.drawRect(50,48,20,12,BLACK);
+} 
+ 
+else if (lastCount == 4){
+tft.drawRect(50,63,20,12,BLACK);
+} 
+ 
+else if (lastCount == 5){
+tft.drawRect(50,78,20,12,BLACK);
+} 
+ 
+else if (lastCount == 6){
+tft.drawRect(50,93,20,12,BLACK);
+}  
+ 
+else if (lastCount == 7){
+tft.drawRect(50,108,20,12,BLACK);
+} 
+ 
+else if (lastCount == 8){
+tft.drawRect(130,48,20,12,BLACK);
+} 
+ 
+else if (lastCount == 9){
+tft.drawRect(130,63,20,12,BLACK);
+} 
+ 
+else if (lastCount == 10){
+tft.drawRect(130,78,20,12,BLACK);
+} 
+
+else if (lastCount == 11){
+tft.drawRect(130,93,20,12,BLACK);
+} 
+
+else if (lastCount == 12){
+tft.drawRect(130,108,20,12,BLACK);
+} 
+
+ // Set new box
+ 
+if (count == 0){ // N1 adjust
+tft.drawRect(2,2,74,18,GREEN);
+}
+
+else if ((count == 1) && (numNoz == 2)){ // N2 adjust
+tft.drawRect(78,2,74,18,GREEN);
+}
+
+else if (count == 2){ 
+tft.drawRect(20,26,28,18,GREEN);
+}
+
+else if (count == 3){
+tft.drawRect(50,48,20,12,GREEN);
+}
+
+else if (count == 4){
+tft.drawRect(50,63,20,12,GREEN);
+}
+
+else if (count == 5){
+tft.drawRect(50,78,20,12,GREEN);
+}
+
+else if (count == 6){
+tft.drawRect(50,93,20,12,GREEN);
+}
+
+else if (count == 7){
+tft.drawRect(50,108,20,12,GREEN);
+}
+
+else if (count == 8){
+tft.drawRect(130,48,20,12,GREEN);
+}
+
+else if (count == 9){
+tft.drawRect(130,63,20,12,GREEN);
+}
+
+else if (count == 10){
+tft.drawRect(130,78,20,12,GREEN);
+}
+
+else if (count == 11){
+tft.drawRect(130,93,20,12,GREEN);
+}
+
+else if (count == 12){
+tft.drawRect(130,108,20,12,GREEN);
+}
+
+lastCount = count;
+}
+
+
+
+
  
  //****************** ENCODER ACTIONS ************************************************
  if (count == 0){   // knob controls servo value
-dirt = rpg1.dir(); //Get Dir
-n1Pos = n1Pos + dirt; //Ad Dirt to n1pos
-if (n1Pos > 0){
- n1Pos = 0;
+n1Pos = Enc.Get();
+if (sync == 1){
+    n2Pos = (n1Pos + syncSpread);
+    if (n2Pos > 90){
+ n2Pos = 90;
+ //Enc.Set(90);
+ }
+ if (n2Pos < -90){
+     n2Pos = -90;
+ //    Enc.Set(-90);
+     }
+ } 
+ if (sync == 2){
+    n2Pos = n2Pos + (lastN1Pos - n1Pos);
+    if (n2Pos > 90){
+ n2Pos = 90;
+ //Enc.Set(90);
  }
- if (n1Pos < -45){
-     n1Pos = -45;
+ if (n2Pos < -90){
+     n2Pos = -90;
+ //    Enc.Set(-90);
      }
+ }  
+if (n1Pos > 90){
+ n1Pos = 90;
+ Enc.Set(90);
+ }
+ if (n1Pos < -90){
+     n1Pos = -90;
+     Enc.Set(-90);
      }
-     
+ }   
+ //*************************** 
+ if (count == 1){   // knob controls servo value
+n2Pos = Enc.Get(); 
+if (n2Pos > 90){
+ n2Pos = 90;
+ Enc.Set(90);
+ }
+ if (n2Pos < -90){
+     n2Pos = -90;
+     Enc.Set(-90);
+     }
+ }    
+//****************************       
 if (count == 2){     //knob controls tool number
-dirt = rpg1.dir(); //Get Dir
-currentToolNo = currentToolNo + dirt; //Ad Dirt to n1pos
+
+currentToolNo = Enc.Get();
 if (currentToolNo > numTools){
  currentToolNo = numTools;
  }
  if (currentToolNo < 1){
      currentToolNo = 1;
+     Enc.Set(1);
      }
      }
+ //****************************    
+if (count == 3){  // Knob controls twitch setting
+ twitch = Enc.Get();
+ if (twitch < 0){
+     twitch = 0;
+     Enc.Set(0);
+     }
+     if (twitch > 3){
+         twitch = 3;
+         Enc.Set(3);
+         }
+  }    
+ //**************************** 
+  if (count == 4){  // Knob controls sync setting. 1=off, 2=sync, 3=rev
+ sync = Enc.Get();
+ if (sync < 0){
+     sync= 0;
+     Enc.Set(0);
+     }
+     if (sync > 2){
+         sync = 2;
+         Enc.Set(2);
+         }
+  } 
+  //****************************       
+  // 5,6,7 AUX?
+  if (count == 8){   // knob controls the direction of rotation for N1
+ n1Dir = Enc.Get();
+ 
+ if (n1Dir > 1){
+     n1Dir = 1;
+     Enc.Set(1);
+     }
      
-     if (count == 6){     //knob controls tool number
-dirt = rpg1.dir(); //Get Dir
-numTools = numTools + dirt; //Ad Dirt to n1pos
+     if (n1Dir < 0){
+         n1Dir =0;
+         Enc.Set(0);
+         }
+ }   
+//**************************** 
+   if (count == 9){   // knob controls the direction of rotation for N2
+ n2Dir = Enc.Get();
+ 
+ if (n2Dir > 1){
+     n2Dir = 1;
+     Enc.Set(1);
+     }
+     
+     if (n2Dir < 0){
+         n2Dir =0;
+         Enc.Set(0);
+         }
+ }
+//**************************** 
+   if (count == 10){   // knob controls the version 
+version = Enc.Get();
+ 
+ if (version > 1){
+     version = 1;
+     Enc.Set(1);
+     }
+     
+     if (version < 0){
+         version =0;
+         Enc.Set(0);
+         }
+ }         
+ //****************************    
+     if (count == 11){     //knob controls tool number
+numTools = Enc.Get();
 if (numTools > 50){
  numTools = 50;
+ Enc.Set(50);
  }
  if (numTools < 1){
      numTools = 1;
+     Enc.Set(1);
      }
      }
-     
-     if (count == 7){     //knob controls tool number
-dirt = rpg1.dir(); //Get Dir
-numNoz = numNoz + dirt; //Ad Dirt to n1pos
+//****************************     
+     if (count == 12){     //knob controls tool number
+numNoz = Enc.Get();
 if (numNoz> 2){
  numNoz = 2;
+ Enc.Set(2);
  }
  if (numNoz < 1){
      numNoz = 1;
+     Enc.Set(1);
      }
      }     
-     
- 
-// ************* Rectangles for identifying active selection *****************************
- 
-if (count == 0){ // N1 adjust
-tft.drawRect(2,2,74,18,GREEN);
-}
-else{
-tft.drawRect(2,2,74,18,WHITE);
-}
+//****************************************************************************************
 
-if ((count == 1) && (numNoz == 2)){ // N2 adjust
-tft.drawRect(78,2,74,18,GREEN);
-}
-
-if ((count != 1) && (numNoz == 2)){ // N2 adjust
-tft.drawRect(78,2,74,18,WHITE);
+if (version == 0){ //Basic runs the servo direct from the display unit
+if (twitch == 0){ 
+tw.stop();
+twitchStart = 0;
+twitchDirection = 0;
+servo1Pos = servo1Scale.from(n1Pos);
 }
 
-
-if (count == 2){ 
-tft.drawRect(20,26,28,18,GREEN);
-}
-else{
-tft.drawRect(20,26,28,18,BLACK);
-}
- 
-if (count == 3){
-tft.drawRect(53,48,20,12,GREEN);
+if (twitch > 0){
+    if (twitchStart == 0){
+    tw.start();  // start timer for nozzle sweep
+    servo1Pos = servo1Scale.from(n1Pos);
+    lastTwitchMove = tw.read_ms(); // store time of last nozzle movement
+    minTwitch = servo1Pos; // store original servo postion for bottom of sweep
+    maxTwitch = servo1Pos + 1000; // 11.11mS per degree of sweep, 55 = 5 degrees of sweep. This should be a viariable set on the display.
+    twitchStart = 1;
+    }
+    if ((twitchStart == 1) && (tw.read_ms() > (lastTwitchMove + twReadMs))){
+        if (twitchDirection == 0){ //going up
+        servo1Pos = servo1Pos + 100; // add variable amount of uS to the servo signal
+        
+                tft.setCursor(130,120);
+        tft.setTextColor(RED);
+        tft.setTextSize(1);
+        tft.printf("%d", servo1Pos);
+        
+        
+        if (servo1Pos > maxServo){ 
+            servo1Pos = maxServo;
+            twitchDirection = 1; //reverse direction
+            }
+         if (servo1Pos > maxTwitch){
+             servo1Pos = maxTwitch;
+             twitchDirection = 1; //reverse direction
+             }
+            lastTwitchMove = tw.read_ms();
+            }
+            
+        if (twitchDirection == 1){ // going down    
+               servo1Pos = servo1Pos - 100; // add variable amount of Ms to the servo signal
+        if (servo1Pos < minServo){ 
+            servo1Pos = minServo;
+            twitchDirection = 0; //reverse direction
+            }
+         if (servo1Pos < minTwitch){
+             servo1Pos = minTwitch;
+             twitchDirection = 0; //reverse direction
+             }
+            lastTwitchMove = tw.read_ms();
+            }         
+    }    
+    }
+    
+myServo.pulsewidth_us(servo1Pos);    
 }
-else{
-tft.drawRect(53,48,20,12,BLACK);
-} 
- 
-if (count == 4){
-tft.drawRect(53,63,20,12,GREEN);
-}
-else{
-tft.drawRect(53,63,20,12,BLACK);
-} 
 
-if (count == 5){
-tft.drawRect(53,78,20,12,GREEN);
-}
-else{
-tft.drawRect(53,78,20,12,BLACK);
-} 
-
-if (count == 6){
-tft.drawRect(53,93,20,12,GREEN);
-}
-else{
-tft.drawRect(53,93,20,12,BLACK);
-} 
-
-if (count == 7){
-tft.drawRect(53,108,20,12,GREEN);
-}
-else{
-tft.drawRect(53,108,20,12,BLACK);
-} 
-
-//****************************************************************************************
- 
-servo1Pos = servo1Scale.from(n1Pos);
-
-myServo.pulsewidth_us(servo1Pos);
 
 //********************** update display values **********************************************
 
@@ -412,12 +789,13 @@
         tempLastAngle = 0;                  
         eeprom.read(currentToolNo, &tempLastAngle);
         lastAngle = (int)tempLastAngle;
-        lastAngle = lastAngle - 45;                       // Not storing signed int, so signed value is 45 less than what was stored to memory.
-        //RotaryEncoder knob1(D2, D3, -45, 45, lastAngle);  //  RotaryEncoder(PinName pin1_name, PinName pin2_name, int min = 0, int max = 100, int val = 50);
-        //knob1.val = lastAngle;
+        lastAngle = lastAngle - 90;                       // Not storing signed int, so signed value is 90 less than what was stored to memory.
         n1Pos = lastAngle;
         eeprom.write(currentToolAddress, currentToolNo);
-        //wait(0.1);
+        wait(0.1);
+        tempLastTwitch = 0;
+        eeprom.read(currentToolNo + 150, &tempLastTwitch); // pull up previous twitch setting.
+        twitch = (int)tempLastTwitch;
         lastToolNo = currentToolNo;
         }
        
@@ -431,26 +809,50 @@
         tft.setTextColor(GREEN);
         tft.setTextSize(2);
         tft.printf("%d", n1Pos);
-        eeprom.write(currentToolNo, n1Pos + 45);        // store new angle in eeprom, add 45 because its an unsigned value.
+        eeprom.write(currentToolNo, n1Pos + 90);        // store new angle in eeprom, add 90 because its an unsigned value.
         //wait(0.1); // adjusted from 1
         dir=1;
         device.printf("N");
-        device.printf("%d", n1Pos + 145); //send over RS485 add 145 so no negative values and always 3 digits. 1xx for N1, 2xx for nozzle 2
+        device.printf("%d", n1Pos + 190); //send over RS485 add 190 so no negative values and always 3 digits.
        //device.printf("N245");  // TEST!!!
-       tft.setCursor(100, 50);
-         tft.setTextSize(2);
-       tft.printf("%d", n1Pos + 145);
-        //wait(0.05);
+      // tft.setCursor(100, 50);
+       //  tft.setTextSize(2);
+      // tft.printf("%d", n1Pos + 145);
+        wait(0.05);
         dir=0;
         lastN1Pos = n1Pos;
         }  
         
+if (n2Pos != lastN2Pos && numNoz == 2){     
+        tft.setCursor(115, 4);
+        tft.setTextColor(BLACK);
+        tft.setTextSize(2);
+        tft.printf("%d", lastN2Pos);
+        tft.setCursor(115, 4);
+        tft.setTextColor(GREEN);
+        tft.setTextSize(2);
+        tft.printf("%d", n2Pos);
+        eeprom.write(currentToolNo +100, n2Pos + 90);        // store new angle in eeprom, add 90 because its an unsigned value.
+        //wait(0.1); // adjusted from 1
+        dir=1;
+        device.printf("N");
+        device.printf("%d", n2Pos + 390); //send over RS485 add 390 so no negative values and always 3 digits.
+       //device.printf("N245");  // TEST!!!
+      // tft.setCursor(100, 50);
+       //  tft.setTextSize(2);
+      // tft.printf("%d", n1Pos + 145);
+        wait(0.05);
+        dir=0;
+        lastN2Pos = n2Pos;
+        }        
+        
+        
    if (numTools != lastNumTools){     
-        tft.setCursor(55, 95);
+        tft.setCursor(133, 95);
         tft.setTextColor(BLACK);
         tft.setTextSize(1);
         tft.printf("%d", lastNumTools);
-        tft.setCursor(55,95);
+        tft.setCursor(133,95);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
         tft.printf("%d", numTools);
@@ -460,11 +862,11 @@
         }          
 
    if (numNoz != lastNumNoz){     
-        tft.setCursor(55, 110);
+        tft.setCursor(133, 110);
         tft.setTextColor(BLACK);
         tft.setTextSize(1);
         tft.printf("%d", lastNumNoz);
-        tft.setCursor(55,110);
+        tft.setCursor(133,110);
         tft.setTextColor(WHITE);
         tft.setTextSize(1);
         tft.printf("%d", numNoz);
@@ -483,47 +885,140 @@
         tft.setTextColor(BLACK);
         tft.setTextSize(2);
         tft.printf("N2");
+        tft.setCursor(115, 4);
+        tft.printf("%d", n2Pos);
         tft.drawRect(78,2,74,18,BLACK);        
         }
         lastNumNoz = numNoz;
         }    
-
-
+        
+        
+   if (twitch != lastTwitch){    
+        tft.setCursor(52, 50);
+        tft.setTextColor(BLACK);
+        tft.setTextSize(1);
+        tft.printf("%d", lastTwitch);
+        tft.setCursor(52, 50);
+        tft.setTextColor(GREEN);
+        tft.setTextSize(1);
+        tft.printf("%d", twitch);
+        dir=1;
+        device.printf("W");
+        device.printf("%d", twitch); 
+        wait(0.05);
+        dir=0;
+        eeprom.write(currentToolNo +150, twitch); // store new twitch setting 
+        lastN1Pos = 999; // trigger sending servo values to module 
+        lastN2Pos = 999;
+        lastTwitch = twitch;                 
+}
 
+   if (sync != lastSync){    // 
+        tft.setCursor(52, 65);
+        tft.setTextColor(BLACK);
+        tft.setTextSize(1);
+        if (lastSync == 0){    
+        tft.printf("OFF");
+        }
+        if (lastSync == 1){
+        tft.printf("ON");
+        }
+        if (lastSync == 2){
+        tft.printf("REV"); 
+        }                       
+        tft.setCursor(52, 65);
+        tft.setTextSize(1);
+        if (sync == 0){
+        tft.setTextColor(WHITE);
+        tft.printf("OFF");
+        }
+        if (sync == 1){
+        tft.setTextColor(GREEN);
+        tft.printf("ON");  
+        }
+        if (sync == 2){
+        tft.setTextColor(RED);
+        tft.printf("REV");  
+        }                    
+        eeprom.write(syncAddress, sync); // store new twitch setting 
+        syncSpread = (n2Pos - n1Pos);
+        
+        lastSync = sync; 
+                        
+}
 
-   if (count != lastCount){     
-        tft.setCursor(150, 100);
+   if (n1Dir != lastN1Dir){    // 
+        tft.setCursor(132, 50);
         tft.setTextColor(BLACK);
         tft.setTextSize(1);
-        tft.printf("%d", lastCount);
-        tft.setCursor(150,100);
-        tft.setTextColor(RED);
+        if (lastN1Dir == 0){            
+        tft.printf("NOR");
+        }
+        if (lastN1Dir == 1){
+         tft.printf("REV");
+         }           
+        tft.setCursor(132, 50);
+        if (n1Dir == 0){
+        tft.setTextColor(WHITE);
+        tft.printf("NOR");
+        }
+        if (n1Dir == 1){
+        tft.setTextColor(GREEN);
+        tft.printf("REV");
+        }
+        eeprom.write(n1DirAddress, n1Dir); // store new twitch setting 
+        lastN1Dir = n1Dir;                 
+}
+
+   if (n2Dir != lastN2Dir){    // 
+        tft.setCursor(132, 65);
+        tft.setTextColor(BLACK);
         tft.setTextSize(1);
-        tft.printf("%d", count);
-        lastCount = count;
-        }                 
+        if (lastN2Dir == 0){            
+        tft.printf("NOR");
+        }
+        if (lastN2Dir == 1){
+         tft.printf("REV");
+         }           
+        tft.setCursor(132, 65);
+        if (n2Dir == 0){
+        tft.setTextColor(WHITE);
+        tft.printf("NOR");
+        }
+        if (n2Dir == 1){
+        tft.setTextColor(GREEN);
+        tft.printf("REV");
+        }
+        eeprom.write(n2DirAddress, n2Dir); // store new twitch setting 
+        lastN2Dir = n2Dir;                 
+}
+
+   if (version != lastVersion){    // 
+        tft.setCursor(132, 80);
+        tft.setTextColor(BLACK);
+        tft.setTextSize(1);
+        if (lastVersion == 0){
+            tft.printf("BAS");
+            }
+        if (lastVersion == 1){
+            tft.printf("PRO");
+            }
+        tft.setCursor(132, 80);
+        if (version == 0){
+        tft.setTextColor(WHITE);
+        tft.setTextSize(1);
+        tft.printf("BAS");
+        }
+        if (version == 1){
+            tft.setTextColor(RED);
+            tft.printf("PRO");
+            }
+        eeprom.write(versionAddress, version); // store new twitch setting 
+        lastVersion = version;                 
+}
+
 
 //**************************** RS485 link ***************************************
-    
-  //dir =0; // receiver enable  
-/*    
-if (device.readable())
-            {
-                c = device.getc();
-                if(c=='B'){
-                received = 1;   
-                } 
-}
-
-if (received == 1){
-        tft.setCursor(150, 80);
-        tft.setTextColor(WHITE);
-        tft.setTextSize(1);
-        tft.printf("1"); 
-        }
-
-*/
-
 
 while (device.readable())
             {
@@ -668,11 +1163,11 @@
                              index = 0;
                              }
                      else if (c == '-'){
-                         
+                         /*
                                            tft.setTextColor(WHITE);
                   tft.setCursor(100, 100);
                    tft.printf("1");
-                         
+                         */
                          currentToolNo = (currentToolNo -1);
                          if (currentToolNo < 1){
                              currentToolNo = numTools;
@@ -683,12 +1178,6 @@
 }
 }
 
-
-
-
-
-
-
 }
 }