Advertisement
creativesamurai1982

arduinoCineControler.ino

Jun 3rd, 2025
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 6.23 KB | Source Code | 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. const long CONTINUOUS_MOVE_STEPS = 999999999;
  45.  
  46. int steps = 800;  //  Number of steps to move x amount of 8mm cine film frames.
  47. /*
  48. __________________
  49. | STEPS | FRAMES |
  50. |-------|--------|
  51. |  100  |   6    |
  52. |  200  |   12   | 1
  53. |  300  |   18   |
  54. |  400  |   24   | 2
  55. |  500  |   30   |
  56. |  600  |   36   | 3
  57. |  700  |   42   |
  58. |  800  |   48   | 4
  59. |  900  |   54   |
  60. | 1000  |   60   | 5
  61. */
  62.  
  63. // Create two new instances of the AccelStepper class
  64. AccelStepper takeup(AccelStepper::DRIVER, Z_STP_PIN, Z_DIR_PIN);   // Runs all the time unless switch is pressed!
  65. AccelStepper capstan(AccelStepper::DRIVER, X_STP_PIN, X_DIR_PIN);  // Runs only when told to via serial coms
  66.  
  67. // SWITCH SETUP
  68. ezButton tensionSW(9);   // Create ezButton object that attach to pin 7;
  69. ezButton stopButton(11);  // Create ezButton object that attach to pin 3;
  70.  
  71. bool isTakeupActive = false;       // Flag to track the status of the takeup stepper
  72. bool isCapstanContinuous = false;  // Flag to indicate capstan continuous movement
  73.  
  74. void setup() {
  75.   Serial.begin(9600);  // Initialize serial communication at 9600 baud
  76.  
  77.   Serial.println("");
  78.   Serial.println("M = Move");  //
  79.   Serial.println("T = Activate Takeup Stepper");  //
  80.   Serial.println("O = De-activate Takeup Stepper");  //
  81.   Serial.println("X = enable capstan continuous movement");  //
  82.   Serial.println("");
  83.  
  84.   tensionSW.setDebounceTime(50);  // set debounce time to 50 milliseconds
  85.  
  86.   takeup.setMaxSpeed(takeupMaxSpeed);   // Set the maximum speed of the stepper motor
  87.   takeup.setSpeed(takeupSpeed);         // Set the speed of the stepper motor
  88.   takeup.setAcceleration(takeupAccel);  // Set the acceleration of the stepper motor
  89.  
  90.   capstan.setMaxSpeed(capstanMaxSpeed);   // Set the maximum speed of the stepper motor
  91.   capstan.setSpeed(capstanSpeed);         // Set the speed of the stepper motor
  92.   capstan.setAcceleration(capstanAccel);  // Set the acceleration of the stepper motor
  93.  
  94.   takeup.move(CONTINUOUS_MOVE_STEPS);  // * Tells the Takeup stepper to to move x steps
  95.                            // * when takeup.run() is called in the loop.
  96. }
  97.  
  98. void loop() {
  99.   serialCheck();
  100.   tensionSW.loop();  // Checks that status of the switch
  101.   stopButton.loop();
  102.   switchReleased();  // code for when the switch is released
  103.   switchPressed();   // code for when the switch is pressed
  104.  
  105.   if (isTakeupActive) {
  106.     takeup.run();
  107.   }
  108.  
  109.   if (isCapstanContinuous) {
  110.     capstan.move(steps * microSteps);
  111.     capstan.run();
  112.     if (stopButton.isPressed()) {
  113.       isCapstanContinuous = false;  // Stop the capstan continuous movement
  114.       capstan.stop();
  115.       Serial.println("Capstan movement stopped.");
  116.     }
  117.   } else {
  118.     capstan.run();
  119.   }
  120. }
  121.  
  122. void serialCheck() {
  123.   // Note: we shall process one command at a time! This means: reset the
  124.   // following variable to zero as soon as the command is done processing.
  125.   static char pendingCommand = 0;
  126.  
  127.   // Do not proceed with another command until we're done with the current one!
  128.   if (pendingCommand == 0) {
  129.     if (Serial.available() > 0) {
  130.       pendingCommand = Serial.read();
  131.  
  132.       // Ignore carriage return and newline characters.
  133.       if (pendingCommand == '\r' || pendingCommand == '\n') {
  134.         pendingCommand = 0;
  135.         return;
  136.       }
  137.      
  138.       // Perform a single action upon receiving a command. Makes code easier to read.
  139.       switch (pendingCommand) {
  140.         case 'M':
  141.           capstan.move(steps * microSteps);
  142.           break;   // Move the motor 100 steps
  143.         case 'T':  // Activate the takeup stepper
  144.           isTakeupActive = true;
  145.           break;
  146.         case 'O':  // De-activate the takeup stepper
  147.           isTakeupActive = false;
  148.           break;
  149.         case 'X':  // enable capstan continuous movement
  150.           isCapstanContinuous = true;
  151.           break;
  152.         default:
  153.           Serial.print("Unknown command: ");
  154.           Serial.println(pendingCommand);
  155.           pendingCommand = 0;
  156.           break;  // Ignore unknown commands
  157.       }
  158.     }
  159.   } else {
  160.     // Send a message when the motor is done moving:
  161.     if (capstan.isRunning() == false) {
  162.       pendingCommand = 0;  // Reset the pendingCommand
  163.       Serial.println("S");  // Send message "We're done!"
  164.     }
  165.   }
  166. }
  167.  
  168. void switchReleased() {
  169.  
  170.   if (tensionSW.isReleased()) {
  171.     Serial.println("MOVING - Trigger Released");
  172.     takeup.move(CONTINUOUS_MOVE_STEPS);
  173.   }
  174. }
  175.  
  176. void switchPressed() {
  177.   if (tensionSW.isPressed()) {
  178.     Serial.println("STOPPED - Trigger Pressed");
  179.     takeup.stop();
  180.   }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement