-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathL298n.h
44 lines (32 loc) · 993 Bytes
/
L298n.h
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
class L298n{
public:
uint8_t dIrpin1, dIrpin2, pWmpin,lastpWm=0,step=5,interval=10; //
Cytron(int getdIrpin1, getdIrpin2, int getpWmpin){ //constructor function takes direction and pwm pins for cytron
dIrpin1=getdIrpin1;
dIrpin2=getdIrpin2;
pWmpin=getpWmpin;
pinMode(dIrpin1,OUTPUT); //sets direction pin as output
pinMode(dIrpin2,OUTPUT);
}
void direction(bool dIrection){
digitalWrite(dIrpin1,dIrection);
digitalWrite(dIrpin2,~dIrection);
}
void drive(uint8_t pWm){
if(pWm>lastpWm){ //accelerate
for(lastpWm=lastpWm;lastpWm<pWm,lastpWm+=step){ //increase the speed step by step
analogWrite(pWmpin,constraint(lastpWm,0,pWm));
delay(interval);
}
}
else if(pWm<lastpWm){ //decelerate
for(lastpWm=lastpWm;lastpWm>pWm,lastpWm-=step){ //decrease the speed step by step
analogWrite(pWmpin,constraint(lastpWm,pWm,255));
delay(interval);
}
}
else{ //no change
analogWrite(pWmpin,pWm);
}
}
}