Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerBroker2 authored Jul 29, 2021
1 parent 725a712 commit cb58b18
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/forward_brake_backward/forward_brake_backward.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "MotorController.h"


MotorController motor = MotorController(2, 3, 4);


void setup()
{

}


void loop()
{
motor.write(255);
delay(5000);

motor.brake();
delay(5000);

motor.write(-255);
delay(5000);
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=MotorController
version=1.0.0
author=PowerBroker2 <[email protected]>
maintainer=PowerBroker2 <[email protected]>
sentence=Easy to use Arduino library to control virtually any DC motor controller
paragraph=Easy to use Arduino library to control virtually any DC motor controller
category=Device Control
url=https://github.com/PowerBroker2/ELMduino
architectures=*
78 changes: 78 additions & 0 deletions src/MotorController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "MotorController.h"




MotorController::MotorController(const byte& _pwmPin, const byte& _in1, const byte& _in2)
{
pwmPin = _pwmPin;
in1 = _in1;
in2 = _in2;

pinMode(pwmPin, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}




void MotorController::write(const int& _speed)
{
if (_speed > 0)
forward(_speed);
else
reverse(_speed);
}




void MotorController::forward(const int& _speed)
{
speed = _speed;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(pwmPin, speed);
}




void MotorController::brake()
{
speed = 0;
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(pwmPin, speed);
}




void MotorController::free()
{
speed = 0;
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
analogWrite(pwmPin, speed);
}




void MotorController::reverse(const int& _speed)
{
speed = _speed;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(pwmPin, speed);
}




int MotorController::read()
{
return speed;
}
23 changes: 23 additions & 0 deletions src/MotorController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "Arduino.h"




class MotorController
{
public:
MotorController(const byte& _pwmPin, const byte& _in1, const byte& _in2);
void write(const int& _speed);
void forward(const int& _speed);
void brake();
void free();
void reverse(const int& _speed);
int read();

private:
int speed;
byte pwmPin;
byte in1;
byte in2;
};

0 comments on commit cb58b18

Please sign in to comment.