My fat cat always steals my skinny cat's food, making my fat cat fatter and my skinny cat skinnier. I can't be around to yell at her all day, so I'm making a food dish with an automatic lid that only opens for the skinny cat.
Basic concept: Magnet on the skinny cat's collar. 3 axis magnetometer detects it, talks to an arduino, which moves a servo to open the lid.
Disclaimer: I'm a wayward biologist. My previous code experience is R and my previous hardware experience is building a Dobsonian telescope. I have no idea what I'm doing.
Files
InitialDataTestv3.ino
Ideally, the arduino would get 10 initial values on startup and use that to create a baseline. Then it uses the baseline to determine when the magnet is near, because the baseline changes.
This is the sketch that alternates between good data and mystery numbers every other read.
Modified from Sparkfun's magnetometer sample code.
Testing the magnetometer to see if it can get sensible data. This is the sketch which gets good data, which changes sensibly when you move a magnet closer, but never returns to baseline when you remove the magnet.
Modified from Sparkfun's magnetometer sample code.
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.
Great initiative with your project! Your magnetometer might be dealing with residual magnetism, which could be causing the data anomaly post-magnet removal. A 'self-calibration' routine may be helpful. The inconsistent readings might be a timing issue - you might want to add a delay in your loop.
Understanding the raw data and the bit math will surely help you advance in the right direction! Wishing you the best of luck. For those who are curious about cat behaviors, particularly why some tabby cats tend to become overweight, you might find this article informative: Why Are Tabby Cats Fat?. Keep learning and pushing forward!
Great initiative with your project! Your magnetometer might be dealing with residual magnetism, which could be causing the data anomaly post-magnet removal. A 'self-calibration' routine may be helpful. The inconsistent readings might be a timing issue - you might want to add a delay in your loop.
Understanding the raw data and the bit math will surely help you advance in the right direction! Wishing you the best of luck. For those who are curious about cat behaviors, particularly why some tabby cats tend to become overweight, you might find this article informative: Why Are Tabby Cats Fat?. Keep learning and pushing forward!