Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 13.16 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3.  
  4. //#include <LiquidCrystal.h>
  5.  
  6. #include <Stepper.h>
  7.  
  8. #include <Servo.h>
  9.  
  10. #include <LiquidCrystal_I2C.h>
  11.  
  12. //LiquidCrystal lcd(1, 2, 4, 5, 6, 7);  // Use these pins for the 1602 lcd
  13.  
  14. LiquidCrystal_I2C lcd(0x27,20,4);
  15.  
  16. const int SW_pin = 8; // digital pin connected to switch output
  17.  
  18. const int X_pin = A0; // analog pin connected to X output
  19.  
  20. const int Y_pin = A1; // analog pin connected to Y output
  21.  
  22. int MenuNr = 0;   // Menu number
  23.  
  24. int PhotoNr = 2;  // The amount of photos that have to be taken
  25.  
  26. bool Flag1 = 0;   // This flag is only active during 1 program cycle (prevents constantly adding/subtracting 1 to the menu number when the joystick is pushed to the side)
  27.  
  28. bool Flag2 = 0;   // This flag is only active during 1 program cycle (prevents constantly adding/subtracting 2 to the photo number when the joystick is pushed up or down)
  29.  
  30. bool Flag3 = 0;   // This flag is only active during 1 program cycle (prevents constantly adding/subtracting 1 to the RPM when the joystick is pushed up or down)
  31.  
  32. bool Flag4 = 0;   // This flag is only active during 1 program cycle (prevents constantly adding/subtracting 1 to the turn number when the joystick is pushed to the side)
  33.  
  34. bool Flag5 = 0;   // This flag is only active during 1 program cycle (prevents constantly adding/subtracting 1 to the RPM when the joystick is pushed up or down)
  35.  
  36. bool Flag6 = 0;   // This flag is only active during 1 program cycle to clear the lcd
  37.  
  38. int SwMenu = 0;   // Switch menu (Sub menu's in the main menu's)
  39.  
  40. bool BtnFlag = 0; // This flag is only active during 1 program cycle (prevents constantly adding of 1 to SwMenu when button is pressed)
  41.  
  42. const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
  43.  
  44. int FullRev = 14336;                  // 1 full revolution of the big gear -> Small-Big gear ratio is 7:1
  45.  
  46. int rolePerMinute = 15;               // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
  47.  
  48. int PhotoTaken = 0;                   // Amount of photo's that have been taken
  49.  
  50. int StepPerPhoto;                     // Amount of steps per photo (calculated -> see MenuNr 0/SwMenu 2)
  51.  
  52. int TurnNr = 1;                       // Amount of turns
  53.  
  54. int CurrentTurn = 0;                  // Stores current turn number
  55.  
  56. int Steps = 0;                        // Amount of individual steps the stepper motor has to turn
  57.  
  58. Stepper myStepper(stepsPerRevolution, 9, 11, 10, 12);  // Use these pins for the stepper motor
  59.  
  60. Servo Servo1;  // Define Servo1 as a servo
  61.  
  62. void setup() {
  63.  
  64.   lcd.begin(16, 2);                   //Lcd setup
  65.  
  66.   pinMode(SW_pin, INPUT);             //Set pushbutton as input
  67.  
  68.   digitalWrite(SW_pin, HIGH);         //Set SW_pin High
  69.  
  70.  
  71.  
  72.   myStepper.setSpeed(rolePerMinute);  //Set RPM of steppermotor
  73.  
  74.   Servo1.attach(3);                   //Attach servo to pin 3
  75.  
  76.   Servo1.write(90);                   //Move servo to mid possition
  77.  
  78.   lcd.init();                      // initialize the lcd
  79.  
  80.   lcd.init();
  81.  
  82.   lcd.backlight();
  83.  
  84.   lcd.setCursor(4, 0);                //Startup screen start
  85.  
  86.   lcd.print("Welcome!");              //      """""      //
  87.  
  88.   lcd.setCursor(1, 1);                //      """""      //
  89.  
  90.   lcd.print("Software: V1.6");        //      """""      //
  91.  
  92.   delay(3000);                        //      """""      //
  93.  
  94.   lcd.clear();                        //      """""      //
  95.  
  96.   lcd.setCursor(0, 0);                //      """""      //
  97.  
  98.   lcd.print("Designed by");           //      """""      //
  99.  
  100.   lcd.setCursor(0, 1);                //      """""      //
  101.  
  102.   lcd.print("Brian Brocken");         //      """""      //
  103.  
  104.   delay(2000);                        //      """""      //
  105.  
  106.   lcd.clear();                        //Startup screen end
  107.  
  108. }
  109.  
  110. void loop() {
  111.  
  112.   int XValue = analogRead(X_pin);     // Read the analog value from The X-axis from the joystick
  113.  
  114.   int YValue = analogRead(Y_pin);     // Read the analog value from The Y-axis from the joystick
  115.  
  116.   int SwValue = digitalRead(SW_pin);  // Read the digital value from The Button from the joystick
  117.  
  118.   if (MenuNr < 0){  //This sets the min number of menu's
  119.  
  120.     MenuNr = 0;
  121.  
  122.   }
  123.  
  124.   else if ( MenuNr > 2){  //This sets the max numbers of menu's
  125.  
  126.     MenuNr = 2;
  127.  
  128.   }
  129.  
  130.  
  131.  
  132.   if (XValue < 400 && Flag1 == 0 && SwMenu == 0){  //if the joystick is pushed to the right side and flag1 is 0 then 1 will be added to the menu number (purpose of the flag -> see comment Flags above)
  133.  
  134.     MenuNr = MenuNr + 1;
  135.  
  136.     Flag1 = 1;
  137.  
  138.     lcd.clear();
  139.  
  140.   }
  141.  
  142.   if (XValue > 600 && Flag1 == 0 && SwMenu == 0){  //if the joystick is pushed to the left side and flag1 is 0 then 1 will be subtracted from the menu number (purpose of the flag -> see comment Flags above)
  143.  
  144.     MenuNr = MenuNr - 1;
  145.  
  146.     Flag1 = 1;
  147.  
  148.     lcd.clear();
  149.  
  150.   }
  151.  
  152.  
  153.  
  154.   if (XValue > 399 && XValue < 599 && Flag1 == 1){  //if joystick is at neutral possition, flag1 is set to 0 (purpose of the flag -> see comment Flags above)
  155.  
  156.     Flag1 = 0;
  157.  
  158.   }
  159.  
  160.   if (SwValue == 0 && BtnFlag == 0){  //if the button is pressed and the flag is 0 -> add 1 to menu
  161.  
  162.     SwMenu = SwMenu + 1;
  163.  
  164.     BtnFlag = 1;
  165.  
  166.     lcd.clear();
  167.  
  168.   }
  169.  
  170.   if (SwValue == 1 && BtnFlag == 1){  //if the button is not pressed and the flag is 0 -> Reset the flag (purpose of the flag -> see comment Flags above)
  171.  
  172.     BtnFlag = 0;
  173.  
  174.   }
  175.  
  176.  
  177.  
  178. //***********************************************Menu0***********************************************//
  179.  
  180.   if (MenuNr == 0){  //Menu0 program
  181.  
  182.     if (SwMenu == 0){ //Menu 0 selected
  183.  
  184.       lcd.setCursor(0, 0);
  185.  
  186.       lcd.print("Photogrametry");
  187.  
  188.     }
  189.  
  190.  
  191.  
  192.     if (SwMenu == 1){ //entered menu 0
  193.  
  194.       lcd.setCursor(0, 0);
  195.  
  196.       lcd.print("Nr of photo's");
  197.  
  198.       lcd.setCursor(7, 1);
  199.  
  200.       lcd.print(PhotoNr);
  201.  
  202.      
  203.  
  204.       if (YValue < 400 && Flag2 == 0){ //joystick up -> Add 2 to number of photo's
  205.  
  206.         PhotoNr = PhotoNr + 2;
  207.  
  208.         Flag2 = 1;
  209.  
  210.         lcd.clear();
  211.  
  212.       }
  213.  
  214.       if (YValue > 600 && Flag2 == 0){ //joystick down -> Subtract 2 from number of photo's
  215.  
  216.         PhotoNr = PhotoNr - 2;
  217.  
  218.         Flag2 = 1;
  219.  
  220.         lcd.clear();
  221.  
  222.       }
  223.  
  224.       if (YValue > 399 && YValue < 599 && Flag2 == 1){  //if the Y-axis is back at it's neutral possition -> Flag3 = 0 -> Prevents constant adding or subtracting of 2
  225.  
  226.         Flag2 = 0;
  227.  
  228.       }
  229.  
  230.       if (PhotoNr < 2){    //Min allowable Nr of photo's
  231.  
  232.         PhotoNr = 2;
  233.  
  234.       }
  235.  
  236.       if (PhotoNr > 200){  //Max allowable Nr of photo's
  237.  
  238.         PhotoNr = 200;
  239.  
  240.       }
  241.  
  242.     }
  243.  
  244.     if (SwMenu == 2){ //Program started
  245.  
  246.      
  247.  
  248.       lcd.setCursor(0, 0);
  249.  
  250.       lcd.print("Program started");
  251.  
  252.       lcd.setCursor(0, 1);
  253.  
  254.       lcd.print("Photo Nr: ");
  255.  
  256.       lcd.print(PhotoTaken);
  257.  
  258.      
  259.  
  260.       StepPerPhoto = FullRev / PhotoNr;  //Calculate amount of steps per photo
  261.  
  262.       if (PhotoTaken < PhotoNr){          
  263.  
  264.       myStepper.setSpeed(rolePerMinute);  //Set motor speed
  265.  
  266.       myStepper.step(StepPerPhoto);       //move the calculated amount of steps
  267.  
  268.       PhotoTaken = PhotoTaken + 1;        //Add 1 to photos taken
  269.  
  270.       lcd.setCursor(0, 1);
  271.  
  272.       lcd.print("Photo Nr: ");            //Taking photo's
  273.  
  274.       lcd.print(PhotoTaken);
  275.  
  276.       Servo1.write(30);
  277.  
  278.       delay(300);
  279.  
  280.       Servo1.write(90);
  281.  
  282.       delay(1000);
  283.  
  284.       }
  285.  
  286.       if (PhotoTaken == PhotoNr){  //If the amount of photos taken is equal to the amount of photos that have to be taken -> Program finished
  287.  
  288.         lcd.setCursor(0, 0);
  289.  
  290.         lcd.print("Program finished");
  291.  
  292.         delay(3000);
  293.  
  294.         lcd.clear();  //Rest parameters
  295.  
  296.         PhotoTaken = 0;
  297.  
  298.         PhotoNr = 2;
  299.  
  300.         SwMenu = 0;
  301.  
  302.         Steps = 0;
  303.  
  304.       }
  305.  
  306.     }
  307.  
  308.   }
  309.  
  310. //***********************************************Menu1***********************************************//
  311.  
  312.   if (MenuNr == 1){  //Menu1 program
  313.  
  314.    
  315.  
  316.     if (SwMenu == 0){  //Menu1 selected
  317.  
  318.       lcd.setCursor(0, 0);
  319.  
  320.       lcd.print("Cinematic");
  321.  
  322.     }
  323.  
  324.     if (SwMenu == 1){  //Entered menu1 - sub menu1
  325.  
  326.       lcd.setCursor(0, 0);
  327.  
  328.       lcd.print("motor speed");
  329.  
  330.       lcd.setCursor(7, 1);
  331.  
  332.       lcd.print(rolePerMinute);
  333.  
  334.       if (YValue < 400 && Flag3 == 0){ // joystick up -> Add 1 RPM
  335.  
  336.         rolePerMinute = rolePerMinute + 1;
  337.  
  338.         Flag3 = 1;
  339.  
  340.         lcd.clear();
  341.  
  342.       }
  343.  
  344.       if (YValue > 600 && Flag3 == 0){ // joystick down -> Subtract 1 RPM
  345.  
  346.         rolePerMinute = rolePerMinute - 1;
  347.  
  348.         Flag3 = 1;
  349.  
  350.         lcd.clear();
  351.  
  352.       }
  353.  
  354.       if (YValue > 399 && YValue < 599 && Flag3 == 1){  //if the Y-axis is back at it's neutral possition -> Flag3 = 0 -> Prevents constant adding or subtracting of 1
  355.  
  356.         Flag3 = 0;
  357.  
  358.       }
  359.  
  360.       if (rolePerMinute < 1){  //Min allowable RPM
  361.  
  362.         rolePerMinute = 1;
  363.  
  364.       }
  365.  
  366.       if (rolePerMinute > 17){  //Max allowable RPM
  367.  
  368.         rolePerMinute = 17;
  369.  
  370.       }
  371.  
  372.     }
  373.  
  374.     if (SwMenu == 2){  //Entered menu1 - sub menu2
  375.  
  376.       lcd.setCursor(0, 0);
  377.  
  378.       lcd.print("Nr of turns");
  379.  
  380.       lcd.setCursor(7, 1);
  381.  
  382.       lcd.print(TurnNr);
  383.  
  384.       if (YValue < 400 && Flag4 == 0){ // joystick up -> Add 1 turn
  385.  
  386.         TurnNr = TurnNr + 1;
  387.  
  388.         Flag4 = 1;
  389.  
  390.         lcd.clear();
  391.  
  392.       }
  393.  
  394.       if (YValue > 600 && Flag4 == 0){ // joystick down -> Subtract 1 turn
  395.  
  396.         TurnNr = TurnNr - 1;
  397.  
  398.         Flag4 = 1;
  399.  
  400.         lcd.clear();
  401.  
  402.       }
  403.  
  404.       if (YValue > 399 && YValue < 599 && Flag4 == 1){  //if the Y-axis is back at it's neutral possition -> Flag3 = 0 -> Prevents constant adding or subtracting of 1
  405.  
  406.         Flag4 = 0;
  407.  
  408.       }
  409.  
  410.       if (TurnNr < 1){  //Min allowable amount of turns
  411.  
  412.         TurnNr = 1;
  413.  
  414.       }
  415.  
  416.       if (TurnNr > 200){  //Max allowable amount of turns
  417.  
  418.         TurnNr = 200;
  419.  
  420.       }
  421.  
  422.     }
  423.  
  424.     if (SwMenu == 3){  //Program started
  425.  
  426.       lcd.setCursor(0, 0);
  427.  
  428.       lcd.print("Program started");
  429.  
  430.       lcd.setCursor(0, 1);
  431.  
  432.       lcd.print("Turns done: ");
  433.  
  434.       lcd.print(CurrentTurn);
  435.  
  436.       if (CurrentTurn < TurnNr){
  437.  
  438.         myStepper.setSpeed(rolePerMinute);
  439.  
  440.         myStepper.step(FullRev);
  441.  
  442.         CurrentTurn = CurrentTurn + 1;
  443.  
  444.         lcd.setCursor(0, 1);
  445.  
  446.         lcd.print("Turns done: ");
  447.  
  448.         lcd.print(CurrentTurn);
  449.  
  450.       }
  451.  
  452.       if (CurrentTurn == TurnNr){  //If the current turn is equal to the amount of thurns that need to be turned -> program finished
  453.  
  454.         lcd.setCursor(0, 0);
  455.  
  456.         lcd.print("Program finished");
  457.  
  458.         delay(3000);
  459.  
  460.         lcd.clear();  //Reset
  461.  
  462.         CurrentTurn = 0;
  463.  
  464.         PhotoNr = 1;
  465.  
  466.         rolePerMinute = 15;
  467.  
  468.         SwMenu = 0;
  469.  
  470.         Steps = 0;
  471.  
  472.       }
  473.  
  474.      
  475.  
  476.     }
  477.  
  478.   }
  479.  
  480. //***********************************************Menu2***********************************************//
  481.  
  482.   if (MenuNr == 2){  //Menu2 selected
  483.  
  484.    
  485.  
  486.     if (SwMenu == 0){
  487.  
  488.       lcd.setCursor(0, 0);
  489.  
  490.       lcd.print("Manual control");
  491.  
  492.     }
  493.  
  494.      if (SwMenu == 1){  //Entered menu2
  495.  
  496.       lcd.setCursor(0, 0);
  497.  
  498.       lcd.print("motor speed");
  499.  
  500.       lcd.setCursor(7, 1);
  501.  
  502.       lcd.print(rolePerMinute);
  503.  
  504.       if (YValue < 400 && Flag5 == 0){ // joystick up -> Add 1 RPM
  505.  
  506.         rolePerMinute = rolePerMinute + 1;
  507.  
  508.         Flag5 = 1;
  509.  
  510.         lcd.clear();
  511.  
  512.       }
  513.  
  514.       if (YValue > 600 && Flag5 == 0){ // joystick down -> Subtract 1 RPM
  515.  
  516.         rolePerMinute = rolePerMinute - 1;
  517.  
  518.         Flag5 = 1;
  519.  
  520.         lcd.clear();
  521.  
  522.       }
  523.  
  524.       if (YValue > 399 && YValue < 599 && Flag5 == 1){  //if the Y-axis is back at it's neutral possition -> Flag3 = 0 -> Prevents constant adding or subtracting of 1
  525.  
  526.         Flag5 = 0;
  527.  
  528.       }
  529.  
  530.       if (rolePerMinute < 1){  //Min allowable RPM
  531.  
  532.         rolePerMinute = 1;
  533.  
  534.       }
  535.  
  536.       if (rolePerMinute > 17){  //Max allowable RPM
  537.  
  538.         rolePerMinute = 17;
  539.  
  540.       }
  541.  
  542.       if (XValue < 400 ){  //if the joystick is pushed to the right side and the neutral flag is 0 then 1 will be added to the menu number (purpose of the flag -> see comment Flag1 above)
  543.  
  544.         Steps = Steps + 1;
  545.  
  546.         myStepper.setSpeed(rolePerMinute);
  547.  
  548.         myStepper.step(Steps);
  549.  
  550.         lcd.setCursor(14, 1);
  551.  
  552.         lcd.print("->");
  553.  
  554.         Flag6 = 1;
  555.  
  556.       }
  557.  
  558.       if (XValue > 600 ){  //if the joystick is pushed to the left side and the neutral flag is 0 then 1 will be subtracted from the menu number (purpose of the flag -> see comment Flag1 above)
  559.  
  560.         Steps = Steps + 1;
  561.  
  562.         myStepper.setSpeed(rolePerMinute);
  563.  
  564.         myStepper.step(-Steps);
  565.  
  566.         lcd.setCursor(0, 1);
  567.  
  568.         lcd.print("<-");
  569.  
  570.         Flag6 = 1;
  571.  
  572.       }
  573.  
  574.       if (XValue > 399 && XValue < 599){  //if the Y-axis is back at it's neutral possition -> Flag3 = 0 -> Prevents constant adding or subtracting of 1
  575.  
  576.         Steps = 0;
  577.  
  578.         if (Flag6 == 1){
  579.  
  580.           lcd.clear();
  581.  
  582.           Flag6 = 0;  //This flag is only active during 1 program cycle to clear the lcd
  583.  
  584.         }
  585.  
  586.       }
  587.  
  588.      }
  589.  
  590.      if (SwMenu == 2){  //Leave menu 2 when button is pressed
  591.  
  592.        lcd.clear();
  593.  
  594.        CurrentTurn = 0;
  595.  
  596.        PhotoNr = 1;
  597.  
  598.        rolePerMinute = 15;
  599.  
  600.        SwMenu = 0;
  601.  
  602.        Steps = 0;
  603.  
  604.      }
  605.  
  606.   }
  607.  
  608. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement