-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCMotor.cpp
More file actions
93 lines (71 loc) · 1.82 KB
/
DCMotor.cpp
File metadata and controls
93 lines (71 loc) · 1.82 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
DCMotor.cpp - Library for controlling a bidirectional DC Motor
based on a L293D motor controller and the circuit in the following
diagram: https://www.ti.com/lit/ds/symlink/l293d.pdf?ts=1614037260430&ref_url=https%253A%252F%252Fwww.ti.com%252Fproduct%252FL293D .
Created by Jake LoRocco. February 2021.
*/
#include "Arduino.h"
#include "DCMotor.h"
DCMotor::DCMotor( int enablePin, int rightPin, int leftPin) {
this->enPin = enablePin;
this->rPin = rightPin;
this->lPin = leftPin;
pinInit();
}
void DCMotor::pinInit() {
pinMode(enPin, OUTPUT);
pinMode(rPin, OUTPUT);
pinMode(lPin, OUTPUT);
this->pinsInitialized = true;
}
void DCMotor::turnFastStop( int dir, int sec ) {
//0 - right turn;
//1 - left turn;
int highPin;
int lowPin;
if( dir == 0 ) {
highPin = this->rPin;
lowPin = this->lPin;
} else {
highPin = this->lPin;
lowPin = this->rPin;
}
digitalWrite(this->enPin, HIGH);
digitalWrite(highPin, HIGH);
digitalWrite(lowPin, LOW);
delay(sec * 1000);
digitalWrite(highPin, LOW);
}
void DCMotor::turnFreeStop( int dir, int sec, int del ) {
//0 - right turn;
//1 - left turn;
int highPin;
int lowPin;
if( dir == 0 ) {
highPin = this->rPin;
lowPin = this->lPin;
} else {
highPin = this->lPin;
lowPin = this->rPin;
}
digitalWrite(this->enPin, HIGH);
digitalWrite(highPin, HIGH);
digitalWrite(lowPin, LOW);
delay(sec * 1000);
digitalWrite(this->enPin, LOW);
digitalWrite(highPin, LOW);
delay(del * 1000);
digitalWrite(this->enPin, HIGH);
}
void DCMotor::rightFast( int sec ) {
turnFastStop( 0, sec );
}
void DCMotor::leftFast( int sec ) {
turnFastStop( 1, sec );
}
void DCMotor::rightFree( int sec, int del ) {
turnFreeStop( 0, sec, del );
}
void DCMotor::leftFree( int sec, int del ) {
turnFreeStop( 1, sec, del );
}