/********* 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:47: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 *****/


/********* User code review feedback **********
#### Feedback 1 ####
- I especially need the part  Add the logic to rotate the motor ba
ck and forth to reach the potentiometer's lowest and highest val
ues
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include <EasyButton.h> //https://github.com/evert-arias/EasyButton
#include <L298N.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*****/
L298N motorController(motorSpeedPin, motorForwardPin, motorBackwardPin); //Creating an instance of L298N 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 L298N library
  motorController.setSpeed(motorSpeed);

  // Add the logic to rotate the motor back and forth to reach the potentiometer's lowest and highest values
  if (potValue < 100)
  {
    motorController.forward();
  }
  else if (potValue > 900)
  {
    motorController.backward();
  }
  else
  {
    motorController.stop();
  }

  delay(100); // Adjust the delay as needed
}

void updateOutputs(void)
{
  digitalWrite(motorForwardPin, motorForwardRawData);
  digitalWrite(motorBackwardPin, motorBackwardRawData);
  analogWrite(motorSpeedPin, motorSpeedRawData);
}
