Advertisement
Guest User

Untitled

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