Advertisement
pleasedontcode

"Motor Switch" rev_01

Mar 31st, 2024
78
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 Switch"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-31 10:11:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Control 2 DC Motors using L293D Bridge for forward */
  21.     /* and backward direction, pushbutton for switching */
  22.     /* on and off */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <L293.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup();
  30. void loop();
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t SwitchPushbuttonPin = 2;
  34.  
  35. /***** DEFINITION OF MOTOR CONTROL PINS *****/
  36. const uint8_t Motor1EnablePin = 4;
  37. const uint8_t Motor1DirectionPin1 = 5;
  38. const uint8_t Motor1DirectionPin2 = 6;
  39. const uint8_t Motor2EnablePin = 7;
  40. const uint8_t Motor2DirectionPin1 = 8;
  41. const uint8_t Motor2DirectionPin2 = 9;
  42.  
  43. /***** DEFINITION OF MOTOR CLASS INSTANCES *****/
  44. L293 motor1(Motor1EnablePin, Motor1DirectionPin1, Motor1DirectionPin2);
  45. L293 motor2(Motor2EnablePin, Motor2DirectionPin1, Motor2DirectionPin2);
  46.  
  47. /****** Flag to indicate motor state *****/
  48. bool motorsOn = false;
  49.  
  50. void setup()
  51. {
  52.     pinMode(SwitchPushbuttonPin, INPUT_PULLUP);
  53.    
  54.     motor1.setPWMOffset(10);  // Optional speed offset adjustment for Motor 1
  55.     motor2.setPWMOffset(-10); // Optional speed offset adjustment for Motor 2
  56. }
  57.  
  58. void loop()
  59. {
  60.     bool switchPushed = digitalRead(SwitchPushbuttonPin) == LOW;
  61.    
  62.     if (switchPushed && !motorsOn)
  63.     {
  64.         // Turn on the motors and make them go forward
  65.         motor1.forward();
  66.         motor2.forward();
  67.         motorsOn = true;
  68.     }
  69.     else if (!switchPushed && motorsOn)
  70.     {
  71.         // Turn off the motors and stop them
  72.         motor1.forceStop(1000); // Perform electrical braking with a 1 second handling time for Motor 1
  73.         motor2.forceStop(1000); // Perform electrical braking with a 1 second handling time for Motor 2
  74.         motorsOn = false;
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement