Advertisement
pleasedontcode

LinearRailSlide rev_67

Nov 29th, 2023
91
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:40:23
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <AccelStepper.h>
  21.  
  22. /****** SYSTEM REQUIREMENT 1 *****/
  23. /* A stepper motor drives a 200 mm length linear rail */
  24. /* slide. The motor performs 100 steps per */
  25. /* revolution. Each revolution moves the slide 10mm. */
  26. /*
  27.    Here, we assume that the stepper motor is properly configured
  28.    to achieve this requirement. Hence, no changes are required in the code.
  29. */
  30.  
  31. /****** SYSTEM REQUIREMENT 2 *****/
  32. /* Perform a complete continuous travel from right to */
  33. /* left in 12 hours. Then go back from left to right */
  34. /* again in 12 hours. The movement shall be */
  35. /* continuous in 12 hours. Do not use delay function. */
  36. /*
  37.    In order to achieve this requirement, we need to modify
  38.    the loop function to continuously run the stepper motor
  39.    for 12 hours in one direction, then reverse the direction
  40.    and run for another 12 hours. We can use millis() function
  41.    to track the time and control the motor movement.
  42. */
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t stepper_A4988StepperMotorDriver_STEP_PIN_D8 = 8;
  50. const uint8_t stepper_A4988StepperMotorDriver_DIR_PIN_D9 = 9;
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  53. AccelStepper stepper(AccelStepper::FULL4WIRE, stepper_A4988StepperMotorDriver_STEP_PIN_D8, stepper_A4988StepperMotorDriver_DIR_PIN_D9);
  54.  
  55. // Variables for tracking time and movement direction
  56. unsigned long startTime;
  57. bool forwardDirection = true;
  58.  
  59. void setup(void)
  60. {
  61.   // put your setup code here, to run once:
  62.  
  63.   // Set the step and direction pins as output
  64.   pinMode(stepper_A4988StepperMotorDriver_STEP_PIN_D8, OUTPUT);
  65.   pinMode(stepper_A4988StepperMotorDriver_DIR_PIN_D9, OUTPUT);
  66.  
  67.   // Set the maximum speed and initial speed of the stepper motor
  68.   stepper.setMaxSpeed(1000);
  69.   stepper.setSpeed(50);
  70.  
  71.   // Initialize start time
  72.   startTime = millis();
  73. }
  74.  
  75. void loop(void)
  76. {
  77.   // put your main code here, to run repeatedly:
  78.  
  79.   // Calculate the elapsed time
  80.   unsigned long elapsedTime = millis() - startTime;
  81.  
  82.   // Perform movement for 12 hours in one direction
  83.   if (forwardDirection && elapsedTime < 12 * 3600 * 1000)
  84.   {
  85.     // Run the stepper motor at the set speed
  86.     stepper.runSpeed();
  87.   }
  88.   // Reverse the direction and reset the start time
  89.   else if (forwardDirection)
  90.   {
  91.     forwardDirection = false;
  92.     startTime = millis();
  93.     stepper.setSpeed(-50); // Set negative speed for reverse movement
  94.   }
  95.   // Perform movement for another 12 hours in reverse direction
  96.   else if (!forwardDirection && elapsedTime < 24 * 3600 * 1000)
  97.   {
  98.     // Run the stepper motor at the set speed
  99.     stepper.runSpeed();
  100.   }
  101.   // Reset the direction and start time after 24 hours
  102.   else
  103.   {
  104.     forwardDirection = true;
  105.     startTime = millis();
  106.     stepper.setSpeed(50); // Set positive speed for forward movement
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement