Advertisement
creativesamurai1982

PJ01_Arduino_pyCineScan.ino

Mar 6th, 2024
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | Software | 0 0
  1. /*
  2. Pins for the CNC Shield v3
  3.  
  4.  X_STP_PIN           2
  5.  Y_STP_PIN           3
  6.  Z_STP_PIN           4
  7.  
  8.  X_DIR_PIN           5
  9.  Y_DIR_PIN           6
  10.  Z_DIR_PIN           7
  11.  
  12.  SHIELD_EN_PIN       8 pos useage for turning steppers off for realligment
  13. */
  14.  
  15.  
  16. // INCLUDES
  17. #include <AccelStepper.h>
  18. #include <ezButton.h>
  19.  
  20. // Define the stepper motor connections
  21. const int Z_DIR_PIN = 7;   //  Z Takeup Reels Direction Pin
  22. const int Z_STP_PIN = 4;  //  Z Takeup Reels Step Pin
  23. const int X_DIR_PIN = 5;   //  X Capstans Direction Pin
  24. const int X_STP_PIN = 2;  //  X Capstans Step Pin
  25.  
  26. const int stepsPerRev = 200;   // Number of steps for a full rotation(360) // SPROCKET(CAPSTAN)
  27. const int stepsPerRev2 = 200;  // Number of steps for a full rotation(360) // Takeup Reel
  28. const int microSteps = 16;     // 16th microstepping
  29. //const int takeupMaxSpeed = 250;     // Takeup Reel Max Speed
  30. //const int takeupSpeed = 250;        // Takeup Reel Speed
  31. //const int capstanMaxSpeed = 600;    // Capstan Reel Max Speed
  32. //const int capstanSpeed = 600;       // Capstan Reel Speed 120
  33. //const int capstanAccel = 99999999;  // * Sets the steppers acceleration to a high number so that the stepper
  34. //const int takeupAccel = 99999999;   // * number so that the stepper Doesn't acceralate. 99999999
  35.  
  36. const int takeupMaxSpeed = 450;   // Takeup Reel Max Speed
  37. const int takeupSpeed = 450;      // Takeup Reel Speed
  38. const int capstanMaxSpeed = 900;  // Capstan Reel Max Speed
  39. const int capstanSpeed = 900;     // Capstan Reel Speed 120
  40.  
  41. const int capstanAccel = 800;      // * Sets the steppers acceleration to a high number so that the stepper
  42. const int takeupAccel = 99999999;  // * number so that the stepper Doesn't acceralate. 99999999
  43.  
  44. int steps = 800;  //  Number of steps to move x amount of 8mm cine film frames.
  45. /*
  46. __________________
  47. | STEPS | FRAMES |
  48. |-------|--------|
  49. |  100  |   6    |
  50. |  200  |   12   | 1
  51. |  300  |   18   |
  52. |  400  |   24   | 2
  53. |  500  |   30   |
  54. |  600  |   36   | 3
  55. |  700  |   42   |
  56. |  800  |   48   | 4
  57. |  900  |   54   |
  58. | 1000  |   60   | 5
  59. */
  60.  
  61. // Create two new instances of the AccelStepper class
  62. AccelStepper takeup(AccelStepper::DRIVER, Z_STP_PIN, Z_DIR_PIN);   // Runs all the time unless switch is pressed!
  63. AccelStepper capstan(AccelStepper::DRIVER, X_STP_PIN, X_DIR_PIN);  // Runs only when told to via serial coms
  64.  
  65. // SWITCH SETUP
  66. ezButton tensionSW(9);   // Create ezButton object that attach to pin 7;
  67. ezButton stopButton(11);  // Create ezButton object that attach to pin 3;
  68.  
  69. bool isTakeupActive = false;       // Flag to track the status of the takeup stepper
  70. bool isCapstanContinuous = false;  // Flag to indicate capstan continuous movement
  71.  
  72. void setup() {
  73.   Serial.begin(9600);  // Initialize serial communication at 9600 baud
  74.  
  75.   Serial.println("");
  76.   Serial.println("M = Move");  //
  77.   Serial.println("T = Activate Takeup Stepper");  //
  78.   Serial.println("X = enable capstan continuous movement");  //
  79.   Serial.println("");
  80.  
  81.   tensionSW.setDebounceTime(50);  // set debounce time to 50 milliseconds
  82.  
  83.   takeup.setMaxSpeed(takeupMaxSpeed);   // Set the maximum speed of the stepper motor
  84.   takeup.setSpeed(takeupSpeed);         // Set the speed of the stepper motor
  85.   takeup.setAcceleration(takeupAccel);  // Set the acceleration of the stepper motor
  86.  
  87.   capstan.setMaxSpeed(capstanMaxSpeed);   // Set the maximum speed of the stepper motor
  88.   capstan.setSpeed(capstanSpeed);         // Set the speed of the stepper motor
  89.   capstan.setAcceleration(capstanAccel);  // Set the acceleration of the stepper motor
  90.  
  91.   takeup.move(999999999);  // * Tells the Takeup stepper to to move x steps
  92.                            // * when takeup.run() is called in the loop.
  93. }
  94.  
  95. void loop() {
  96.   serialCheck();
  97.   tensionSW.loop();  // Checks that status of the switch
  98.   stopButton.loop();
  99.   switchReleased();  // code for when the switch is released
  100.   switchPressed();   // code for when the switch is pressed
  101.  
  102.   if (isTakeupActive) {
  103.     takeup.run();
  104.   }
  105.  
  106.   if (isCapstanContinuous) {
  107.     capstan.move(steps * microSteps);
  108.     capstan.run();
  109.     if (stopButton.isPressed()) {
  110.       isCapstanContinuous = false;  // Stop the capstan continuous movement
  111.       capstan.stop();
  112.       Serial.println("Capstan movement stopped.");
  113.     }
  114.   } else {
  115.     capstan.run();
  116.   }
  117. }
  118.  
  119. void serialCheck() {
  120.   // Note: we shall process one command at a time! This means: reset the
  121.   // following variable to zero as soon as the command is done processing.
  122.   static char current_command;
  123.  
  124.   // Do not proceed with another command until we're done with the current one!
  125.   if (current_command == 0) {
  126.     if (Serial.available() > 0) {
  127.       current_command = Serial.read();
  128.       // Perform a single action upon receiving a command. Makes code easier to read.
  129.       switch (current_command) {
  130.         default:
  131.           current_command = 0;
  132.           break;  // Ignore unknown commands
  133.         case 'M':
  134.           capstan.move(steps * 16);
  135.           break;   // Move the motor 100 steps
  136.         case 'T':  // Activate the takeup stepper
  137.           isTakeupActive = true;
  138.           break;
  139.         case 'A':  // Activate the takeup stepper
  140.           isTakeupActive = false; //not sure this is working!
  141.           break;
  142.         case 'X':  // enable capstan continuous movement
  143.           isCapstanContinuous = true;
  144.           break;
  145.       }
  146.     }
  147.   } else {
  148.     // Send a message when the motor is done moving:
  149.     if (capstan.isRunning() == false) {
  150.       current_command = 0;  // Reset the current_command
  151.       Serial.println("S");  // Send message "We're done!"
  152.     }
  153.   }
  154. }
  155.  
  156. void switchReleased() {
  157.  
  158.   if (tensionSW.isReleased()) {
  159.     Serial.println("MOVING - Trigger Released");
  160.     takeup.move(999999999);
  161.   }
  162. }
  163.  
  164. void switchPressed() {
  165.   if (tensionSW.isPressed()) {
  166.     Serial.println("STOPPED - Trigger Pressed");
  167.     takeup.stop();
  168.   }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement