/
L293D.cpp
88 lines (70 loc) · 1.91 KB
/
L293D.cpp
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
/*
* L293D class
*/
#include "L293D.h"
//Constructor
L293D::L293D(int pin1, int pin2){
this->pin_motor_1 = pin1;
this->pin_motor_2 = pin2;
pinMode(this->pin_motor_1, OUTPUT);
pinMode(this->pin_motor_2, OUTPUT);
this->previousMillis = millis();
this->running = false;
this->runningSpeed = 255; //Between 0 and 255
this->runningMilliseconds = 0;
this->runningDirection = 0;
}
void L293D::SetRunningMilliseconds(unsigned long milliseconds){
this->runningMilliseconds = milliseconds;
}
void L293D::SetDirection(int direction){
//Stop whatever was running, maybe not due to hickups
//if(this->running){
// this->Stop();
//}
this->runningDirection = direction;
}
void L293D::Update(){
unsigned long currentMillis = millis();
if(this->runningMilliseconds > 0 && !this->running){
/*Serial.print("going on ");
Serial.println(millis());*/
this->previousMillis = currentMillis; // Remember the time
//start
this->Run();
}
if(this->running && (currentMillis - this->previousMillis >= this->runningMilliseconds)){
/*Serial.print("going off ");
Serial.println(millis());*/
this->previousMillis = currentMillis; // Remember the time
this->runningMilliseconds = 0;
//stop
this->Stop();
}
/*Serial.print(currentMillis - this->previousMillis);
Serial.print(" - ");
Serial.println(this->runningMilliseconds);*/
}
void L293D::Run(){
this->running = true;
//down
if(this->runningDirection == 0){
analogWrite(pin_motor_1, this->runningSpeed);
analogWrite(pin_motor_2, 0);
}
//up
else if(this->runningDirection == 1){
analogWrite(pin_motor_1, 0);
analogWrite(pin_motor_2, this->runningSpeed);
}
}
void L293D::Stop(){
this->running = false;
analogWrite(pin_motor_1, 0);
analogWrite(pin_motor_2, 0);
}
void L293D::ImmediateStop(){
this->runningMilliseconds = 0;
//this->running = false;
this->Stop();
}