Advertisement
pleasedontcode

Motor Control rev_09

Jan 29th, 2024
127
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-29 22:53:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Activate the L298N Dual Full-Bridge Motor Driver */
  21.     /* connected to pins D4, D5, and D3. Set IN1 and IN2 */
  22.     /* to HIGH, EN1 to 50% duty cycle, for 3 seconds. */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* Refine the user description requirement by */
  25.     /* including the functionality to stop the motor */
  26.     /* connected to the L298N Dual Full-Bridge Motor */
  27.     /* Driver after a duration of 6 seconds. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <L298N.h>    // Include L298N library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void);
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t IN1_PIN = 4;
  40. const uint8_t IN2_PIN = 5;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t EN_PIN = 3;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. bool IN1_rawData = 0;
  47. bool IN2_rawData = 0;
  48. uint8_t EN_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. float IN1_phyData = 0.0;
  52. float IN2_phyData = 0.0;
  53. float EN_phyData = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. L298N motor(EN_PIN, IN1_PIN, IN2_PIN);    // Create L298N motor instance
  57.  
  58. unsigned long systemStartTime = 0;  // Variable to store the system start time
  59. bool motorStarted = false;          // Flag to indicate if motor has started
  60. bool motorStopped = false;          // Flag to indicate if motor has stopped
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(IN1_PIN, OUTPUT);
  66.     pinMode(IN2_PIN, OUTPUT);
  67.     pinMode(EN_PIN, OUTPUT);
  68.    
  69.     // Activate the L298N Dual Full-Bridge Motor Driver
  70.     // Set IN1 and IN2 to HIGH, EN1 to 50% duty cycle, for 3 seconds
  71.     IN1_rawData = HIGH;
  72.     IN2_rawData = HIGH;
  73.     EN_rawData = 128; // 50% duty cycle
  74.    
  75.     // Store the system start time
  76.     systemStartTime = millis();
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.     if (!motorStarted && millis() - systemStartTime >= 3000) {
  83.         // Start the motor
  84.         motorStarted = true;
  85.         EN_rawData = 255; // 100% duty cycle
  86.     }
  87.    
  88.     if (motorStarted && !motorStopped && millis() - systemStartTime >= 6000) {
  89.         // Stop the motor after 6 seconds
  90.         motorStopped = true;
  91.         EN_rawData = 0; // 0% duty cycle
  92.     }
  93.    
  94.     updateOutputs(); // Refresh output data
  95. }
  96.  
  97. void updateOutputs()
  98. {
  99.     // Set output pins according to the raw data
  100.     digitalWrite(IN1_PIN, IN1_rawData);
  101.     digitalWrite(IN2_PIN, IN2_rawData);
  102.     analogWrite(EN_PIN, EN_rawData);
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement