1919/* Assign a unique ID to this sensor at the same time */
2020Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345 );
2121
22+ // Replace these two lines with the results of calibration
23+ // ---------------------------------------------------------------------------
24+
2225float raw_mins[2 ] = {1000.0 , 1000.0 };
2326float raw_maxes[2 ] = {-1000.0 , -1000.0 };
27+ // ---------------------------------------------------------------------------
2428
2529float mins[2 ];
2630float maxes[2 ];
@@ -58,7 +62,7 @@ void fill(int red, int green, int blue) {
5862void warm_up (void )
5963{
6064 sensors_event_t event;
61- fill (0 , 0 , 128 );
65+ fill (0 , 0 , 64 );
6266 for (int ignore = 0 ; ignore < 100 ; ignore++) {
6367 mag.getEvent (&event);
6468 delay (10 );
@@ -70,27 +74,30 @@ void warm_up(void)
7074// User needs to rotate the CPX a bunch during this.
7175// Can be refined by doing more of the saem by pressing the A button.
7276// Indicated to the user by green LEDs.
73- void calibrate (void )
77+ void calibrate (bool do_the_readings )
7478{
7579 sensors_event_t event;
7680 float values[2 ];
7781
78- fill (0 , 128 , 0 );
82+ fill (0 , 64 , 0 );
7983
80- unsigned long start_time = millis ();
81- while (millis () - start_time < 5000 ) {
84+ if (do_the_readings) {
85+ unsigned long start_time = millis ();
86+ while (millis () - start_time < 5000 ) {
8287
83- mag.getEvent (&event);
84- values[0 ] = event.magnetic .x ;
85- values[1 ] = event.magnetic .y * -1 ;
86- if (values[0 ] != 0.0 && values[1 ] != 0.0 ) { /* ignore the random zero readings... it's bogus */
87- for (int i = 0 ; i < 2 ; i++) {
88- raw_mins[i] = values[i] < raw_mins[i] ? values[i] : raw_mins[i];
89- raw_maxes[i] = values[i] > raw_maxes[i] ? values[i] : raw_maxes[i];
88+ mag.getEvent (&event);
89+ values[0 ] = event.magnetic .x ;
90+ values[1 ] = event.magnetic .y * -1 ;
91+ if (values[0 ] != 0.0 && values[1 ] != 0.0 ) { /* ignore the random zero readings... it's bogus */
92+ for (int i = 0 ; i < 2 ; i++) {
93+ raw_mins[i] = values[i] < raw_mins[i] ? values[i] : raw_mins[i];
94+ raw_maxes[i] = values[i] > raw_maxes[i] ? values[i] : raw_maxes[i];
95+ }
9096 }
97+ delay (5 );
9198 }
92- delay (5 );
9399 }
100+
94101 for (int i = 0 ; i < 2 ; i++) {
95102 corrections[i] = (raw_maxes[i] + raw_mins[i]) / 2 ;
96103 mins[i] = raw_mins[i] - corrections[i];
@@ -119,7 +126,28 @@ void setup(void)
119126 }
120127
121128 warm_up ();
122- calibrate ();
129+
130+ // If reset with button A pressed or calibration hasn't been done, run calibration and report the results
131+ if (digitalRead (BUTTON_A) || (raw_mins[0 ] == 1000.0 && raw_mins[1 ] == 1000.0 )) {
132+ while (!Serial);
133+ Serial.begin (9600 );
134+ Serial.println (" Compass calibration\n " );
135+
136+ raw_mins[0 ] = 1000.0 ;
137+ raw_mins[1 ] = 1000.0 ;
138+ raw_maxes[0 ] = -1000.0 ;
139+ raw_maxes[1 ] = -1000.0 ;
140+ calibrate (true );
141+
142+ Serial.println (" Calibration results\n " );
143+ Serial.println (" Update the corresponding lines near the top of the code\n " );
144+ Serial.print (" float raw_mins[2] = {" ); Serial.print (raw_mins[0 ]); Serial.print (" , " ); Serial.print (raw_mins[1 ]); Serial.println (" };" );
145+ Serial.print (" float raw_maxes[2] = {" ); Serial.print (raw_maxes[0 ]); Serial.print (" , " ); Serial.print (raw_maxes[1 ]); Serial.println (" };\n " );
146+
147+ while (1 );
148+ } else {
149+ calibrate (false );
150+ }
123151}
124152
125153
@@ -137,7 +165,7 @@ void loop(void)
137165{
138166 // Pressing button A does another round of calibration.
139167 if (digitalRead (BUTTON_A)) {
140- calibrate ();
168+ calibrate (true );
141169 }
142170
143171 sensors_event_t event;
@@ -169,7 +197,7 @@ void loop(void)
169197 leds = led_patterns[direction_index];
170198 for (int pixel = 0 ; pixel < 10 ; pixel++) {
171199 if (pixel == leds[0 ] || pixel == leds[1 ]) {
172- strip.setPixelColor (pixel, 255 , 255 , 255 );
200+ strip.setPixelColor (pixel, 4 , 0 , 0 );
173201 } else {
174202 strip.setPixelColor (pixel, 0 , 0 , 0 );
175203 }
0 commit comments