Advertisement
pleasedontcode

LinearRailSlide rev_74

Nov 29th, 2023
86
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 22:07:35
  15.     - Source Code generated by: Francesco Alessandro
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /********* User code review feedback **********
  20. #### Feedback 1 ####
  21. - do not block code with delay.
  22. ********* User code review feedback **********/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Arduino.h>
  26. #include <Stepper.h>
  27.  
  28. /****** SYSTEM REQUIREMENT 1 *****/
  29. /* Perform a complete continuous travel from right to */
  30. /* left in 12 hours. Then go back from left to right */
  31. /* again in 12 hours. The movement shall be */
  32. /* continuous in 12 hours. Do not use delay function. */
  33.  
  34. /****** SYSTEM REQUIREMENT 2 *****/
  35. /* A stepper motor drives a 200 mm length linear rail */
  36. /* slide. The motor performs 100 steps/revolution. */
  37. /* Step per mm of Lead screw is 10mm. */
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t STEPPER_PIN_1_PIN_D2 = 2;
  45. const uint8_t STEPPER_PIN_2_PIN_D3 = 3;
  46. const uint8_t STEPPER_PIN_3_PIN_D4 = 4;
  47. const uint8_t STEPPER_PIN_4_PIN_D5 = 5;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. Stepper myStepper(100, STEPPER_PIN_1_PIN_D2, STEPPER_PIN_2_PIN_D3, STEPPER_PIN_3_PIN_D4, STEPPER_PIN_4_PIN_D5);
  51.  
  52. void setup(void)
  53. {
  54.   // put your setup code here, to run once:
  55.   pinMode(STEPPER_PIN_1_PIN_D2, OUTPUT);
  56.   pinMode(STEPPER_PIN_2_PIN_D3, OUTPUT);
  57.   pinMode(STEPPER_PIN_3_PIN_D4, OUTPUT);
  58.   pinMode(STEPPER_PIN_4_PIN_D5, OUTPUT);
  59. }
  60.  
  61. void loop(void)
  62. {
  63.   // put your main code here, to run repeatedly:
  64.   /****** SYSTEM REQUIREMENT 1 *****/
  65.   /* Complete continuous travel from right to left in 12 hours */
  66.   unsigned long startTime = millis();
  67.   while (millis() - startTime < 12 * 60 * 60 * 1000) {
  68.     myStepper.step(1); // Move one step clockwise
  69.   }
  70.  
  71.   /* Go back from left to right again in 12 hours */
  72.   startTime = millis();
  73.   while (millis() - startTime < 12 * 60 * 60 * 1000) {
  74.     myStepper.step(-1); // Move one step counterclockwise
  75.   }
  76.  
  77.   /****** SYSTEM REQUIREMENT 2 *****/
  78.   /* Calculate number of steps required to move 200 mm */
  79.   int stepsPerRevolution = 100;
  80.   int mmPerRevolution = 10;
  81.   float stepsPerMM = stepsPerRevolution / mmPerRevolution;
  82.   int stepsToMove = stepsPerMM * 200;
  83.  
  84.   /* Move the stepper motor to slide the linear rail */
  85.   for (int i = 0; i < stepsToMove; i++) {
  86.     myStepper.step(1); // Move one step clockwise
  87.   }
  88.  
  89.   delay(1000); // Wait for 1 second before the next movement
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement