Advertisement
pleasedontcode

LinearRailSlide rev_68

Nov 29th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: LinearRailSlide
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-11-29 21:44:33
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /********* User code review feedback **********
  20. #### Feedback 1 ####
  21. - Let's consider length of 200mm and steps per revolution.
  22. ********* User code review feedback **********/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <AccelStepper.h>
  27.  
  28. /****** SYSTEM REQUIREMENT 1 *****/
  29. /* A stepper motor drives a 200 mm length linear rail */
  30. /* slide. The motor performs 100 steps per */
  31. /* revolution. Each revolution moves the slide 10mm. */
  32. /*
  33.    Here, we assume that the stepper motor is properly configured
  34.    to achieve this requirement. Hence, no changes are required in the code.
  35. */
  36.  
  37. /****** SYSTEM REQUIREMENT 2 *****/
  38. /* Perform a complete continuous travel from right to */
  39. /* left in 12 hours. Then go back from left to right */
  40. /* again in 12 hours. The movement shall be */
  41. /* continuous in 12 hours. Do not use delay function. */
  42. /*
  43.    In order to achieve this requirement, we need to modify
  44.    the loop function to continuously run the stepper motor
  45.    for 12 hours in one direction, then reverse the direction
  46.    and run for another 12 hours. We can use millis() function
  47.    to track the time and control the motor movement.
  48. */
  49.  
  50. /****** FUNCTION PROTOTYPES *****/
  51. void setup(void);
  52. void loop(void);
  53.  
  54. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  55. const uint8_t stepper_A4988StepperMotorDriver_STEP_PIN_D8 = 8;
  56. const uint8_t stepper_A4988StepperMotorDriver_DIR_PIN_D9 = 9;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  59. AccelStepper stepper(AccelStepper::FULL4WIRE, stepper_A4988StepperMotorDriver_STEP_PIN_D8, stepper_A4988StepperMotorDriver_DIR_PIN_D9);
  60.  
  61. // Variables for tracking time and movement direction
  62. unsigned long startTime;
  63. bool forwardDirection = true;
  64.  
  65. void setup(void)
  66. {
  67.   // put your setup code here, to run once:
  68.  
  69.   // Set the step and direction pins as output
  70.   pinMode(stepper_A4988StepperMotorDriver_STEP_PIN_D8, OUTPUT);
  71.   pinMode(stepper_A4988StepperMotorDriver_DIR_PIN_D9, OUTPUT);
  72.  
  73.   // Set the maximum speed and initial speed of the stepper motor
  74.   stepper.setMaxSpeed(1000);
  75.   stepper.setSpeed(50);
  76.  
  77.   // Initialize start time
  78.   startTime = millis();
  79. }
  80.  
  81. void loop(void)
  82. {
  83.   // put your main code here, to run repeatedly:
  84.  
  85.   // Calculate the elapsed time
  86.   unsigned long elapsedTime = millis() - startTime;
  87.  
  88.   // Perform movement for 12 hours in one direction
  89.   if (forwardDirection && elapsedTime < 12 * 3600 * 1000)
  90.   {
  91.     // Run the stepper motor at the set speed
  92.     stepper.runSpeed();
  93.   }
  94.   // Reverse the direction and reset the start time
  95.   else if (forwardDirection)
  96.   {
  97.     forwardDirection = false;
  98.     startTime = millis();
  99.     stepper.setSpeed(-50); // Set negative speed for reverse movement
  100.   }
  101.   // Perform movement for another 12 hours in reverse direction
  102.   else if (!forwardDirection && elapsedTime < 24 * 3600 * 1000)
  103.   {
  104.     // Run the stepper motor at the set speed
  105.     stepper.runSpeed();
  106.   }
  107.   // Reset the direction and start time after 24 hours
  108.   else
  109.   {
  110.     forwardDirection = true;
  111.     startTime = millis();
  112.     stepper.setSpeed(50); // Set positive speed for forward movement
  113.   }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement