How to understand ' Bounce b = Bounce(); ' please?

Hi everyone,

The button2 doc. has INSTANTIATE:

#include <Bounce2.h>
Bounce b = Bounce(); // Instantiate a Bounce object

what the Bounce() means, why can't use: Serial.print(b); ?

ref.

thanks
adam

Read the comment. That line of code instantiates an instance (!) of an object of type Bounce named b.

I'm not sure what sense it would make or even if you could print that object.

What is it that you wish to accomplish with

  Serial.print(b); 

?

If you want to use that library, read the documentation. I don't think you'll see anyone trying to Serial.print a Bounce object.

a7

1 Like

Thank you.

Did you want to print the button’s state?

1 Like

Thank you J-M-LJackson

actually I made: Serial.print(b); didn't work when I saw: Bounce b = Bounce();, what data look like of b?

There's no better way to learn more about how it works and what a Bounce even is than to go to the source:

Bounce2 github page.

If that isn't the same library, google should turn up the source code for your perusal.

Also - any code that ends up in a sketch you compile and run is somewhere on your computer, the source code for it that is. Every library available for study.

I will say that some libraries are worth more time as far as learning about how to do things than others.

a7

1 Like

Thanks.

Are you familiar with C++ and classes?

1 Like

Thanks.
not familiar much.
BTW. I got button’s state by: b.update();

OK, but that's only telling you that the button changed state, not what state it changed to.

There are a number of other functions that might be more useful, please look at some of the examples on the GitHub site to see things like pressed, isPressed, fell, rose and so forth.

I don't use that library, just basing my reply on a hasty glance at the code.

You do have to call uodate() frequently, that's when it looks at the button and decides what to do to track it.

You will see the update() function often as the first thing to do every time you start you loop() code again.

a7

1 Like

Sure, I'll do more examples.
Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.