-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.h
57 lines (46 loc) · 1.15 KB
/
sensor.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
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SENSOR_H
#define SENSOR_H
#include <stdint.h> /* Declarations of uint_32 and the like */
#include <pic32mx.h> /* Declarations of system-specific addresses etc */
#include "mipslab.h" /* Declatations for these labs */
#include <stdbool.h> // need this for bool
long echoStart = 0;
long echoStop = 0;
bool echoHasBeenTriggered = false;
bool takeStartTime = false;
long globalDistance = 0;
/*
35 IC4/PMCS1/PMA14/INT4/RD11 *trig
34 PMRD/CN14/RD5 * echo
*/
void startTrigger(){
PORTDSET = 0x800; //sets bit 11, starts the trigger
takeStartTime = true;
}
void stopTrigger(){
PORTDCLR = 0x800; //clear bit 11 of port D, ie stop the trigger
}
bool echoIsUp(){
return (PORTD & 0x20);
}
long calculateDistance(){
return ((echoStop-echoStart)*5)/58; //5* == little hack 1/58 == 34000/2*10^-6
}
void updateSensor(long tick){
if(takeStartTime){
echoStart= tick;
takeStartTime= false;
}
if(echoIsUp()){
echoStop = tick;
echoHasBeenTriggered = true;
}
if(echoHasBeenTriggered && !echoIsUp()){
echoHasBeenTriggered = false;
globalDistance = calculateDistance();
}
}
long readSensor(){
return globalDistance;
}
#endif