-
Notifications
You must be signed in to change notification settings - Fork 4
/
Odometer.cpp
56 lines (49 loc) · 1.32 KB
/
Odometer.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
/*
* Odometer.cpp - Handles the odometer (speed encoder) sensor of the Smartcar.
* Version: 0.2
* Author: Dimitris Platis (based on the Smartcar project by Team Pegasus)
* License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
*/
#include "Smartcar_sensors.h"
volatile unsigned long _pulseCounter_sensors = 0;
Odometer::Odometer(){
}
int Odometer::attach(int odometerPin){
switch(odometerPin){ //converting digital pins to interrupts for Arduino Mega
case 2:
_odometerInterruptPin = 0;
break;
case 3:
_odometerInterruptPin = 1;
break;
case 21:
_odometerInterruptPin = 2;
break;
case 20:
_odometerInterruptPin = 3;
break;
case 19:
_odometerInterruptPin = 4;
break;
case 18:
_odometerInterruptPin = 5;
break;
default:
return 0; //signals invalid interrupt pin
}
attachInterrupt(_odometerInterruptPin, updateCounter_sensors, RISING);
return 1;
}
void Odometer::begin(){
_pulseCounter_sensors = 0; //initialize the counter
}
unsigned int Odometer::getDistance(){
return PulsesToCentimeters(_pulseCounter_sensors);
}
void Odometer::detach(){
_pulseCounter_sensors = 0; //reinitialize the counter so if distance is calculated again, result will be 0 and not what was left from before
detachInterrupt(_odometerInterruptPin);
}
void updateCounter_sensors(){
_pulseCounter_sensors++;
}