I've been messing with this for a bit, and here's the current state of affairs:
- The box and tupperware are a great solution. Done here for a while.
- I can make the servo work with external power supply, since The Internet says the 5V Arduino pin is sketchy and The Internet Never Lies.
- I can get data off the magnetometer and print to serial.
- I can get the servo to move based on the magnetometer, if I just map the input to the servo.
- I cannot get consistent and accurate data off the magnetometer, so having it hooked up to the servo is pretty useless.
I'm having 2 main problems with the data.
First, if I just use the example code for the magnetometer and leave it alone, I get consistent baseline data. If I move a magnet close to it, the data changes in the appropriate way. But when I remove the magnet, the data never returns to baseline. To be honest I'm having trouble coming up with possible causes for this problem. If it was a strong magnet or if it was there for a long time, it may magnetize nearby metal to cause this, but it's a weak magnet only near it for a few seconds.
Second problem: I want to establish a baseline, poll the data every 1-2 seconds, then use a simple if statement to move the servo motor if the data deviates from the baseline. To do this I need to get multiple readings and compare them. Every time I write a sketch where it reads the data more than once, I get a pattern: first read gets good data, second read gets mystery numbers that are NOT real data, third read good data, and it alternates like that. It does this even if I use different variable names for the first and second sets of data. It does this even if I reset the variables to 0 before the end of the loop. Pretty sure this has to do with bit math, somehow, which I'm 100% ignorant of.
Next steps: Learn more about the raw data that the magnetometer sends, learn more about the bit math it uses to convert it into the serial output I see, probably find out what 'registers' are because that seems important.