CCCPaste

"Bluetooth Motor" rev_08

/********* 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: "Bluetooth Motor"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-06-01 20:06:55

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system must receive input via the HC-05 */
	/* Bluetooth module to control the L298N motor driver */
	/* for moving the wheels forward, backward, left, */
	/* right, and stop. */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/AndreaLombardo/L298N

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void printSomeInfo(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2 = 2;
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4 = 4;
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6 = 6;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5 = 5;

/***** DEFINITION OF Software Serial *****/
const uint8_t blue_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
const uint8_t blue_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial blue_HC05_mySerial(blue_HC05_mySerial_PIN_SERIAL_RX_A1, blue_HC05_mySerial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = 0;
bool motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = 0;
bool motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = 0;
uint8_t motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_phyData = 0.0;
float motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_phyData = 0.0;
float motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_phyData = 0.0;
float motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
L298N motor(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4);

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, OUTPUT);
  pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, OUTPUT);
  pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, OUTPUT);
  pinMode(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, OUTPUT);

  blue_HC05_mySerial.begin(9600); // Initialize Bluetooth serial communication
  Serial.begin(9600); // Initialize serial communication for debugging
  while (!Serial) {}

  motor.setSpeed(70); // Set initial motor speed
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  updateOutputs(); // Refresh output data

  if (blue_HC05_mySerial.available()) {
    char command = blue_HC05_mySerial.read(); // Read command from Bluetooth

    switch (command) {
      case 'F': // Forward
        motor.forward();
        break;
      case 'B': // Backward
        motor.backward();
        break;
      case 'L': // Left
        // Implement left turn logic
        motor.setSpeed(70); // Reduce speed for turning
        motor.forward();
        delay(500); // Adjust delay for turning duration
        motor.setSpeed(255); // Restore speed after turning
        break;
      case 'R': // Right
        // Implement right turn logic
        motor.setSpeed(70); // Reduce speed for turning
        motor.backward();
        delay(500); // Adjust delay for turning duration
        motor.setSpeed(255); // Restore speed after turning
        break;
      case 'S': // Stop
        motor.stop();
        break;
      default:
        // Invalid command
        break;
    }
    printSomeInfo(); // Print motor status
  }
}

void updateOutputs()
{
  digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData);
  digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData);
  digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData);
  analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_rawData);
}

void printSomeInfo()
{
  Serial.print("Motor is moving = ");
  Serial.print(motor.isMoving());
  Serial.print(" at speed = ");
  Serial.println(motor.getSpeed());
}

/* END CODE */