Code Monkey home page Code Monkey logo

arduino-motordriver's Introduction

Arduino MotorDriver Library

tests PlatformIO Registry

This library let's you easily switch between different methods of controlling motors.

Assume you have the following code which controls a DC motor via an PWM and direction pin:

// code without MotorDriver library

const int MOTOR_DIR_PIN = 5;
const int MOTOR_PWM_PIN = 6;

void setup() {
    pinMode(MOTOR_DIR_PIN, OUTPUT);
    pinMode(MOTOR_PWM_PIN, OUTPUT);
}

void loop() {
    // forward
    digitalWrite(MOTOR_DIR_PIN, HIGH);
    analogWrite(MOTOR_PWM_PIN, 255);
    delay(1000);

    // stop
    analogWrite(MOTOR_PWM_PIN, 0);
    delay(1000);

    // backward
    digitalWrite(MOTOR_DIR_PIN, LOW);
    analogWrite(MOTOR_PWM_PIN, 255);
    delay(1000);

    // stop
    analogWrite(MOTOR_PWM_PIN, 0);
    delay(1000);
}

With this library this can be simplified to:

#include <MotorDriver.h>

DirPwmMotor motor(5, 6);

void setup() {}

void loop() {
    motor.setSpeed(255);
    delay(1000);

    motor.stop();
    delay(1000);

    motor.setSpeed(-255);
    delay(1000);

    motor.stop();
    delay(1000);
}

Now imagine you change the motor control circuit and now have two separate direction pins FWD and BWD and a PWM pin for speed. With this library you only need to change the DirPwmMotor to FwdBwdPwmMotor:

-DirPwmMotor motor(5, 6);
+FwdBwdPwmMotor motor(4, 5, 6);

A MotorDriver has a super simple api:

int speed();
void setSpeed(int speed);
bool reversed();
void setReversed(bool reversed);
void stop(bool brake = false);

The following implementations are available:

Classname Pins Comment
MotorDriver * Generic base class, define your own interface
PwmMotor Pwm A single direction pwm motor controller
DirPwmMotor Dir, Pwm A direction / pwm motor controller
FwdBwdPwmMotor Dir FWD, Dir BWD, Pwm Uses two pins to set the direction
HBridgeHighLowMotor H1, L1, H2, L2 A H-bridge motor controller with high / low pwm controls
HBridgeSelectPwmMotor Sel1, Pwm1, Sel2, Pwm2 A H-bridge controller with select / pwm controls
HBridgeSoftDeadtimeMotor H1, L1, H2, L2 A H-bridge motor controller with software deadtime

PRs / issues welcome!

arduino-motordriver's People

Contributors

dependabot[bot] avatar tfeldmann avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.