Accessories
Arduino for motors and lights library.
PortShieldL293d.cpp
1 /*************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Driver port for L293n>
5 *************************************************************/
6 
7 #if !defined(__AVR_ATmega32U4__)
8 
9 #include "Accessories.h"
10 
11 #ifndef NO_SHIELDL293D
12 
14 {
15 }
16 
17 void PortShieldL293d::begin(unsigned char inOutPort, uint8_t inSpeed, uint8_t inFreq)
18 {
19  this->pmotor = new AF_DCMotor(inOutPort, inFreq);
20  this->pmotor->pwmfreq = inFreq;
21  this->pmotor->setSpeed(inSpeed);
22 
23 #ifdef ACCESSORIES_PRINT_ACCESSORIES
24  this->printedOutPort = inOutPort;
25  this->printedSpeed = inSpeed;
26 #endif
27 
28  this->pmotor->run(RELEASE);
29 }
30 
31 int PortShieldL293d::SetSpeed(uint8_t inSpeed)
32 {
33  int oldspeed = Port::SetSpeed(inSpeed);
34  this->pmotor->setSpeed(inSpeed);
35  return oldspeed;
36 }
37 
38 void PortShieldL293d::MoveLeftDir(unsigned long inDuration)
39 {
40 #ifdef ACCESSORIES_DEBUG_MODE
41  Serial.print(F(" PortShieldL293d MoveLeftDir() "));
42  if (inDuration != 0)
43  {
44  Serial.print(F("for "));
45  Serial.print(inDuration);
46  Serial.println(F("ms"));
47  }
48  else
49  Serial.println("");
50 #endif
51  this->pmotor->run(FORWARD);
52  if (inDuration != 0)
53  {
54  delay(inDuration);
55  this->pmotor->run(RELEASE);
56  }
57 
58  this->state = PORT_LEFT;
59 }
60 
61 void PortShieldL293d::MoveRightDir(unsigned long inDuration)
62 {
63 #ifdef ACCESSORIES_DEBUG_MODE
64  //Serial.print(this->pmotor->motornum);
65  Serial.print(F(" PortShieldL293d MoveRightDir() "));
66  if (inDuration != 0)
67  {
68  Serial.print(F("for "));
69  Serial.print(inDuration);
70  Serial.println(F("ms"));
71  }
72  else
73  Serial.println("");
74 #endif
75  this->pmotor->run(BACKWARD);
76  if (inDuration != 0)
77  {
78  delay(inDuration);
79  this->pmotor->run(RELEASE);
80  }
81 
82  this->state = PORT_RIGHT;
83 }
84 
86 {
87  this->pmotor->run(RELEASE);
88  this->state = PORT_STOP;
89 }
90 #endif
91 
92 #ifdef ACCESSORIES_PRINT_ACCESSORIES
93 void PortShieldL293d::printPort()
94 {
95  Serial.print(F("[PortShieldL293d port: M"));
96  Serial.print((int)this->printedOutPort, DEC);
97  Serial.print(F(" speed:"));
98  Serial.print((int)this->printedSpeed, DEC);
99  Serial.print(F("]"));
100 }
101 #endif
102 #endif
PORT_STATE state
Definition: Port.hpp:67
void MoveLeftDir(unsigned long inDuration = 0)
void MoveRightDir(unsigned long inDuration = 0)
virtual int SetSpeed(int inSpeed)
Definition: Port.cpp:108
int SetSpeed(uint8_t inSpeed)
AF_DCMotor * pmotor
virtual void begin()
Definition: Port.hpp:99