Advertisement
pleasedontcode

"Motor Control" rev_01

Jun 1st, 2024
627
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-06-01 19:10:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* it must get input  to command wheels forward, */
  21.     /* backward, left, right, and stop */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include "L298N_MotorDriver.h"  // Include the L298N_MotorDriver library
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. void printSomeInfo(void);
  31. void processCommand(char command);
  32.  
  33. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  34. const uint8_t IN1_PIN = 7;
  35. const uint8_t IN2_PIN = 8;
  36. const uint8_t EN_PIN = 9;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. L298N_MotorDriver motor(EN_PIN, IN1_PIN, IN2_PIN);  // Initialize the motor object
  40.  
  41. void setup(void)
  42. {
  43.   // put your setup code here, to run once:
  44.   Serial.begin(9600);
  45.   while (!Serial) {}
  46.  
  47.   motor.setSpeed(0);  // Initialize motor speed to 0
  48.   motor.enable();     // Enable the motor
  49. }
  50.  
  51. void loop(void)
  52. {
  53.   // put your main code here, to run repeatedly:
  54.   if (Serial.available() > 0) {
  55.     char command = Serial.read();  // Read the incoming command
  56.     processCommand(command);       // Process the command
  57.   }
  58. }
  59.  
  60. void processCommand(char command)
  61. {
  62.   switch (command) {
  63.     case 'F':  // Forward
  64.       motor.setDirection(true);
  65.       motor.setSpeed(255);
  66.       break;
  67.     case 'B':  // Backward
  68.       motor.setDirection(false);
  69.       motor.setSpeed(255);
  70.       break;
  71.     case 'L':  // Left
  72.       // Assuming left turn means reducing speed
  73.       motor.setDirection(true);
  74.       motor.setSpeed(120);
  75.       break;
  76.     case 'R':  // Right
  77.       // Assuming right turn means reducing speed
  78.       motor.setDirection(true);
  79.       motor.setSpeed(120);
  80.       break;
  81.     case 'S':  // Stop
  82.       motor.setSpeed(0);
  83.       break;
  84.     default:
  85.       Serial.println("Unknown command");
  86.       break;
  87.   }
  88.   printSomeInfo();
  89. }
  90.  
  91. void printSomeInfo()
  92. {
  93.   Serial.print("Motor is moving = ");
  94.   Serial.print(motor.getSpeed() > 0);  // Check if motor is moving
  95.   Serial.print(" at speed = ");
  96.   Serial.println(motor.getSpeed());
  97. }
  98.  
  99. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement