Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: LinearRailSlide
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2023-11-29 21:40:23
- - Source Code generated by: Francesco Alessandro
- ********* Pleasedontcode.com **********/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <AccelStepper.h>
- /****** SYSTEM REQUIREMENT 1 *****/
- /* A stepper motor drives a 200 mm length linear rail */
- /* slide. The motor performs 100 steps per */
- /* revolution. Each revolution moves the slide 10mm. */
- /*
- Here, we assume that the stepper motor is properly configured
- to achieve this requirement. Hence, no changes are required in the code.
- */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Perform a complete continuous travel from right to */
- /* left in 12 hours. Then go back from left to right */
- /* again in 12 hours. The movement shall be */
- /* continuous in 12 hours. Do not use delay function. */
- /*
- In order to achieve this requirement, we need to modify
- the loop function to continuously run the stepper motor
- for 12 hours in one direction, then reverse the direction
- and run for another 12 hours. We can use millis() function
- to track the time and control the motor movement.
- */
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t stepper_A4988StepperMotorDriver_STEP_PIN_D8 = 8;
- const uint8_t stepper_A4988StepperMotorDriver_DIR_PIN_D9 = 9;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- AccelStepper stepper(AccelStepper::FULL4WIRE, stepper_A4988StepperMotorDriver_STEP_PIN_D8, stepper_A4988StepperMotorDriver_DIR_PIN_D9);
- // Variables for tracking time and movement direction
- unsigned long startTime;
- bool forwardDirection = true;
- void setup(void)
- {
- // put your setup code here, to run once:
- // Set the step and direction pins as output
- pinMode(stepper_A4988StepperMotorDriver_STEP_PIN_D8, OUTPUT);
- pinMode(stepper_A4988StepperMotorDriver_DIR_PIN_D9, OUTPUT);
- // Set the maximum speed and initial speed of the stepper motor
- stepper.setMaxSpeed(1000);
- stepper.setSpeed(50);
- // Initialize start time
- startTime = millis();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- // Calculate the elapsed time
- unsigned long elapsedTime = millis() - startTime;
- // Perform movement for 12 hours in one direction
- if (forwardDirection && elapsedTime < 12 * 3600 * 1000)
- {
- // Run the stepper motor at the set speed
- stepper.runSpeed();
- }
- // Reverse the direction and reset the start time
- else if (forwardDirection)
- {
- forwardDirection = false;
- startTime = millis();
- stepper.setSpeed(-50); // Set negative speed for reverse movement
- }
- // Perform movement for another 12 hours in reverse direction
- else if (!forwardDirection && elapsedTime < 24 * 3600 * 1000)
- {
- // Run the stepper motor at the set speed
- stepper.runSpeed();
- }
- // Reset the direction and start time after 24 hours
- else
- {
- forwardDirection = true;
- startTime = millis();
- stepper.setSpeed(50); // Set positive speed for forward movement
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement