PulseIn() Unreliable with Pushbutton

I'm pretty new to the Arduino, but I've been writing code in C++ for a while. Anyway, I'm trying to make a very simple setup - a pushbutton connected to an Arduino, which is connected via USB to a computer. When a button is held down, I need the computer to display how long the button was held down for.

After looking it up in the Reference section, this seemed to be extremely easy, using the PulseIn() function. I copied the code directly from http://arduino.cc/en/Reference/PulseIn, with the addition of Serial.begin in setup and serial.println(duration) in the loop. The board and pushbutton are connected like this http://arduino.cc/en/Tutorial/Button. The resistor is 1k.

I uploaded the code, opened the serial monitor, and started pushing the button. Out of every 10 or so pushes, maybe 4 or 5 would register and come up on the screen. I've made sure that all of the wires are making contact properly, and I've tried switching the button. I don't think the problem is one of debouncing, because an undebounced signal should give me some noise, whereas my problem is that I'm not getting enough of anything. If I hit the button 10 times, I'd see four numbers on the screen, as if I had only hit the button 4 times.

I've considered rewriting the PulseIn() function with a loop, an if statement, and the millis() function - if the pin is high, record the current time. Then while the pin is still high, constantly store the new time. Once it goes low, compare the newest time you have to the time you recorded when it was first pressed - the difference is the amount of time the button was pushed. I'm guessing that this is how PulseIn() works anyway, minus the timeouts. I'd rather not rewrite prebuilt functions, and I'm not even sure that all of that work would solve the problem.

I'm not sure if this makes a difference but I have a Duemilanove and I'm running the IDE in Windows 7

Any help would be greatly appreciated!

Any help would be greatly appreciated!

How about posting your code and let other eye balls look for something you might have missed?

Lefty

Like I said before I copied the code directly from http://arduino.cc/en/Reference/PulseIn, but i'll rewrite it with my changes anyway

int pin = 7;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
  Serial.begin(9600);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}

The code works perfectly for me.

Did you wire the button to pin 2 (that's how it's wired in the Button tutorial)?

Are you certain you're using a 1K resistor?

I either switched the pin in the code or moved the wire, I don't remember which but they're the same thing I guess. I'm sure that it's a 1k resistor but I can always check its resistance to make sure. Should I try using a smaller resistor? I'm not quite sure what to do at this point, I've cut my program down to nothing more than one function and I don't get regular results as I think I should. Any other suggestions? Are there any known problems with this function?

Should I try using a smaller resistor?

No. 1K is a good choice.

Are there any known problems with this function?

None that I'm aware of. Which version of the Arduino IDE are you using?

I have no idea what to suggest. The code works for me and you're confident the hardware is wired correctly. There isn't anything left to change!

Hopefully, someone else can offer a suggestion.

While you are measuring your resisitor you might also want to measure the switch to see if you get reliable switch action on a ohm meter (if your meter has a continuity buzzer mode, use that). Or try another switch, you might have bad/flaky switch contacts.

Lefty

I'm using version 17 of the IDE.

After checking both the resistor and the pushbutton and finding them to be fine, I decided to rewrite the PulseIn() function, and found that my function works just as I needed it to - every single time I push the button, I get a number in the Serial Monitor for how long I pushed it.

I'll post the code here in case anyone else has the same problem that I did and stumbles upon this post. There are two small caveats to my code - the first is that I did not include a timeout, because it begins as an If statement, and so if you wanted a timeout you could just put this code within a loop and then break out of the loop after a certain amount of time. The second caveat is that because I use the millis() function, the result is in milliseconds, and not microseconds like that of the PulseIn() function. My project didn't require microsecond precision, though.

int inputPin = 2; 
int pulseDuration = 0;
int pulseBegin = 0;


void setup()
{
  pinMode(inputPin, INPUT);
  Serial.begin(9600); 
}

void loop()
{
     if (digitalRead(inputPin) == HIGH)
       {
         pulseBegin = millis();
         while (digitalRead(inputPin) == HIGH){}
         pulseDuration = millis()-pulseBegin;
         Serial.println(pulseDuration);
       }
}

The algorithm is as follows: First, check to see if the pin is HIGH. If it is, immediately store the current time in "pulseBegin". As long as the pin is high, do nothing (hence the "while" loop with nothing inside the brackets). The Arduino will break out of this loop as soon as the condition is no longer met, which in this case means that it will move on to the next line when the pin becomes a LOW. As soon as it does become a low, subtract the time at which you started pressing the button from the current time, which leaves you with the duration of the pulse. Then print that duration to the Serial Monitor.

Hope this helps someone in the future!

Thank you for following-up.