1 | byte n = 0; // Interrupt Bit Count
|
2 | volatile byte pos = 0; // Values Position Count
|
3 | volatile byte values[5] = {
|
4 | 0,0,0,0,0}; // Values to be stored by sensor
|
5 | byte cbit = 0; // Current bit read in
|
6 |
|
7 | boolean irFlag = false; // Flag to indicate IR reading has been made
|
8 | boolean ambFlag = false; // Flag to indicate ambient temp reading has been made
|
9 |
|
10 |
|
11 | byte irValues[5] = {
|
12 | 0,0,0,0,0}; // Variable to store IR readings
|
13 | byte ambValues[5] = {
|
14 | 0,0,0,0,0}; // Variable to store Ambient readings
|
15 |
|
16 | const int len = 5; // Length of values array
|
17 | const int clkPin = 3; // Pins
|
18 | const int dataPin = 7;
|
19 | const int actionPin = 9;
|
20 |
|
21 | void setup(){
|
22 | Serial.begin(9600);
|
23 |
|
24 | pinMode(clkPin, INPUT); // Initialize pins
|
25 | pinMode(dataPin, INPUT);
|
26 | pinMode(actionPin, OUTPUT);
|
27 | digitalWrite(clkPin, HIGH);
|
28 | digitalWrite(dataPin, HIGH);
|
29 | digitalWrite(actionPin, HIGH);
|
30 | delay(200);
|
31 | Serial.println("Type to Start..."); // Wait for input to start
|
32 | //while(!Serial.available());
|
33 | Serial.println("Starting...");
|
34 | Serial.println("IR (C), Ambient (C), Time Since Start (ms)");
|
35 |
|
36 | attachInterrupt(1,tn9Data,FALLING); // Interrupt
|
37 | digitalWrite(actionPin,LOW);
|
38 | delay(2000); // Make sensor start sending data
|
39 | }
|
40 |
|
41 | void loop(){
|
42 |
|
43 |
|
44 |
|
45 | if(pos == len && values[0] == 0x4C){ // If sensor has sent IR packet...
|
46 | for(int i = 0; i < len; i++){ // Store values to irValues
|
47 | irValues[i] = values[i];
|
48 | }
|
49 | irFlag = true; // Indicate IR reading
|
50 | pos = 0;
|
51 | digitalWrite(actionPin,LOW); // Make sensor start sending data
|
52 | }
|
53 |
|
54 | if(pos == len && values[0] == 0x66){ // If sensor has sent ambient packet...
|
55 | for(int i = 0; i < len; i++){ // Store values to ambValues
|
56 | ambValues[i] = values[i];
|
57 | }
|
58 | ambFlag = true; // Indicate Ambient reading
|
59 | pos = 0;
|
60 | digitalWrite(actionPin,LOW); // Make sensor start sending data
|
61 | }
|
62 |
|
63 | if(pos == len && values[0] == 0x53){ // If sensor has sent junk packet
|
64 | pos = 0;
|
65 | digitalWrite(actionPin,LOW); // Make sensor start sending data
|
66 | }
|
67 |
|
68 | if(irFlag && ambFlag){ // If successful IR and Ambient reading...
|
69 | digitalWrite(actionPin,HIGH); // Make sensor stop sending data. Because Timing is weird, I want to ensure the interrupts do not happen during this section.
|
70 | word tempword = 0; // Next 4 lines isolate temperature component of values
|
71 | tempword = tempword | irValues[1];
|
72 | tempword = tempword << 8;
|
73 | tempword = tempword | irValues[2];
|
74 | if(tn9Check(irValues)){ // If checksum is valid, print IR temperature
|
75 | //Serial.print("IR = ");
|
76 | Serial.print(int(tempword)/16.0 - 273.15);
|
77 | Serial.print(", ");
|
78 | }
|
79 | else{ // If checksum isn't valid, print impossible temp
|
80 | //Serial.print("IR = ");
|
81 | Serial.print("-273.15, ");
|
82 | }
|
83 |
|
84 | tempword = 0; // Isolate temperature component again for ambient
|
85 | tempword = tempword | ambValues[1];
|
86 | tempword = tempword << 8;
|
87 | tempword = tempword | ambValues[2];
|
88 | if(tn9Check(ambValues)){ // If checksum is valid, print ambient temperature
|
89 | //Serial.print("Amb = ");
|
90 | Serial.print(int(tempword)/16.0 - 273.15);
|
91 | }
|
92 | else{ // If checksum isn't valid, print impossible temp
|
93 | //Serial.print("Amb = ");
|
94 | Serial.print("-273.15");
|
95 | }
|
96 | irFlag = false; // Reset flags
|
97 | ambFlag = false;
|
98 |
|
99 | Serial.print(", ");
|
100 | Serial.println(millis());
|
101 |
|
102 | Serial.println(digitalRead(actionPin)); // Print time for logging purposes
|
103 | delay(2000); // Simulate other sensors or code
|
104 | digitalWrite(actionPin,LOW); // Make sensor start sending data
|
105 | }
|
106 |
|
107 |
|
108 | }
|
109 |
|
110 |
|
111 | void tn9Data(){ // Interrupt Function
|
112 | cbit = digitalRead(dataPin); // Read bit
|
113 | if(pos >= len) pos = 0; // Keep index below 5
|
114 | values[pos] = (values[pos] << 1) | cbit; // Store to values
|
115 | n++; // Increment bit count
|
116 | if(n == 8){ // Increment position count based on bits read in
|
117 | pos++;
|
118 | n = 0;
|
119 | }
|
120 | if(pos == len){
|
121 | for(int i = 0; i < len; i++){ // Store values to ambValues
|
122 | Serial.print(values[i]);
|
123 | Serial.print(" ");
|
124 | }
|
125 |
|
126 | Serial.print("\n"); // If complete "packet" sent, stop sensor from sending
|
127 | Serial.println(digitalRead(actionPin));
|
128 | digitalWrite(actionPin,HIGH); // again until main loop allows it.
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 | boolean tn9Check(byte tn9Values[]){ // Checksum calculating function
|
134 | int mcheck = (int)tn9Values[0] + (int)tn9Values[1] + (int)tn9Values[2]; // Checksum calculation
|
135 | int scheck = (int)tn9Values[3]; // Checksum sent by sensor
|
136 | boolean crc = false; // Initialize return value
|
137 | if(mcheck > 510) mcheck = mcheck - 512; // Handle sensor byte rollover
|
138 | if(mcheck > 255) mcheck = mcheck - 256; // Handle sensor byte rollover
|
139 | if(mcheck == scheck) crc = true; // Check checksum
|
140 | return(crc); // Return
|
141 | }
|