I'm trying to set up my Arduino Duemilanove as a motion sensor that I don't have to keep plugged into my computer. The idea is that I can hook it up to wall power and then plug in my USB so that I check how many times the motion sensor has been activated, but each time I unplug the USB and then plug it back in the counter resets, which makes me think to power goes off when it switches between supplies. I'm wondering if there is a way to modify the board so that I plug and unplug the USB without resetting the program.
You are falling afoul of the Arduino's auto-reset circuitry. There are ways to open the serial port without sending that signal on most platforms, but I don't recall off the top of my head how to do so.
Why don't you tell us how you are accessing the Arduino's serial port and someone else can remind us how to not do an auto-reset.
-j
The Seeeduino has a small switch to enable and disable the automatic reset. If you're thinking about getting another Arduino, it's worth a look.
For now, you can attach a small value resistor between the RESET and 5V, I've seen 110 ohms suggested in these forums.
If you don't expect half a million counts, put the count in EEPROM, and provide your own "reset to 0" function through a regular input pin.
http://www.arduino.cc/playground/Code/EEPROMWriteAnything
Include the two functions in your sketch. Then,
const long timeout = 10000; // new motion after ten seconds
long motions = 0;
long when = 0;
#define COUNT_RESET_BUTTON 13
#define MOTION_DETECT_INPUT 2
void setup()
{
EEPROM_readAnything(0, motions);
Serial.begin(9600);
Serial.print("Current motion count: ");
Serial.println(motions);
pinMode(COUNT_RESET_BUTTON, INPUT);
digitalWrite(COUNT_RESET_BUTTON, HIGH); // internal pullup
pinMode(MOTION_DETECT_INPUT, INPUT);
}
void loop()
{
// if they push the "Reset" button, truly reset to 0 motions
if (digitalRead(COUNT_RESET_BUTTON) == LOW)
{
motions = 0;
EEPROM_writeAnything(0, motions);
}
// if the motion detector was hit, debounce and increment motions
long now = millis();
if (digitalRead(MOTION_DETECT_INPUT) == HIGH &&
(now-when) > timeout)
{
motions++;
EEPROM_writeAnything(0, motions);
when = now;
}
// other stuff
// post the current motions count to an attached LCD display, etc.
}
Thanks Halley, I'm going to go the EEPROM route