Counting rain with tipping bucket

Hi

I got a cheap tipping bucket rain gauge off eBay. When the bucket tips, the two attached wires connects for around half a second. I'd like to count that event.
Browsing around got me interested in the interrupt function but I can't quite get it working. I recon my Uno uses pin2 for interrupt 0 and experimented with both 3.3v, 5v and GND for the other one - rising/falling.

I use this code:

int x = 0;  

void setup() {                
  attachInterrupt(0, increment, RISING);
  Serial.begin(9600);  
}

void loop() {
  delay(3000);
  Serial.println(x, DEC); 
}

void increment() { 
    x++;
}

Running it sometimes types me a zero. Other times it gives me for example 151, 300, 450 etc. What are we counting? Something 50 Hz for 3 seconds? Sounds like the grid AC in my house.
If I tip the bucket, sometimes the counting stops in the middle position, other times nothing happens.
How can I make one tip (perhaps 500ms connection) count as +1? I plan on actually making my rain guage count millimeters of water.

Thanks in advance,
Chris

Well, you browsed to the wrong places. An interrupt is not a good way to handle such a slow event. But your problem is likely that your switch is not de-bounced. You will have to do that whether it is done with an interrupt or not.

De-bouncing sounds kinda cool. But I don't have any idea what it is.

When in doubt, try Google. "switch debouncing"

You will need a pullup resistor on the input you use.

jremington:
When in doubt, try Google. "switch debouncing"

You will need a pullup resistor on the input you use.

Thank you for a long and informative answer. You make newbies feel welcome.
Your post/karma ratio is a mystery. So unfair!

cveng:
I use this code:

You forgot to declare your variable x 'volatile'.

Besides of that: Do you have a pull-up or pull-down resistor connected to your pin?

If not, you better use the internal pull-up resistor:

volatile int x = 0;  

void setup() {                
  pinMode(2,INPUT_PULLUP);
  attachInterrupt(0, increment, RISING);
  Serial.begin(9600);  
}

Then you should get results at least.

But for perfect working code you might need additional debouncing as well as atomic read of x.

Thanks, Jurs.
Just solved it though. It's counting away.

const unsigned int DEBOUNCE_TIME = 40;
const unsigned int REFRESH_TIME = 4000;

byte regnInterrupt = 0;
volatile int mm = 0;

static unsigned long last_interrupt_time = 0;
  
void setup() {
  Serial.begin(9600);
  attachInterrupt(regnInterrupt, tael, FALLING);
}

void loop() {
    Serial.print("Regnmaengde: ");
    Serial.print(mm);
    Serial.println(" mm");
    delay(REFRESH_TIME);
}

void tael() {
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > DEBOUNCE_TIME) {
   mm++;
  }
  last_interrupt_time = interrupt_time; 
}

Didn't need any resistor.
Now I need to get the logging working. Whole new chapter.

Kind regards,
Chris

cveng:
Didn't need any resistor.

The purpose of the resistor is to guarantee the state of the pi when the switch is open. An Arduino I/O pin that is not connected to anything can randomly assume HIGH or LOW values and change rapidly between them.

The Atmega MCU has internal resistors that can be selected with pinMode(pinNum, INPUT_PULLUP); That guarantees that the pin reads HIGH until the external switch connects it to GND and pulls it LOW.

...R

Didn't need any resistor.

Fit one anyway for peace of mind, or use the internal one.

Thank you for a long and informative answer. You make newbies feel welcome.

I take it you could not be bothered to try to googling "switch debouncing", or to take the suggestion to use a pullup resistor seriously.

When you do the google search, the very first hit that turns up (in my location at least) is remarkably informative, and titled "A guide to debouncing". http://www.eng.utah.edu/~cs5780/debouncing.pdf

I'm with @jremington on this.

If Reply #3 had been directed to me I think I would have responded with "Sorry, I forgot about Google"

Google (and its ilk) is THE resource for programmers.

...R

Robin2:
Google (and its ilk) is THE resource for programmers.

I'll tell Arduino to shut down the forum then...

cveng:

Google (and its ilk) is THE resource for programmers.

I'll tell Arduino to shut down the forum then...

Google is just for searching. I regularly use Google to find stuff in this Forum.

...R