Advertisement
pleasedontcode

**Motor Control** rev_01

Nov 28th, 2024
59
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: Arduino Uno
  14.     - Source Code created on: 2024-11-28 19:01:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I have stacked hw 130 l293d on the arduino and */
  21.     /* connected one dc motor to m1 and n20 gear box to */
  22.     /* m3 .we have also attached  1 servo motor to */
  23.     /* servo_1 on the driver.   Write the code for it */
  24.     /* such that   M1 and m3 connected motors run forward */
  25.     /* and reverse */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <AFMotor.h>  // Library for controlling DC motors
  32. #include <Servo.h>    // Library for controlling servo motors
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. // Motor and Servo declarations
  39. AF_DCMotor motor1(1); // Motor connected to M1
  40. AF_DCMotor motor3(3); // Motor connected to M3
  41. Servo myServo;        // Servo object
  42.  
  43. void setup(void)
  44. {
  45.     // Initialize motors
  46.     motor1.setSpeed(255); // Set speed for motor1 (0-255)
  47.     motor3.setSpeed(255); // Set speed for motor3 (0-255)
  48.  
  49.     // Attach the servo to pin 9
  50.     myServo.attach(9); // Assuming the servo is connected to pin 9
  51. }
  52.  
  53. void loop(void)
  54. {
  55.     // Run motors forward
  56.     motor1.run(FORWARD);
  57.     motor3.run(FORWARD);
  58.     delay(2000); // Run forward for 2 seconds
  59.  
  60.     // Run motors backward
  61.     motor1.run(BACKWARD);
  62.     motor3.run(BACKWARD);
  63.     delay(2000); // Run backward for 2 seconds
  64.  
  65.     // Stop motors
  66.     motor1.run(RELEASE);
  67.     motor3.run(RELEASE);
  68.     delay(1000); // Stop for 1 second
  69.  
  70.     // Move servo to 0 degrees
  71.     myServo.write(0);
  72.     delay(1000); // Wait for 1 second
  73.  
  74.     // Move servo to 90 degrees
  75.     myServo.write(90);
  76.     delay(1000); // Wait for 1 second
  77.  
  78.     // Move servo to 180 degrees
  79.     myServo.write(180);
  80.     delay(1000); // Wait for 1 second
  81. }
  82.  
  83. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement