Advertisement
pleasedontcode

**Motor Control** rev_01

Dec 26th, 2024
152
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: **Motor Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-27 03:51:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* this code is stopping the motors on forward api */
  21.     /* call can you make the motors move backward on */
  22.     /* backward api call and stop it on pressing stop and */
  23.     /* foward on forward api call */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <Adafruit_MotorShield.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void moveForward();
  36. void moveBackward();
  37. void stopMotors();
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. Adafruit_MotorShield motorShield = Adafruit_MotorShield(); // Create motor shield object
  41. Adafruit_DCMotor *motor1 = motorShield.getMotor(1); // Get motor 1
  42. Adafruit_DCMotor *motor2 = motorShield.getMotor(2); // Get motor 2
  43.  
  44. void setup(void)
  45. {
  46.     // Initialize the motor shield
  47.     motorShield.begin();
  48. }
  49.  
  50. void loop(void)
  51. {
  52.     // Example API calls
  53.     moveForward(); // Call to move forward
  54.     delay(2000); // Move forward for 2 seconds
  55.     stopMotors(); // Stop the motors
  56.     delay(1000); // Wait for 1 second
  57.     moveBackward(); // Call to move backward
  58.     delay(2000); // Move backward for 2 seconds
  59.     stopMotors(); // Stop the motors
  60. }
  61.  
  62. /****** FUNCTION IMPLEMENTATIONS *****/
  63. void moveForward() {
  64.     motor1->setSpeed(255); // Set speed to maximum
  65.     motor1->run(FORWARD); // Move motor 1 forward
  66.     motor2->setSpeed(255); // Set speed to maximum
  67.     motor2->run(FORWARD); // Move motor 2 forward
  68. }
  69.  
  70. void moveBackward() {
  71.     motor1->setSpeed(255); // Set speed to maximum
  72.     motor1->run(BACKWARD); // Move motor 1 backward
  73.     motor2->setSpeed(255); // Set speed to maximum
  74.     motor2->run(BACKWARD); // Move motor 2 backward
  75. }
  76.  
  77. void stopMotors() {
  78.     motor1->run(RELEASE); // Stop motor 1
  79.     motor2->run(RELEASE); // Stop motor 2
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement