-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkingSensor.ino
143 lines (135 loc) · 3.14 KB
/
ParkingSensor.ino
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "avr/interrupt.h"
#include <LiquidCrystal.h>
const uint8_t frontSensor = 5, rearSensor = 6;
const uint8_t D5 = 0b00100000, D6 = 0b01000000;
uint8_t sensorID = 0, distance = 0, fprintable = 0, rprintable = 0, loc = 0;
volatile uint16_t rintr = 0, rintf = 0, rvalue = 0;
volatile uint16_t fintr = 0, fintf = 0, fvalue = 0;
volatile uint16_t delta = 0;
volatile uint8_t rctr = 0, fctr = 0;
volatile uint8_t lastPCINT2state=0, thisPCINT2state=0;
uint8_t F1 = 0, F2 = 0, F3 = 0, F4 = 0;
uint8_t R1 = 0, R2 = 0, R3 = 0, R4 = 0;
LiquidCrystal lcd(12, 11, 7, 8, 9, 10);
uint16_t tempSum = 0, tempIn[10] = {0,0,0,0,0,0,0,0,0,0};
uint8_t tempPin = 1, tmpArrayCurrent = 0;
float temp = 0.0;
uint16_t deltaT = 0, deltaF = 0, deltaR = 0;
void setup() {
pinMode(frontSensor, INPUT);
pinMode(rearSensor, INPUT);
analogReference(INTERNAL); //change reference to 1.1V instead of 5V
cli();
PCICR = PCICR | 0b100;
PCMSK2 = PCMSK2 | 0b01100000; //Interrupts on PCINT 21, and 22
sei();
lcd.begin(20, 4);
}
void loop() {
if(fprintable) {
distance = fvalue & 0xFF;
sensorID = (fvalue>>8) & 0x07;
fprintable = 0;
if(sensorID == 0) {
loc = 1;
}
else if(sensorID == 1) {
loc = 6;
}
else if(sensorID == 2) {
loc = 11;
}
else if(sensorID == 3) {
loc = 16;
}
lcd.setCursor(loc, 1); lcd.print(" ");
lcd.setCursor(loc, 1); lcd.print(distance);
deltaF = millis();
}
if(rprintable) {
distance = rvalue & 0xFF;
sensorID = (rvalue>>8) & 0x07;
rprintable = 0;
if(sensorID == 0) {
loc = 1;
}
else if(sensorID == 1) {
loc = 6;
}
else if(sensorID == 2) {
loc = 11;
}
else if(sensorID == 3) {
loc = 16;
}
lcd.setCursor(loc, 2); lcd.print(" ");
lcd.setCursor(loc, 2); lcd.print(distance);
deltaR = millis();
}
if((millis() - deltaT) >= 2500) {
deltaT = millis();
temperatureRead();
lcd.setCursor(14, 0);
lcd.print(temp, 1); lcd.write(223); lcd.print("C");
}
}
void temperatureRead() {
tempSum = tempSum - tempIn[tmpArrayCurrent];
tempIn[tmpArrayCurrent] = analogRead(tempPin);
tempSum = tempSum + tempIn[tmpArrayCurrent];
if(tmpArrayCurrent == 9) {
tmpArrayCurrent = 0;
}
else {
tmpArrayCurrent = tmpArrayCurrent + 1;
}
temp = tempSum * 11.0 / (1023);
}
ISR(PCINT2_vect) {
thisPCINT2state = PIND;
if((thisPCINT2state ^ lastPCINT2state) & D5) {
if (fctr == 16) {
fctr = 0;
fprintable = 1;
}
else if (PIND & D5) // pin 5 direct port access {
fintr = micros();
}
else {
fintf = micros();
delta = fintf - fintr;
if (delta <150) {
fvalue = fvalue << 1;
fctr = fctr + 1;
}
else if (delta < 300) {
fvalue = fvalue << 1;
fvalue = fvalue | 1;
fctr = fctr + 1;
}
}
}
else if((thisPCINT2state ^ lastPCINT2state) & D6) {
if (rctr == 16) {
rctr = 0;
rprintable = 1;
}
else if (PIND & D6) // pin 6 direct port access {
rintr = micros();
}
else {
rintf = micros();
delta = rintf - rintr;
if (delta <150) {
rvalue = rvalue << 1;
rctr = rctr + 1;
}
else if (delta < 300) {
rvalue = rvalue << 1;
rvalue = rvalue | 1;
rctr = rctr + 1;
}
}
}
lastPCINT2state = thisPCINT2state;
}