Advertisement
pleasedontcode

Motor Control rev_02

Apr 4th, 2024
120
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:47: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.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - I especially need the part  Add the logic to rotate the motor ba
  30. ck and forth to reach the potentiometer's lowest and highest val
  31. ues
  32. ********* User code review feedback **********/
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  36. #include <L298N.h> //https://github.com/AndreaLombardo/L298N
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void updateOutputs(void);
  42.  
  43. /***** DEFINITION OF ANALOG INPUT PINS *****/
  44. const uint8_t potentiometerPin = A0;
  45.  
  46. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  47. const uint8_t motorForwardPin = 4;
  48. const uint8_t motorBackwardPin = 6;
  49.  
  50. /***** DEFINITION OF PWM OUTPUT PINS *****/
  51. const uint8_t motorSpeedPin = 5;
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. bool motorForwardRawData = 0;
  55. bool motorBackwardRawData = 0;
  56. uint8_t motorSpeedRawData = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. float motorForwardPhyData = 0.0;
  60. float motorBackwardPhyData = 0.0;
  61. float motorSpeedPhyData = 0.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. L298N motorController(motorSpeedPin, motorForwardPin, motorBackwardPin); //Creating an instance of L298N class
  65.  
  66. void setup(void)
  67. {
  68.   pinMode(potentiometerPin, INPUT);
  69.   pinMode(motorForwardPin, OUTPUT);
  70.   pinMode(motorBackwardPin, OUTPUT);
  71.   pinMode(motorSpeedPin, OUTPUT);
  72. }
  73.  
  74. void loop(void)
  75. {
  76.   updateOutputs(); // Refresh output data
  77.  
  78.   // Implement the motor control logic here
  79.   // Example:
  80.   // Read the potentiometer value from the analog input pin
  81.   int potValue = analogRead(potentiometerPin);
  82.  
  83.   // Map the potentiometer value to motor speed
  84.   int motorSpeed = map(potValue, 0, 1023, 0, 255);
  85.  
  86.   // Set the motor speed using the L298N library
  87.   motorController.setSpeed(motorSpeed);
  88.  
  89.   // Add the logic to rotate the motor back and forth to reach the potentiometer's lowest and highest values
  90.   if (potValue < 100)
  91.   {
  92.     motorController.forward();
  93.   }
  94.   else if (potValue > 900)
  95.   {
  96.     motorController.backward();
  97.   }
  98.   else
  99.   {
  100.     motorController.stop();
  101.   }
  102.  
  103.   delay(100); // Adjust the delay as needed
  104. }
  105.  
  106. void updateOutputs(void)
  107. {
  108.   digitalWrite(motorForwardPin, motorForwardRawData);
  109.   digitalWrite(motorBackwardPin, motorBackwardRawData);
  110.   analogWrite(motorSpeedPin, motorSpeedRawData);
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement