-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCMotor.cpp
More file actions
41 lines (37 loc) · 818 Bytes
/
DCMotor.cpp
File metadata and controls
41 lines (37 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "Arduino.h"
#include <DCMotor.h>
DCMotor::DCMotor(unsigned int in1, unsigned int in2, unsigned int en, bool *isMovingForward)
{
this->in1 = in1;
this->in2 = in2;
this->en = en;
this->isMovingForward = isMovingForward;
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void DCMotor::move(double speed)
{
if (speed == 0)
{
this->stop();
return;
}
else if (*(this->isMovingForward))
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(en, speed);
}
else
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en, speed);
}
}
void DCMotor::stop()
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(en, 0);
}