CCCPaste

"Motor Control" rev_01

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

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* it must get input  to command wheels forward, */
	/* backward, left, right, and stop */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include "L298N_MotorDriver.h"  // Include the L298N_MotorDriver library

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t IN1_PIN = 7;
const uint8_t IN2_PIN = 8;
const uint8_t EN_PIN = 9;

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

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {}

  motor.setSpeed(0);  // Initialize motor speed to 0
  motor.enable();     // Enable the motor
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    char command = Serial.read();  // Read the incoming command
    processCommand(command);       // Process the command
  }
}

void processCommand(char command)
{
  switch (command) {
    case 'F':  // Forward
      motor.setDirection(true);
      motor.setSpeed(255);
      break;
    case 'B':  // Backward
      motor.setDirection(false);
      motor.setSpeed(255);
      break;
    case 'L':  // Left
      // Assuming left turn means reducing speed
      motor.setDirection(true);
      motor.setSpeed(120);
      break;
    case 'R':  // Right
      // Assuming right turn means reducing speed
      motor.setDirection(true);
      motor.setSpeed(120);
      break;
    case 'S':  // Stop
      motor.setSpeed(0);
      break;
    default:
      Serial.println("Unknown command");
      break;
  }
  printSomeInfo();
}

void printSomeInfo()
{
  Serial.print("Motor is moving = ");
  Serial.print(motor.getSpeed() > 0);  // Check if motor is moving
  Serial.print(" at speed = ");
  Serial.println(motor.getSpeed());
}

/* END CODE */