1+ /* Read the IR sensor and try to figure out where the heat is located.
2+ */
3+
4+ #include " HeatSensor.h"
5+
6+ #include < Wire.h>
7+ #include < Adafruit_AMG88xx.h>
8+
9+ Adafruit_AMG88xx amg;
10+
11+ float pixels[AMG88xx_PIXEL_ARRAY_SIZE];
12+
13+ void HeatSensor::setup ()
14+ {
15+ x = 0 ;
16+ y = 0 ;
17+ magnitude = 0 ;
18+
19+ bool status;
20+
21+ // default settings
22+ status = amg.begin ();
23+ if (!status) {
24+ Serial.println (" Could not find a valid AMG88xx sensor, check wiring!" );
25+ while (1 );
26+ }
27+
28+ yield ();
29+ delay (100 ); // let sensor boot up
30+ }
31+
32+ // Find the approximate X and Y values of the peak temperature in the pixel array,
33+ // along with the magnitude of the brightest spot.
34+ void HeatSensor::find_focus ()
35+ {
36+ amg.readPixels (pixels);
37+ yield ();
38+
39+ x = 0 , y = 0 , magnitude = 0 ;
40+ float minVal = 100 , maxVal = 0 ;
41+ int i = 0 ;
42+ for (float yPos = 3.5 ; yPos > -4 ; yPos -= 1.0 ) {
43+ for (float xPos = 3.5 ; xPos > -4 ; xPos -= 1.0 ) {
44+ float p = pixels[i];
45+ x += xPos * p;
46+ y += yPos * p;
47+ minVal = min (minVal, p);
48+ maxVal = max (maxVal, p);
49+ i++;
50+ }
51+ }
52+ x = - x / AMG88xx_PIXEL_ARRAY_SIZE / 5.0 ;
53+ y = y / AMG88xx_PIXEL_ARRAY_SIZE / 5.0 ;
54+ x = max (-1.0 , min (1.0 , x));
55+ y = max (-1.0 , min (1.0 , y));
56+ magnitude = max (0 , min (50 , maxVal - 20 ));
57+
58+ // Report.
59+ #define SERIAL_OUT 3
60+ #if SERIAL_OUT == 1
61+ // Print raw values
62+ Serial.print (" [" );
63+ for (int i=1 ; i<=AMG88xx_PIXEL_ARRAY_SIZE; i++){
64+ Serial.print (pixels[i-1 ]);
65+ Serial.print (" , " );
66+ if ( i%8 == 0 ) Serial.println ();
67+ }
68+ Serial.println (" ]" );
69+ Serial.println ();
70+ #endif
71+ #if SERIAL_OUT == 2
72+ // Print character-graphic array
73+ const char charPixels[] = " .-*o0#" ;
74+ Serial.println (" ========" );
75+ for (int i = 1 ; i <= AMG88xx_PIXEL_ARRAY_SIZE; i++) {
76+ int val = min (5 , round (max (0 , pixels[i-1 ] - 20 ) / 2 ));
77+ Serial.print (charPixels[val]);
78+ if (i % 8 == 0 )
79+ Serial.println ();
80+ }
81+ Serial.println ();
82+ #endif
83+ #if SERIAL_OUT == 3 || SERIAL_OUT == 2
84+ // Print coordinates and brightness
85+ Serial.print (x);
86+ Serial.print (' ' );
87+ Serial.print (y);
88+ Serial.print (' ' );
89+ Serial.println (magnitude);
90+ #endif
91+ }
92+
93+ /*
94+ void loop() {
95+ // Read all the pixels
96+
97+ // Find the focal point.
98+ float x, y, magnitude;
99+ find_focus(x, y, magnitude);
100+
101+ // Set diagnostic LEDs.
102+ analogWrite(CENTER_LED, round(max(0, magnitude / 30) * 255));
103+ analogWrite(RIGHT_LED, round(min(1.0, max(0.0, -x / 3)) * 255));
104+ analogWrite(LEFT_LED, round(min(1.0, max(0.0, x / 3)) * 255));
105+ analogWrite(UP_LED, round(min(1.0, max(0.0, y / 3)) * 255));
106+ analogWrite(DOWN_LED, round(min(1.0, max(0.0, -y / 3)) * 255));
107+
108+ delay(200);
109+ }
110+ */
0 commit comments