Advertisement
AdamBB

Slider Code Final

Jun 28th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 19.62 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 20, 4);
  6.  
  7. const int dirPinS = 2;
  8. const int dirPinP = 3;
  9. const int dirPinT = 4;
  10.  
  11. const int stepPinS = 5;
  12. const int stepPinP = 6;
  13. const int stepPinT = 7;
  14.  
  15. const int encoderPinA = A3;
  16. const int encoderPinB = A2;
  17. int encoderPinALast = LOW;
  18. int encoderPinACurrent = LOW;
  19. int encoderPinBCurrent = LOW;
  20.  
  21. unsigned long nextMoveSlide = micros();
  22. unsigned long nextMovePan = micros();
  23. unsigned long nextMoveTilt = micros();
  24.  
  25. unsigned long nextStatRefresh = millis();
  26.  
  27. long globalSlide = 0;
  28. long globalPan = 0;
  29. long globalTilt = 0;
  30. int globalTime = 30;
  31. long pointAS, pointAP, pointAT, pointBS, pointBP, pointBT;
  32. int moveMode = 0;  // 0 = none, 1 = slide, 2 = pan+tilt
  33. int statsMode = 0; // 0 = none, 1 = show current pos, 2 = show point A pos, 3 = show point B pos, 4 = set time
  34. bool statsDisplayed = false;
  35.  
  36. bool regenerateLcdRequired = true;
  37. int regenerateLcdMode = 0;
  38. // 0 = whole screen
  39. // 1 = menu items
  40. // 2 = menu arrow
  41. int currentScreen = 0;
  42. // 0 = home
  43. // 10 = stats index
  44. // 11 = show stats - current pos
  45. // 12 = show stats - A point pos
  46. // 13 = show stats - B point pos
  47. // 20 = set point index
  48. // 21 = set point A
  49. // 22 = set point B
  50. // 30 = go to point index
  51. // 31 = going to point a
  52. // 32 = goint to point b
  53. int currentOptionSelected = 0; // LCD's line selected in menu
  54.  
  55. void setPointA() {
  56.   pointAS = globalSlide;
  57.   pointAP = globalPan;
  58.   pointAT = globalTilt;
  59.   Serial.println(
  60.     "Set point A - S:" +
  61.     String(pointAS) + " P:" +
  62.     String(pointAP) + " T;" +
  63.     String(pointAT)
  64.   );
  65. }
  66.  
  67. void setPointB() {
  68.   pointBS = globalSlide;
  69.   pointBP = globalPan;
  70.   pointBT = globalTilt;
  71.   Serial.println(
  72.     "Set point B - S:" +
  73.     String(pointBS) + " P:" +
  74.     String(pointBP) + " T;" +
  75.     String(pointBT)
  76.   );
  77. }
  78.  
  79. void goToPoint(bool point) {
  80.   long microstepsSlide, microstepsPan, microstepsTilt;
  81.   long afterMoveGlobalSlide, afterMoveGlobalPan, afterMoveGlobalTilt;
  82.   if (point == 0) {
  83.     microstepsSlide = globalSlide - pointAS;
  84.     microstepsPan = globalPan - pointAP;
  85.     microstepsTilt = globalTilt - pointAT;
  86.     afterMoveGlobalSlide = pointAS;
  87.     afterMoveGlobalPan = pointAP;
  88.     afterMoveGlobalTilt = pointAT;
  89.     if(pointAS < globalSlide) {
  90.       digitalWrite(dirPinS, LOW);
  91.     } else {
  92.       digitalWrite(dirPinS, HIGH);
  93.     }
  94.     if(pointAP < globalPan) {
  95.       digitalWrite(dirPinP, LOW);
  96.     } else {
  97.       digitalWrite(dirPinP, HIGH);
  98.     }
  99.     if(pointAT < globalTilt) {
  100.       digitalWrite(dirPinT, LOW);
  101.     } else {
  102.       digitalWrite(dirPinT, HIGH);
  103.     }
  104.      
  105.   } else if (point == 1) {
  106.     microstepsSlide = globalSlide - pointBS;
  107.     microstepsPan = globalPan - pointBP;
  108.     microstepsTilt = globalTilt - pointBT;
  109.     afterMoveGlobalSlide = pointBS;
  110.     afterMoveGlobalPan = pointBP;
  111.     afterMoveGlobalTilt = pointBT;
  112.     if(pointBS < globalSlide) {
  113.       digitalWrite(dirPinS, LOW);
  114.     } else {
  115.       digitalWrite(dirPinS, HIGH);
  116.     }
  117.     if(pointBP < globalPan) {
  118.       digitalWrite(dirPinP, LOW);
  119.     } else {
  120.       digitalWrite(dirPinP, HIGH);
  121.     }
  122.     if(pointBT < globalTilt) {
  123.       digitalWrite(dirPinT, LOW);
  124.     } else {
  125.       digitalWrite(dirPinT, HIGH);
  126.     }
  127.   }
  128.  
  129.   long totalTime = globalTime * 1000000;
  130.   long nextMoveSlide, nextMovePan, nextMoveTilt;
  131.  
  132.   microstepsSlide = abs(microstepsSlide);
  133.   microstepsPan = abs(microstepsPan);
  134.   microstepsTilt = abs(microstepsTilt);
  135.  
  136.   long timeGapSlide = totalTime / microstepsSlide;
  137.   long timeGapPan = totalTime / microstepsPan;
  138.   long timeGapTilt = totalTime / microstepsTilt;
  139.  
  140.   Serial.println("Steps to do SLIDE: " + String(microstepsSlide));
  141.   Serial.println("Steps to do PAN: " + String(microstepsPan));
  142.   Serial.println("Steps to do TILT: " + String(microstepsTilt));
  143.   Serial.println("Microseconds for movement: " + String(totalTime));
  144.  
  145.   Serial.println("2s to start moving.....");
  146.   delay(2000);
  147.  
  148.   nextMoveSlide = micros();
  149.   nextMovePan = micros();
  150.   nextMoveTilt = micros();
  151.  
  152.   while (
  153.     microstepsSlide > 0 ||
  154.     microstepsPan > 0 ||
  155.     microstepsTilt > 0
  156.   ) {
  157.     bool doSlideMove = 0;
  158.     bool doPanMove = 0;
  159.     bool doTiltMove = 0;
  160.     if (microstepsSlide > 0) {
  161.       if (nextMoveSlide < micros()) {
  162.         doSlideMove = 1;
  163.         microstepsSlide = microstepsSlide - 1;
  164.         nextMoveSlide = nextMoveSlide + timeGapSlide;
  165.       }
  166.     }
  167.     if (microstepsPan > 0) {
  168.       if (nextMovePan < micros()) {
  169.         doPanMove = 1;
  170.         microstepsPan = microstepsPan - 1;
  171.         nextMovePan = nextMovePan + timeGapPan;
  172.       }
  173.     }
  174.     if (microstepsTilt > 0) {
  175.       if (nextMoveTilt < micros()) {
  176.         doTiltMove = 1;
  177.         microstepsTilt = microstepsTilt - 1;
  178.         nextMoveTilt = nextMoveTilt + timeGapTilt;
  179.       }
  180.     }
  181.  
  182.     if(doSlideMove) { digitalWrite(stepPinS, HIGH); }
  183.     if(doPanMove) { digitalWrite(stepPinP, HIGH); }
  184.     if(doTiltMove) { digitalWrite(stepPinT, HIGH); }
  185.     delayMicroseconds(20);
  186.     if(doSlideMove) { digitalWrite(stepPinS, LOW); }
  187.     if(doPanMove) { digitalWrite(stepPinP, LOW); }
  188.     if(doTiltMove) { digitalWrite(stepPinT, LOW); }
  189.     delayMicroseconds(20);
  190.   }
  191.  
  192.   globalSlide = afterMoveGlobalSlide;
  193.   globalPan = afterMoveGlobalPan;
  194.   globalTilt = afterMoveGlobalTilt;
  195.   Serial.println("MOVE FINISHED!");
  196. }
  197.  
  198. void setup() {
  199.   pinMode(stepPinS,OUTPUT);
  200.   pinMode(stepPinP,OUTPUT);
  201.   pinMode(stepPinT,OUTPUT);
  202.   pinMode(dirPinS,OUTPUT);
  203.   pinMode(dirPinP,OUTPUT);
  204.   pinMode(dirPinT,OUTPUT);
  205.  
  206.   pinMode(A1, INPUT);
  207.   pinMode(A2, INPUT);
  208.   pinMode(A3, INPUT);
  209.   pinMode(A6, INPUT);
  210.   pinMode(A7, INPUT);
  211.  
  212.   Serial.begin(9600);
  213.   lcd.begin();
  214.   lcd.setCursor(0,0);
  215.   lcd.print("--------------------");
  216.   lcd.setCursor(0,1);
  217.   lcd.print("|    PRO SLIDER    |");
  218.   lcd.setCursor(0,2);
  219.   lcd.print("| 3 axis movement  |");
  220.   lcd.setCursor(0,3);
  221.   lcd.print("--------------------");
  222.   delay(1500);
  223.   lcd.clear();
  224. }
  225.  
  226. int getJoyValue(long value, bool direction) {
  227.   // if direction == 0, analyse lower half of values (eg. left, bottom),
  228.   // if direction == 1, analyse higher half of values (eg right, top)
  229.   if(value >= 520 && direction == 1) {
  230.     return map(value, 520, 1024, 0, 100);
  231.   } else if(value <= 500 && direction == 0) {
  232.     return map(value, 500, 0, 0, 100);
  233.   } else {
  234.     return 0;
  235.   }
  236. }
  237.  
  238. void loop() {
  239.   long joyY = analogRead(6);
  240.   long joyX = analogRead(7);
  241.  
  242.   // bool values - flags if button, encoder of joystick was used
  243.   bool button = analogRead(1) < 50;
  244.   bool encLeft = false;
  245.   bool encRight = false;
  246.   bool joyLeft = joyX < 450;
  247.   bool joyRight = joyX > 570;
  248.   bool joyUp = joyY > 570;
  249.   bool joyDown = joyY < 450;
  250.  
  251.   // int values for joystick input - from 0 to 100 for each direction
  252.   long joyLeftValue = getJoyValue(joyX, 1);
  253.   long joyRightValue = getJoyValue(joyX, 0);
  254.   long joyUpValue = getJoyValue(joyY, 0);
  255.   long joyDownValue = getJoyValue(joyY, 1);
  256.  
  257.   // handling of encoder rotations
  258.   encoderPinACurrent = digitalRead(encoderPinA);
  259.   encoderPinBCurrent = digitalRead(encoderPinB);
  260.   if (encoderPinALast != encoderPinACurrent && encoderPinACurrent == LOW) {
  261.     if (encoderPinBCurrent != encoderPinACurrent) {
  262.       encRight = true;
  263.     } else {
  264.       encLeft = true;
  265.     }
  266.   }
  267.   encoderPinALast = encoderPinACurrent;
  268.  
  269.   // if LCD refreshing was requested, proceed
  270.   if(regenerateLcdRequired) {
  271.     regenerateLcd();
  272.   }
  273.  
  274.   // if button or encoder was used, handle click or menu option change
  275.   if(button || encLeft || encRight) {
  276.     // exclude few screens from encoder move (arrow up/down) handler
  277.     bool disableEncoder;
  278.     if(
  279.       currentScreen == 11 ||  // show current pos. screen
  280.       currentScreen == 12 ||  // show point A pos. screen
  281.       currentScreen == 13 ||  // show point B pos. screen
  282.       currentScreen == 40 ||  // set time screen
  283.       currentScreen == 210 || // set slide of A screen
  284.       currentScreen == 211 || // set pan/tilt of A screen
  285.       currentScreen == 220 || // set slide of B screen
  286.       currentScreen == 221    // set pan/tilt of B screen
  287.     ) {
  288.       disableEncoder = true;
  289.     } else {
  290.       disableEncoder = false;
  291.     }
  292.     if(button) {
  293.       handleEnterClick();
  294.       delay(400); // anti double-click
  295.    } else if(encLeft && !disableEncoder) {
  296.      if(currentOptionSelected > 0) {
  297.        currentOptionSelected--;
  298.        regenerateLcdRequired = true;
  299.        regenerateLcdMode = 2;
  300.      }
  301.    } else if(encRight && !disableEncoder) {
  302.      if(currentOptionSelected < 3) {
  303.        currentOptionSelected++;
  304.        regenerateLcdRequired = true;
  305.        regenerateLcdMode = 2;
  306.      }
  307.    } else if(statsMode = 4) {
  308.      //handle encoder move for setting movement time
  309.      if(encLeft && globalTime > 15) {
  310.        globalTime--;
  311.      } else if(encRight && globalTime < 600) {
  312.        globalTime++;
  313.      }
  314.      statsDisplayed = false; // set to let stats screen render again with new time
  315.    }
  316.   }
  317.  
  318.   // if manual move flag is set and joystick is in sure, proceed with movement
  319.   if(moveMode > 0) {
  320.     if (
  321.       (moveMode == 1 && (joyLeftValue > 0 || joyRightValue > 0)) ||
  322.       (moveMode == 2 && (joyLeftValue > 0 || joyRightValue > 0 || joyUpValue > 0 || joyDownValue > 0))
  323.     ) {
  324.       handleManualMove(joyLeftValue, joyRightValue, joyUpValue, joyDownValue);
  325.     } else if(nextStatRefresh < millis()) {
  326.       lcd.setCursor(0,1);
  327.       lcd.print("Slide " + String(globalSlide) + "     ");
  328.       lcd.setCursor(0,2);
  329.       lcd.print("Pan   " + String(globalPan) + "     ");
  330.       lcd.setCursor(0,3);
  331.       lcd.print("Tilt  " + String(globalTilt) + "     ");
  332.       nextStatRefresh = millis() + 300;
  333.     }
  334.   }
  335.  
  336.   if(statsMode > 0 && !statsDisplayed) {
  337.     lcd.clear();
  338.     if(statsMode == 1) {
  339.       lcd.setCursor(0,0);
  340.       lcd.print("Current position");
  341.       lcd.setCursor(0,1);
  342.       lcd.print("Slide " + String(globalSlide));
  343.       lcd.setCursor(0,2);
  344.       lcd.print("Pan   " + String(globalPan));
  345.       lcd.setCursor(0,3);
  346.       lcd.print("Tilt  " + String(globalTilt));
  347.     } else if(statsMode == 2) {
  348.       lcd.setCursor(0,0);
  349.       lcd.print("Point A position");
  350.       lcd.setCursor(0,1);
  351.       lcd.print("Slide " + String(pointAS));
  352.       lcd.setCursor(0,2);
  353.       lcd.print("Pan   " + String(pointAP));
  354.       lcd.setCursor(0,3);
  355.       lcd.print("Tilt  " + String(pointAT));
  356.     } else if(statsMode == 3) {
  357.       lcd.setCursor(0,0);
  358.       lcd.print("Point B position");
  359.       lcd.setCursor(0,1);
  360.       lcd.print("Slide " + String(pointBS));
  361.       lcd.setCursor(0,2);
  362.       lcd.print("Pan   " + String(pointBP));
  363.       lcd.setCursor(0,3);
  364.       lcd.print("Tilt  " + String(pointBT));
  365.     } else if(statsMode == 4) {
  366.       lcd.setCursor(0,0);
  367.       lcd.print("Set movement time");
  368.       lcd.setCursor(0,1);
  369.       lcd.print(String(globalTime) + " s.");
  370.     }
  371.     statsDisplayed = true; // set true so it won't be rendered again in next loop iterations
  372.   }
  373. }
  374.  
  375. void regenerateLcd() {
  376.   if(regenerateLcdMode == 0) {
  377.     currentOptionSelected = 0;
  378.     lcd.clear();
  379.     regenerateLcdMenuItems();
  380.     regenerateLcdMenuArrow();
  381.   } else if(regenerateLcdMode == 1) {
  382.     regenerateLcdMenuItems();
  383.   } else if(regenerateLcdMode == 2) {
  384.     regenerateLcdMenuArrow();
  385.   }
  386.   regenerateLcdRequired = false;
  387. }
  388.  
  389. void regenerateLcdMenuItems() {
  390.   if(currentScreen == 0) {
  391.     char menuOptions[4][18] = {
  392.       "Show current data",
  393.       "Set target points",
  394.       "Go to point",
  395.       "Set Time"
  396.     };
  397.     drawMenuOptions(menuOptions);
  398.   } else if(currentScreen == 10) {
  399.     char menuOptions[4][18] = {
  400.       "Show current pos.",
  401.       "Show point A pos.",
  402.       "Show point B pos.",
  403.       "Return"
  404.     };
  405.     drawMenuOptions(menuOptions);
  406.   } else if(currentScreen == 20) {
  407.     char menuOptions[4][18] = {
  408.       "Set point A",
  409.       "Set point B",
  410.       "",
  411.       "Return"
  412.     };
  413.     drawMenuOptions(menuOptions);
  414.   } else if(currentScreen == 21) {
  415.     char menuOptions[4][18] = {
  416.       "Set SLIDE of A",
  417.       "Set PAN/TILT of A",
  418.       "",
  419.       "Save"
  420.     };
  421.     drawMenuOptions(menuOptions);
  422.   } else if(currentScreen == 210) {
  423.     lcd.clear();
  424.     lcd.setCursor(1,0);
  425.     lcd.print("Set SLIDE of A");
  426.   } else if(currentScreen == 211) {
  427.     lcd.clear();
  428.     lcd.setCursor(1,0);
  429.     lcd.print("Set PAN/TILT of A");
  430.   } else if(currentScreen == 22) {
  431.     char menuOptions[4][18] = {
  432.       "Set SLIDE of B",
  433.       "Set PAN/TILT of B",
  434.       "",
  435.       "Save"
  436.     };
  437.     drawMenuOptions(menuOptions);
  438.   } else if(currentScreen == 220) {
  439.     lcd.clear();
  440.     lcd.setCursor(1,0);
  441.     lcd.print("Set SLIDE of B");
  442.   } else if(currentScreen == 221) {
  443.     lcd.clear();
  444.     lcd.setCursor(1,0);
  445.     lcd.print("Set PAN/TILT of B");
  446.   } else if(currentScreen == 30) {
  447.     char menuOptions[4][18] = {
  448.       "Go to point A",
  449.       "Go to point B",
  450.       "",
  451.       "Return"
  452.     };
  453.     drawMenuOptions(menuOptions);
  454.   } else if(
  455.     currentScreen == 11 ||
  456.     currentScreen == 12 ||
  457.     currentScreen == 13 ||
  458.     currentScreen == 14 ||
  459.     currentScreen == 40
  460.   ) {
  461.     // no handling here, but empty handler needed to not redrawing menu options (instruction below)
  462.   } else {
  463.     char menuOptions[4][18] = { "", "", "", ""};
  464.     drawMenuOptions(menuOptions);
  465.   }
  466. }
  467.  
  468. void regenerateLcdMenuArrow() {
  469.   for(int i=0; i<=3; i++) {
  470.     lcd.setCursor(0, i);
  471.     if(i == currentOptionSelected) {
  472.       lcd.print(">");
  473.     } else {
  474.       lcd.print(" ");
  475.     }
  476.   }
  477. }
  478.  
  479. void drawMenuOptions(char lines[4][18]) {
  480.   for (int i=0; i<=3; i++) {
  481.     lcd.setCursor(2,i);
  482.     lcd.print(String(lines[i]));
  483.   }
  484. }
  485.  
  486. void handleEnterClick() {
  487.   regenerateLcdMode = 0; // just in case if regenerate LCD will be requested
  488.   if(currentScreen == 0) {
  489.     // home screen links:
  490.     if(currentOptionSelected == 0) {
  491.       currentScreen = 10;
  492.       regenerateLcdRequired = true;
  493.     } else if(currentOptionSelected == 1) {
  494.       currentScreen = 20;
  495.       regenerateLcdRequired = true;
  496.     } else if(currentOptionSelected == 2) {
  497.       currentScreen = 30;
  498.       regenerateLcdRequired = true;
  499.     } else if(currentOptionSelected == 3) {
  500.       currentScreen = 40;
  501.       statsMode = 4;
  502.       statsDisplayed = false;
  503.     }
  504.   } else if(currentScreen == 10) {
  505.     // stats index screen
  506.     if(currentOptionSelected == 0) {
  507.       currentScreen = 11;
  508.       statsMode = 1;
  509.       statsDisplayed = false;
  510.     } else if(currentOptionSelected == 1) {
  511.       currentScreen = 12;
  512.       statsMode = 2;
  513.       statsDisplayed = false;
  514.     } else if(currentOptionSelected == 2) {
  515.       currentScreen = 13;
  516.       statsMode = 3;
  517.       statsDisplayed = false;
  518.     } else if(currentOptionSelected == 3) {
  519.       currentScreen = 0;
  520.       regenerateLcdRequired = true;
  521.     }
  522.   } else if(currentScreen == 11) {
  523.     currentScreen = 10;
  524.     statsMode = 0;
  525.     regenerateLcdRequired = true;
  526.   } else if(currentScreen == 12) {
  527.     currentScreen = 10;
  528.     statsMode = 0;
  529.     regenerateLcdRequired = true;
  530.   } else if(currentScreen == 13) {
  531.     currentScreen = 10;
  532.     statsMode = 0;
  533.     regenerateLcdRequired = true;
  534.   } else if(currentScreen == 20) {
  535.     // set point index screen
  536.     if(currentOptionSelected == 0) {
  537.       currentScreen = 21;
  538.       regenerateLcdRequired = true;
  539.     } else if(currentOptionSelected == 1) {
  540.       currentScreen = 22;
  541.       regenerateLcdRequired = true;
  542.     } else if(currentOptionSelected == 3) {
  543.       currentScreen = 0;
  544.       regenerateLcdRequired = true;
  545.     }
  546.   } else if(currentScreen == 21) {
  547.     // set point A screen
  548.     if(currentOptionSelected == 0) {
  549.       currentScreen = 210;
  550.       moveMode = 1;
  551.       regenerateLcdRequired = true;
  552.     } else if(currentOptionSelected == 1) {
  553.       currentScreen = 211;
  554.       moveMode = 2;
  555.       regenerateLcdRequired = true;
  556.     } else if(currentOptionSelected == 3) {
  557.       currentScreen = 20;
  558.       setPointA();
  559.       regenerateLcdRequired = true;
  560.     }
  561.   } else if(currentScreen == 210) {
  562.     moveMode = 0;
  563.     currentScreen = 21;
  564.     regenerateLcdRequired = true;
  565.   } else if(currentScreen == 211) {
  566.     moveMode = 0;
  567.     currentScreen = 21;
  568.     regenerateLcdRequired = true;
  569.   } else if(currentScreen == 22) {
  570.     // set point B screen
  571.     if(currentOptionSelected == 0) {
  572.       currentScreen = 220;
  573.       moveMode = 1;
  574.       regenerateLcdRequired = true;
  575.     } else if(currentOptionSelected == 1) {
  576.       currentScreen = 221;
  577.       moveMode = 2;
  578.       regenerateLcdRequired = true;
  579.     } else if(currentOptionSelected == 3) {
  580.       setPointB();
  581.       currentScreen = 20;
  582.       regenerateLcdRequired = true;
  583.     }
  584.   } else if(currentScreen == 220) {
  585.     moveMode = 0;
  586.     currentScreen = 22;
  587.     regenerateLcdRequired = true;
  588.   } else if(currentScreen == 221) {
  589.     moveMode = 0;
  590.     currentScreen = 22;
  591.     regenerateLcdRequired = true;
  592.   } else if(currentScreen == 30) {
  593.     // go to point index screen
  594.     if(currentOptionSelected == 0) {
  595.       goToPoint(0);
  596.     } else if(currentOptionSelected == 1) {
  597.       goToPoint(1);
  598.     } else if(currentOptionSelected == 3) {
  599.       currentScreen = 0;
  600.       regenerateLcdRequired = true;
  601.     }
  602.   } else if(currentScreen == 40) {
  603.     currentScreen = 0;
  604.     regenerateLcdRequired = true;
  605.   }
  606. }
  607.  
  608. void handleManualMove(int joyLeftValue, int joyRightValue, int joyUpValue, int joyDownValue) {
  609.   bool doSlideMove = false;
  610.   bool doPanMove = false;
  611.   bool doTiltMove = false;
  612.   long timeGapSlide = 0;
  613.   long timeGapPan = 0;
  614.   long timeGapTilt = 0;
  615.   bool dirS, dirP, dirT;
  616.   bool slideMoveSet = false;
  617.   bool panMoveSet = false;
  618.   bool tiltMoveSet = false;
  619.  
  620.   if(moveMode == 1 && (joyLeftValue || joyRightValue) ) {
  621.     if(joyLeftValue > 0) {
  622.       digitalWrite(dirPinS, HIGH);
  623.       dirS = 1;
  624.       slideMoveSet = true;
  625.       timeGapSlide = map(joyLeftValue, 0, 100, 2000, 40);
  626.     } else {
  627.       digitalWrite(dirPinS, LOW);
  628.       dirS = 0;
  629.       slideMoveSet = true;
  630.       timeGapSlide = map(joyRightValue, 0, 100, 2000, 40);
  631.     }
  632.     if (slideMoveSet && nextMoveSlide < micros()) {
  633.       doSlideMove = 1;
  634.       nextMoveSlide = micros() + timeGapSlide;
  635.     }
  636.   } else if(moveMode == 2) {
  637.     if(joyLeftValue > 0) {
  638.       digitalWrite(dirPinP, HIGH);
  639.       dirP = 1;
  640.       panMoveSet = true;
  641.       timeGapPan = map(joyLeftValue, 0, 100, 10000, 3000);
  642.     } else if(joyRightValue > 0) {
  643.       digitalWrite(dirPinP, LOW);
  644.       dirP = 0;
  645.       panMoveSet = true;
  646.       timeGapPan = map(joyRightValue, 0, 100, 10000, 3000);
  647.     }
  648.     if(joyUpValue > 0) {
  649.       digitalWrite(dirPinT, HIGH);
  650.       dirT = 1;
  651.       tiltMoveSet = true;
  652.       timeGapTilt = map(joyUpValue, 0, 100, 10000, 3000);
  653.     } else if(joyDownValue > 0) {
  654.       digitalWrite(dirPinT, LOW);
  655.       dirT = 0;
  656.       tiltMoveSet = true;
  657.       timeGapTilt = map(joyDownValue, 0, 100, 10000, 3000);
  658.     }
  659.     if (panMoveSet && nextMovePan < micros()) {
  660.       doPanMove = 1;
  661.       nextMovePan = micros() + timeGapPan;
  662.     }
  663.     if (tiltMoveSet && nextMoveTilt < micros()) {
  664.       doTiltMove = 1;
  665.       nextMoveTilt = micros() + timeGapTilt;
  666.     }
  667.   }
  668.  
  669.   if(doSlideMove) { digitalWrite(stepPinS, HIGH); }
  670.   if(doPanMove) { digitalWrite(stepPinP, HIGH); }
  671.   if(doTiltMove) { digitalWrite(stepPinT, HIGH); }
  672.   delayMicroseconds(10);
  673.   if(doSlideMove) {
  674.     digitalWrite(stepPinS, LOW);
  675.     if(dirS) {
  676.       globalSlide++;
  677.     } else {
  678.       globalSlide--;
  679.     }
  680.   }
  681.   if(doPanMove) {
  682.     digitalWrite(stepPinP, LOW);
  683.     if(dirP) {
  684.       globalPan++;
  685.     } else {
  686.       globalPan--;
  687.     }
  688.   }
  689.   if(doTiltMove) {
  690.     digitalWrite(stepPinT, LOW);
  691.     if(dirT) {
  692.       globalTilt++;
  693.     } else {
  694.       globalTilt--;
  695.     }
  696.   }
  697.   delayMicroseconds(10);
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement