Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views3 pages

DC Motor Library C++

The document defines a MotorControl class for controlling motors using an Arduino. It includes methods to initialize motor pins, move forward, backward, turn left, turn right, and stop the motors. Motor control pins and speed are also specified in the code.

Uploaded by

sk.ngeno42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

DC Motor Library C++

The document defines a MotorControl class for controlling motors using an Arduino. It includes methods to initialize motor pins, move forward, backward, turn left, turn right, and stop the motors. Motor control pins and speed are also specified in the code.

Uploaded by

sk.ngeno42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#ifndef MOTORS_H

#define MOTORS_H

#include "Arduino.h""

// Define motor control pins

#define LEFT_MOTOR_FWD 5

#define LEFT_MOTOR_BWD 6

#define RIGHT_MOTOR_FWD 9

#define RIGHT_MOTOR_BWD 10

// Define speed (0-255)

#define MOTOR_SPEED 150

class MotorControl {

public:

// Constructor

MotorControl() {}

// Initialize motor pins

void begin() {

pinMode(LEFT_MOTOR_FWD, OUTPUT);

pinMode(LEFT_MOTOR_BWD, OUTPUT);

pinMode(RIGHT_MOTOR_FWD, OUTPUT);

pinMode(RIGHT_MOTOR_BWD, OUTPUT);
stopMotors();

// Move Forward

void moveForward() {

analogWrite(LEFT_MOTOR_FWD, MOTOR_SPEED);

analogWrite(RIGHT_MOTOR_FWD, MOTOR_SPEED);

digitalWrite(LEFT_MOTOR_BWD, LOW);

digitalWrite(RIGHT_MOTOR_BWD, LOW);

// Move Backward

void moveBackward() {

analogWrite(LEFT_MOTOR_BWD, MOTOR_SPEED);

analogWrite(RIGHT_MOTOR_BWD, MOTOR_SPEED);

digitalWrite(LEFT_MOTOR_FWD, LOW);

digitalWrite(RIGHT_MOTOR_FWD, LOW);

// Turn Left

void turnLeft() {

analogWrite(LEFT_MOTOR_FWD, 60);

analogWrite(RIGHT_MOTOR_FWD, MOTOR_SPEED);

digitalWrite(LEFT_MOTOR_BWD, LOW);

digitalWrite(RIGHT_MOTOR_BWD, LOW);
}

// Turn Right

void turnRight() {

analogWrite(LEFT_MOTOR_FWD, MOTOR_SPEED);

analogWrite(RIGHT_MOTOR_FWD, 60);

digitalWrite(LEFT_MOTOR_BWD, LOW);

digitalWrite(RIGHT_MOTOR_BWD, LOW);

// Stop Motors

void stopMotors() {

digitalWrite(LEFT_MOTOR_FWD, LOW);

digitalWrite(RIGHT_MOTOR_FWD, LOW);

digitalWrite(LEFT_MOTOR_BWD, LOW);

digitalWrite(RIGHT_MOTOR_BWD, LOW);

};

#endif

You might also like