Button State Test

Hello, may someone, please help with this code? Everytime I press the button the Arduino Mega 2560 sometimes reads it as HIGH to LOW, but most the time the button is HIGH. The hardware is below (so is the code). I basically want to toggle the button, like a switch, while printing on or off in the Serial Monitor.

#include <Bounce2.h>

#define button 36

Bounce debouncer = Bounce(); // Instantiate a Bounce object

void setup() {
  // put your setup code here, to run once:
   debouncer.attach(button,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
  Serial.begin(9600);
  debouncer.interval(25); // Use a debounce interval of 25 milliseconds
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState();
  debouncer.update(); // Update the Bounce instance
}
void buttonState() {
  if (debouncer.fell()) {
    Serial.println("ON!");
  }
  else{
  delay(1000);
  Serial.println("OFF!");
}
}

The way to do this is contained in the bwod example : IDE -> file/examples/digital/blinkwithoutdelay. You're on the right path using the debouncer.

Your circuit as well as using "INPUT_PULLUP" is good.
What responce do You get if You press the button and keep it down?

The response I get from the serial monitor is "ON!" then "OFF!"

I don't know the debounce functions so I ask You to check them up, exactly what do they do, what value do they return etc..
Maybe @dougp knows more.

Link to what I am talking about: Bounce2 functions.

  if ( debouncer.fell() ) {  // Call code if button transitions from HIGH to LOW

This tells that a HIGH to LOW transition has occured. At the next call this function will return a false value as I understand.

As I think…...When You have one level of logic You need to test for the other level occuring. You need to remember in what state You have and use the proper logic.
Is there a function like debouncer.raise for transistions from LOW to HIGH?

I am not sure, that's all the resources I have got.

Can You open Bounce2.h and look for possible function names? Eventually Bonce2.cpp and look?

How do I do that?

Double click on the file. Maybe choose the opening program. Those files are just text files so Notepad or any text editor will do fine.

Where is the file?

You must have downloaded the Bounce2-files into a library folder. Enter that library and carry on the investigation.

I am looking through the files (I found .cpp and .h) and I do not see any debouncer.raise. "debouncer" doesn't even exist. It's all debounce. Even in the examples. I'll send a the folder to you so you can see. There is also another page on the forum for this subject.

Bounce2-master.zip (248 KB)

Can You a search for the text "fell" and se what it looks like, where it's found? Then look for similiar constructions, names near that "fell".

The text "fell" is found in examples--->bounce_multiple. It can also be found in examples--->change. That's all I see closest to "fell."

That's good. Can You find any other debouncer.xxx lines? xxx would be other available functions in that library.

There ought to be some documenting file like "readme" or something, telling what that library can do. Look for that.

Yes, I can find other available functions in the library.

Very good. What names do You find? Anything You feel migh be useful regarding our previous dicussion?

Railroader:

  if ( debouncer.fell() ) {  // Call code if button transitions from HIGH to LOW

This tells that a HIGH to LOW transition has occured. At the next call this function will return a false value as I understand.

Yes. Look at the 'change' example of bounce2.

Railroader:
Is there a function like debouncer.raise for transistions from LOW to HIGH?

The corollary of debouncer.fell() is debouncer.rose().