Advertisement
pleasedontcode

Motor Control rev_01

Apr 4th, 2024
148
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 Pro Mini 5V
  14.     - Source Code created on: 2024-04-04 22:43:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* motor and potentiometer is connected physically. */
  21.     /* motor rotates back and forth  to reach  the */
  22.     /* potentiometers lowest and highest values. this is */
  23.     /* done twice for accuracy. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <L298NX2.h> //https://github.com/AndreaLombardo/L298N
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34.  
  35. /***** DEFINITION OF ANALOG INPUT PINS *****/
  36. const uint8_t potentiometerPin = A0;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t motorForwardPin = 4;
  40. const uint8_t motorBackwardPin = 6;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t motorSpeedPin = 5;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. bool motorForwardRawData = 0;
  47. bool motorBackwardRawData = 0;
  48. uint8_t motorSpeedRawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. float motorForwardPhyData = 0.0;
  52. float motorBackwardPhyData = 0.0;
  53. float motorSpeedPhyData = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. L298NX2 motorController(motorSpeedPin, motorForwardPin, motorBackwardPin, motorSpeedPin, motorForwardPin, motorBackwardPin); //Creating an instance of L298NX2 class
  57.  
  58. void setup(void)
  59. {
  60.   pinMode(potentiometerPin, INPUT);
  61.   pinMode(motorForwardPin, OUTPUT);
  62.   pinMode(motorBackwardPin, OUTPUT);
  63.   pinMode(motorSpeedPin, OUTPUT);
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   updateOutputs(); // Refresh output data
  69.  
  70.   // Implement the motor control logic here
  71.   // Example:
  72.   // Read the potentiometer value from the analog input pin
  73.   int potValue = analogRead(potentiometerPin);
  74.  
  75.   // Map the potentiometer value to motor speed
  76.   int motorSpeed = map(potValue, 0, 1023, 0, 255);
  77.  
  78.   // Set the motor speed using the L298NX2 library
  79.   motorController.setSpeedA(motorSpeed);
  80.   motorController.setSpeedB(motorSpeed);
  81.  
  82.   // Add the logic to rotate the motor back and forth to reach the potentiometer's lowest and highest values
  83.  
  84.   delay(100); // Adjust the delay as needed
  85. }
  86.  
  87. void updateOutputs(void)
  88. {
  89.   digitalWrite(motorForwardPin, motorForwardRawData);
  90.   digitalWrite(motorBackwardPin, motorBackwardRawData);
  91.   analogWrite(motorSpeedPin, motorSpeedRawData);
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement