Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Motor Control
- - Source Code compiled for: Arduino Pro Mini 5V
- - Source Code created on: 2024-04-04 22:43:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* motor and potentiometer is connected physically. */
- /* motor rotates back and forth to reach the */
- /* potentiometers lowest and highest values. this is */
- /* done twice for accuracy. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- #include <L298NX2.h> //https://github.com/AndreaLombardo/L298N
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t potentiometerPin = A0;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t motorForwardPin = 4;
- const uint8_t motorBackwardPin = 6;
- /***** DEFINITION OF PWM OUTPUT PINS *****/
- const uint8_t motorSpeedPin = 5;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool motorForwardRawData = 0;
- bool motorBackwardRawData = 0;
- uint8_t motorSpeedRawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float motorForwardPhyData = 0.0;
- float motorBackwardPhyData = 0.0;
- float motorSpeedPhyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- L298NX2 motorController(motorSpeedPin, motorForwardPin, motorBackwardPin, motorSpeedPin, motorForwardPin, motorBackwardPin); //Creating an instance of L298NX2 class
- void setup(void)
- {
- pinMode(potentiometerPin, INPUT);
- pinMode(motorForwardPin, OUTPUT);
- pinMode(motorBackwardPin, OUTPUT);
- pinMode(motorSpeedPin, OUTPUT);
- }
- void loop(void)
- {
- updateOutputs(); // Refresh output data
- // Implement the motor control logic here
- // Example:
- // Read the potentiometer value from the analog input pin
- int potValue = analogRead(potentiometerPin);
- // Map the potentiometer value to motor speed
- int motorSpeed = map(potValue, 0, 1023, 0, 255);
- // Set the motor speed using the L298NX2 library
- motorController.setSpeedA(motorSpeed);
- motorController.setSpeedB(motorSpeed);
- // Add the logic to rotate the motor back and forth to reach the potentiometer's lowest and highest values
- delay(100); // Adjust the delay as needed
- }
- void updateOutputs(void)
- {
- digitalWrite(motorForwardPin, motorForwardRawData);
- digitalWrite(motorBackwardPin, motorBackwardRawData);
- analogWrite(motorSpeedPin, motorSpeedRawData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement