CCCPaste

BluetoothControlledCar

/********* 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: Mobile_Robot_v2
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-02-07 23:41:06

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* If HC receives "F", move forward at 90% speed.  If */
	/* HC receives "B", move backward at 50% speed. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* If HC receives "L", turn left.   If HC receives */
	/* "R", turn right.  If HC receives "0", stop. */
/****** SYSTEM REQUIREMENT 3 *****/
	/* Define an instance to drive motor A.  Define an */
	/* instance to drive motor B. */
/****** END SYSTEM REQUIREMENTS *****/


/********* User code review feedback **********
#### Feedback 1 ####
- updateOutputs() is unnecessary.
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* If HC receives "F", move forward at 90% speed.  If */
	/* HC receives "B", move backward at 50% speed. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* If HC receives "L", turn left.   If HC receives */
	/* "R", turn right.  If HC receives "0", stop. */
/****** SYSTEM REQUIREMENT 3 *****/
	/* Define an instance to drive motor A.  Define an */
	/* instance to drive motor B. */
/****** END SYSTEM REQUIREMENTS *****/

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t IN1_PIN = 2;
const uint8_t IN2_PIN = 4;
const uint8_t IN3_PIN = 6;
const uint8_t IN4_PIN = 7;

/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t EN1_PIN = 3;
const uint8_t EN2_PIN = 5;

/***** DEFINITION OF Software Serial *****/
const uint8_t HC05_SERIAL_TX_PIN = A0;
const uint8_t HC05_SERIAL_RX_PIN = A1;
SoftwareSerial HC05_SERIAL(HC05_SERIAL_RX_PIN, HC05_SERIAL_TX_PIN);

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
uint8_t EN1_rawData = 0;
uint8_t EN2_rawData = 0;

/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
L298N motorA(EN1_PIN, IN1_PIN, IN2_PIN);
L298N motorB(EN2_PIN, IN3_PIN, IN4_PIN);

void setup(void)
{
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(IN3_PIN, OUTPUT);
  pinMode(IN4_PIN, OUTPUT);
  pinMode(EN1_PIN, OUTPUT);
  pinMode(EN2_PIN, OUTPUT);

  HC05_SERIAL.begin(9600);
}

void loop(void)
{
  if (HC05_SERIAL.available())
  {
    char command = HC05_SERIAL.read();
    handleCommand(command);
  }
}

void handleCommand(char command)
{
  switch (command)
  {
    case 'F': // Move forward at 90% speed
      motorA.setSpeed(230);
      motorB.setSpeed(230);
      motorA.forward();
      motorB.forward();
      break;

    case 'B': // Move backward at 50% speed
      motorA.setSpeed(128);
      motorB.setSpeed(128);
      motorA.backward();
      motorB.backward();
      break;

    case 'L': // Turn left
      motorA.setSpeed(0);
      motorB.setSpeed(230);
      motorA.backward();
      motorB.forward();
      break;

    case 'R': // Turn right
      motorA.setSpeed(230);
      motorB.setSpeed(0);
      motorA.forward();
      motorB.backward();
      break;

    case '0': // Stop
      motorA.setSpeed(0);
      motorB.setSpeed(0);
      motorA.stop();
      motorB.stop();
      break;
  }
}