CCCPaste

"Bluetooth Motor" rev_09

/********* 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 20:19:51

********* 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 "L298N_MotorDriver.h"  // Include the L298N_MotorDriver library

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t IN1_PIN = 2;
const uint8_t IN2_PIN = 4;
const uint8_t EN_PIN = 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 IN1_PIN_rawData = 0;
bool IN2_PIN_rawData = 0;
uint8_t EN_PIN_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float IN1_PIN_phyData = 0.0;
float IN2_PIN_phyData = 0.0;
float EN_PIN_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
L298N_MotorDriver motor(EN_PIN, IN1_PIN, IN2_PIN);  // Initialize the motor driver

void setup(void)
{
    // put your setup code here, to run once:
    pinMode(IN1_PIN, OUTPUT);
    pinMode(IN2_PIN, OUTPUT);
    pinMode(EN_PIN, OUTPUT);

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

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

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

        // Process the command
        switch (command)
        {
            case 'F':  // Forward
                motor.setDirection(true);
                motor.enable();
                break;
            case 'B':  // Backward
                motor.setDirection(false);
                motor.enable();
                break;
            case 'L':  // Left
                motor.setSpeed(70);  // Adjust speed for turning
                motor.setDirection(true);
                motor.enable();
                break;
            case 'R':  // Right
                motor.setSpeed(70);  // Adjust speed for turning
                motor.setDirection(false);
                motor.enable();
                break;
            case 'S':  // Stop
                motor.disable();
                break;
            default:
                Serial.println("Unknown command");
                break;
        }

        printSomeInfo();  // Print motor status
    }

    delay(100);  // Small delay to avoid overwhelming the serial buffer
}

void updateOutputs()
{
    digitalWrite(IN1_PIN, IN1_PIN_rawData);
    digitalWrite(IN2_PIN, IN2_PIN_rawData);
    analogWrite(EN_PIN, EN_PIN_rawData);
}

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

/* END CODE */