CCCPaste

"Bluetooth Motor" rev_06

/********* 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 NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-01 19:54:24

********* 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 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void processCommand(char command);

/***** 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;
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7 = 7;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5 = 5;
const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9 = 9;

/***** 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;
bool motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = 0;
uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 0;
uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_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_IN4_PIN_D7_phyData = 0.0;
float motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_phyData = 0.0;
float motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_phyData = 0.0;

void setup(void)
{
    // Initialize motor driver pins as outputs
    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_IN4_PIN_D7, OUTPUT);
    pinMode(motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5, OUTPUT);
    pinMode(motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9, OUTPUT);

    // Initialize Bluetooth serial communication
    blue_HC05_mySerial.begin(9600);
}

void loop(void)
{
    // Check if data is available from the Bluetooth module
    if (blue_HC05_mySerial.available())
    {
        char command = blue_HC05_mySerial.read(); // Read the command
        processCommand(command); // Process the command
    }

    updateOutputs(); // Refresh output data
}

void updateOutputs()
{
    // Update digital outputs
    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);
    digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7, motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData);
    
    // Update PWM outputs
    analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData);
    analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9, motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData);
}

void processCommand(char command)
{
    switch (command)
    {
    case 'F': // Move Forward
        motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
        motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
        break;
    case 'B': // Move Backward
        motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
        motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
        break;
    case 'L': // Turn Left
        motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
        motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
        break;
    case 'R': // Turn Right
        motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = HIGH;
        motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
        motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
        break;
    case 'S': // Stop
        motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
        motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 0; // Stop
        motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 0; // Stop
        break;
    default:
        // Invalid command
        break;
    }
}