The circuit I'm building is a fairly simple motion activated motor, consisting of a button, an LED, and a motor. The button starts out as "false" and the LED is "low", meaning the motor is not sensitive to motion. Then when the button is pushed it becomes "true" and the LED becomes "high", which indicates that the motor is now sensitive to motion. Then, I have a simple if statement that turns on the motor if the analog input level is more than a certain number (i.e. when something passes in front of the sensor). I will be putting this on a small battery powered car so that it is initiated by another passing car.
When the Arduino is powered through USB from a computer, I can let the motion detector sit there all day and not trigger the motor until something actually crosses its path (like it's suppose to). I can also watch the serial monitor and see the different values the motion detector is inputting, with none of them being above the critical level until something actually passes in front of it. This leads me to conclude that both my circuit and sketch work. However, my problem is that when I power the Arduino using a 9V battery connected by a 2.1mm power plug (that I soldered myself) instead of by USB, the circuit no longer works and become quite unpredictable! When I press the button to activate the motion sensitive motor, the motor goes off within a matter of seconds regardless if anything passes over it. Can anyone explain what might be happening and any solutions? My circuit would be rather useless if I had to connect my car to a computer for it to work...
Any help would be greatly appreciated. Thanks in advance!