-
Notifications
You must be signed in to change notification settings - Fork 2
/
HeatSensor.cpp
executable file
·117 lines (101 loc) · 2.92 KB
/
HeatSensor.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
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
// SPDX-FileCopyrightText: 2019 teejaydub for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#if 0 // Change to 1 to enable this code (referenced by user_watch.cpp)
// CORRESPONDING LINE IN user_watch.cpp MUST ALSO BE ENABLED!
/* Read the IR sensor and try to figure out where the heat is located.
*/
#include "HeatSensor.h"
#include <Wire.h>
#include <Adafruit_AMG88xx.h>
Adafruit_AMG88xx amg;
float pixels[AMG88xx_PIXEL_ARRAY_SIZE];
void HeatSensor::setup()
{
x = 0;
y = 0;
magnitude = 0;
bool status;
// default settings
status = amg.begin();
if (!status) {
Serial.println("Could not find a valid AMG88xx sensor, check wiring!");
while (1);
}
yield();
delay(100); // let sensor boot up
}
// Find the approximate X and Y values of the peak temperature in the pixel array,
// along with the magnitude of the brightest spot.
void HeatSensor::find_focus()
{
amg.readPixels(pixels);
yield();
x = 0, y = 0, magnitude = 0;
float minVal = 100, maxVal = 0;
int i = 0;
for (float yPos = 3.5; yPos > -4; yPos -= 1.0) {
for (float xPos = 3.5; xPos > -4; xPos -= 1.0) {
float p = pixels[i];
x += xPos * p;
y += yPos * p;
minVal = min(minVal, p);
maxVal = max(maxVal, p);
i++;
}
}
x = - x / AMG88xx_PIXEL_ARRAY_SIZE / 5.0;
y = y / AMG88xx_PIXEL_ARRAY_SIZE / 5.0;
x = max(-1.0, min(1.0, x));
y = max(-1.0, min(1.0, y));
magnitude = max(0, min(50, maxVal - 20));
// Report.
#define SERIAL_OUT 3
#if SERIAL_OUT == 1
// Print raw values
Serial.print("[");
for(int i=1; i<=AMG88xx_PIXEL_ARRAY_SIZE; i++){
Serial.print(pixels[i-1]);
Serial.print(", ");
if( i%8 == 0 ) Serial.println();
}
Serial.println("]");
Serial.println();
#endif
#if SERIAL_OUT == 2
// Print character-graphic array
const char charPixels[] = " .-*o0#";
Serial.println("========");
for (int i = 1; i <= AMG88xx_PIXEL_ARRAY_SIZE; i++) {
int val = min(5, round(max(0, pixels[i-1] - 20) / 2));
Serial.print(charPixels[val]);
if (i % 8 == 0)
Serial.println();
}
Serial.println();
#endif
#if SERIAL_OUT == 3 || SERIAL_OUT == 2
// Print coordinates and brightness
Serial.print(x);
Serial.print(' ');
Serial.print(y);
Serial.print(' ');
Serial.println(magnitude);
#endif
}
/*
void loop() {
// Read all the pixels
// Find the focal point.
float x, y, magnitude;
find_focus(x, y, magnitude);
// Set diagnostic LEDs.
analogWrite(CENTER_LED, round(max(0, magnitude / 30) * 255));
analogWrite(RIGHT_LED, round(min(1.0, max(0.0, -x / 3)) * 255));
analogWrite(LEFT_LED, round(min(1.0, max(0.0, x / 3)) * 255));
analogWrite(UP_LED, round(min(1.0, max(0.0, y / 3)) * 255));
analogWrite(DOWN_LED, round(min(1.0, max(0.0, -y / 3)) * 255));
delay(200);
}
*/
#endif