Git Product home page Git Product logo

bounce2's People

Contributors

chrisgatwin avatar chrivers avatar danforever avatar dwrobel avatar fpistm avatar ivankravets avatar jamesmyatt avatar mic159 avatar michaelcress avatar mkfrey avatar nemoo2580 avatar paulstoffregen avatar per1234 avatar septillion-git avatar t81 avatar thomasfredericks avatar trapper- avatar wamaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bounce2's Issues

Library works on arduino Due but not on Zero (same sketch).

Hello, the title says it all. My code works fine when I upload it on the Due, but if I'll try to compile it for the Zero (SAMD21) I get these errors:

Remote_Controller_v3:50:17: error: 'Bounce2::Button select' redeclared as different kind of symbol In file included from c:\users\anast\onedrive\�������\arduinodata\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\sys\types.h:68:0, from c:\users\anast\onedrive\�������\arduinodata\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\stdio.h:61, from C:\Users\anast\OneDrive\�������\ArduinoData\packages\arduino\hardware\samd\1.8.11\cores\arduino/api/Print.h:22, from C:\Users\anast\OneDrive\�������\ArduinoData\packages\arduino\hardware\samd\1.8.11\cores\arduino/api/ArduinoAPI.h:31, from C:\Users\anast\OneDrive\�������\ArduinoData\packages\arduino\hardware\samd\1.8.11\cores\arduino/Arduino.h:23, from sketch\Remote_Controller_v3.ino.cpp:1: c:\users\anast\onedrive\�������\arduinodata\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4\arm-none-eabi\include\sys\select.h:73:5: note: previous declaration 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' int select __P ((int __n, fd_set *__readfds, fd_set *__writefds, ^~~~~~ C:\Users\anast\OneDrive\Υπολογιστής\Remote controller v.3\Remote_Controller_v3\Remote_Controller_v3.ino: In function 'void setup()': Remote_Controller_v3:171:10: error: request for member 'attach' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' Remote_Controller_v3:172:10: error: request for member 'interval' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' Remote_Controller_v3:173:10: error: request for member 'setPressedState' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' C:\Users\anast\OneDrive\Υπολογιστής\Remote controller v.3\Remote_Controller_v3\Remote_Controller_v3.ino: In function 'void loop()': Remote_Controller_v3:235:10: error: request for member 'update' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' Remote_Controller_v3:270:85: error: request for member 'pressed' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' C:\Users\anast\OneDrive\Υπολογιστής\Remote controller v.3\Remote_Controller_v3\Remote_Controller_v3.ino: In function 'void mainMenu()': Remote_Controller_v3:305:21: error: request for member 'pressed' in 'select', which is of non-class type 'int(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)' exit status 1 'Bounce2::Button select' redeclared as different kind of symbol

Small "Dead Zone"

Hi.

I just ran across your library. Nice, tight code.

I do see one minor issue related to Line 71 of ‘bounce2.cpp’: It happens if the switch state doesn’t change for a long time and millis() rollover occurs. Then, as millis() increases from 0 again, there will be a small dead zone between millis() = previous_millis and millis() = previous_millis + interval_millis where a change of state will not be promptly detected even though the interval period is long-expired. This dead zone will occur every ~1193 hours.

Not a huge deal as it’s a very small window of time, only interval_millis milliseconds long, every 1193 hours. It could be fixed by adding a flag to indicate that the timeout interval has expired.

Thanks.

State change triggered only the first time

Hi all,

I'm using ESP8266 and trying both change and retrigger examples with a button and a led.
State change (rose and fell) is triggered only the first time I push the button and then it becomes unresponsive.
Any idea about the reason?

Thanks

New release and deprecate duration()

Hi Thomas - thank you very much for your excellent library.

Could I request a new release/pull to the Arduino Library repository and if possible, to mark duration() as deprecated?

Many thanks

ESP32 support

Works with the ESP32 Touch interface easilly with a really small change:

#ifdef ESP32
      virtual bool readCurrentState() { return (touchRead(pin) < 40); }
#else
      virtual bool readCurrentState() { return digitalRead(pin); }
#endif
#include <Arduino.h>


/*
 DESCRIPTION
 ====================
 Simple example of the Bounce library that switches the debug LED when a button is pressed.
 */
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>


#define LED_PIN 21

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

void setup() {
  Serial.begin(115200);


  // Setup the Bounce instance for Touch input T7:
  debouncer.attach(T7);
  debouncer.interval(5); // interval in ms

  //Setup the LED :
  pinMode(21,OUTPUT);

}
int ledState = LOW;
void loop() {
  // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int value = debouncer.read();

  // Turn on or off the LED as determined by the state :
  if ( debouncer.fell() ) {

    // Toggle LED state :
    ledState = !ledState;
    digitalWrite(LED_PIN,ledState);

  }

}

With kind regards, gamecompiler :)

WiringPi support

Hi, i would like to use Bounce2 with WiringPi.
Bounce2 is located in my include directory. Im sure id made something really stupid.

Id got the error:

undefined reference to 'Bounce::attach (int)

Here is my code for reference:

/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
/*
 *
 *08.08.2017 - gamecompiler
 *	Der Code wurde so stark verändert das ich defintiv nicht Lazy Foo wegen den paar Zeilen eine Email schreibe.
 *	Auf jeden Fall basiert dieser Code auf Lazy Foos 21. Tut: http://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php
 *	Mit einer Erwähnung von LazyFoos Tutorial ist glaube ich genüge getan.
 *
 *	Dieser Code spielt die Datei "/data/sounds/milshot.wav" ab sobald der GPIO 5 high wird (WiringPi gpio NICHT BCM), und das in einem externen thread. 
 *	Die delay Fkt im Code unten ist ekelhaft, aber da der Sound in einem seperaten thread abgespielt wird ist mir das in diesem Bsp absolut egal.
 *	Zudem ist es nur ein Test wie man SDL nutzt.
 *	
 *	Alleine die Installation von SDL ist eine Katastrophe auf dem RPi. Viel Glück/Spaß
 *
*/


//Include SDL2
#include <SDL.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <string>

//Include WiringPi
#include <wiringPi.h>

//Include Bounce2
#include <Bounce2.h>

#include <iostream>
using namespace std;


//Load SDL
bool init();

//Load Media
bool loadMedia();

//Clear
void close();

//Sounds
Mix_Chunk *milshot = NULL;



bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if (SDL_Init(SDL_INIT_AUDIO) < 0)
	{
		printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
		success = false;
	}
	
	//Initialize SDL_mixer
	if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
	{
		printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
		success = false;
	}
	return success;
}

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load sound effects
	
	milshot = Mix_LoadWAV("../data/sounds/01_milshot.wav");
	if (milshot == NULL)
	{
		printf("Failed to load high sound effect! SDL_mixer Error: %s\n", Mix_GetError());
		success = false;
	}
	

	return success;
}

void close()
{

	//Free the sound effects
	Mix_FreeChunk(milshot);
	milshot = NULL;

	//Quit SDL subsystems
	Mix_Quit();
	SDL_Quit();
}

//Code snippets
/*
 *	
 *	Mix_PlayChannel(-1, milshot, 0); <== play soundfile in mixer
 *	digitalRead(pin); <== read pin
 *	cout << "milshot" << endl;	//<== print "milshot" to console
 */


Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce(); 

int main(int argc, char* args[])
{
	
	//Start up SDL
	if (!init())
	{
		printf("Failed to initialize!\n");
	}
	else
	{
		//Load media
		if (!loadMedia())
		{
			printf("Failed to load media!\n");
		}
		else
		{	
			//Main loop flag
			bool quit = false;

			//Event handler
			SDL_Event e;
			
			//Setup wiringPi
			if (wiringPiSetup() == -1)
				return 1;
			
			
			
			pinMode(5, INPUT);
			debouncer1.attach(5);
			debouncer1.interval(5); // interval in ms
			
			pinMode(4, INPUT);
			debouncer2.attach(4);
			debouncer2.interval(5); // interval in ms
			
			
			
			//While application is running <<======================================================================================
			while (!quit)
			{
				debouncer1.update();
				debouncer2.update();
				
				// Get the updated value :
				int value1 = debouncer1.read();
				int value2 = debouncer2.read();

				  // Turn on the LED if either button is pressed :
				if (value1 == LOW || value2 == LOW) {
					cout << "ON" << endl;	//<== print "milshot" to console
				} 
				else {
					cout << "OFF" << endl;	//<== print "milshot" to console
				}
				
			}
			//<====================================================================================================================
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

Thanks in advance,
gamecompiler

rose() and fell() do not work if called less frequent than update()

Common problem:

  • User must press a button to trigger a function in the program
  • User must release the button and press it again to trigger the function another time

One could use the rose() or fell() - function for this, but they only work, if they are called in the same loop directly after update().
I my program update() is called often in loop(), but the rose() or fell() - functions are called much less frequent in a timer routine and do not work because the STATE_CHANGED-flag is reset at every call of update().
I moved the "state &= ~_BV(STATE_CHANGED);"-line from update() to rose() and fell(). Now the STATE_CHANGED-flag remains set until rose() or fell() is called the first time.
This is in my opinion the behaviour, that most people would expect and therefore would like to make a pull-request.
A problem could be compatibility because after the modification rose() and fell() would return a different value if called twice without an update() between the calls.
One could define new functions for the modified rose() and fell(), but I think this is not necessary and makes the class difficult to use. It would be hard to explain the difference between the old an new functions.

pinMode and digitalRead

Hello, after the last upgrade from the library I've been facing the following errors on PlatformIO's compilation:

Bounce2.h:90:61: error: 'digitalRead' was not declared in this scope
Bounce2.h:95:26: error: 'pinMode' was not declared in this scope

both errors can be solved putting the following line on the first line of Bounce2.h file:

#include <Arduino.h>

Feature request: .changed()

No idea why I didn't implement this is the past when I made changes to Bounce2 but why only let .update() return if it changed. There are situations you don't care if it's a .fell() or a .rose() but now you have to change both (which is even extra work because it's made out the current state and the fact if it changed) to simply see if the pin changed.

Typo in library.properties

Search for Deboucing and replace by Debouncing.

Appears in the library update dialog, so everyone installing or updating this lib will see it and it's is trivial to fix.

Repeating issue.

Hello,
I am having an issue with the following code:

Converting an old keyboard from a TI-99 to USB.

#include <Bounce2.h>
const int bounceDelay = 90;

Bounce wire1  = Bounce();

void setup(){
  pinMode(0, INPUT);  // 1
  wire1.attach(0);
  wire1.interval(bounceDelay);
}

void loop(){
  wire1.update();
  if(wire1.read()){
    Serial.println("Y");
  }
}

When I launch the serial monitor and press the button (pressing it just once) I get about 50 or so 'Y's... I am expecting to press a button and get just one 'Y'.

What am I doing wrong, and how would I correct this? Any help would be great, thanks!!

Does #define BOUNCE_LOCK_OUT work?

I was trying it in Wokwi as

https://wokwi.com/projects/324855073217708626

and didn't see that it had an effect.

The documentation at https://github.com/thomasfredericks/Bounce2#lock-out-interval says

By defining "#define BOUNCE_LOCK_OUT" in "Bounce.h" (or in your code before including "Bounce.h") you can activate an alternative debouncing method. This method is a lot more responsive, but does not cancel noise.

but it does not seem to have an effect.

Output from that script's simulation:

2297ms leading Edge Debounce: 1
2297ms Wasser Debounce: 1
2349ms Bounce2 Debounce: 1
2350ms Arduino Debounce.ino: 1

Some discussion at https://forum.arduino.cc/t/leading-edge-debouncing/964407/15

overflow of millis() after ~50 days

Hey, thanks for your library. I'm using it on an arduino pro mini (ATmega328@16MHz) to debounce a standard wall push button.
My sketch shall run permanently for month or years, because it controlls a lamp with several channels and light modes, switched by the single wall push button.

Because my sketch has to run longer than ~50 days (lamp control), millis(), will overflow. Do you know, how your library will behave when it reaches that limit or during the debounce time the overflow occurs?

I prevented my sketch for trying this by pausing it if it's too close (debounce time + some milliseconds) to the overflow, but thats not very elegant.

Request: add Button class state output method isPressed()

The Button class doesn't have a logically correct .read() method that takes the PressedState into account. The pressed() and released() methods are one-time changed state outputs. Sometimes I want to detect if a Button is being held down.

Something like isPressed() or isReleased() would make logical sense.

Thanks for your work on this library. So simple yet so useful.

error: invalid conversion from 'int' to 'WiringPinMode' [-fpermissive]

Hello,
I try to use the bounce2 under STM32 arduino code and get the following error:
.platformio\lib\Bounce2_ID1106\src/Bounce2.h:188:26: error: invalid conversion from 'int' to 'WiringPinMode' [-fpermissive]
I use VisualStudioCode with the PlatformIO extension.

Best regards
Elmar

simple button debounced but multiple trigers

hi , i have a project that a simple button trigger a loop ( for() ) and after the loop is completed once, the debouncer do it again like i pushed the button again , do you have a command to clear it please ?

Which license is this released under?

Which license is this library released under? The LICENSE file is the MIT license, but the comment block at the top of Bounc2.h is the GPL. Both can't be right so which is correct?

Can't compile for Arduino Zero

Hi, your library works very well with arduino due and uno. Unfortunatelly I need to use it on a project involving an arduino zero. It seems that there are some problems and can't compile when a board with SAMD is selected. Is there any chance to fix that?
Thank's!

Multiple intervals

Hi,
Enhancement suggestion:
When you select the interval - possibly have another function that requires 2x variables. The off bounce and the on bounce.
This will allow you to say that it has to be on for 100ms before changeover and off for 1000ms before changeover (for example).
What do you think?

Debouncer

Hi.Again

if ( debouncer.rose() does means digitalRead (LOW).?

I need change my code with your perfect bounce library.

Thank you

void 1(){
buttonValue = digitalRead(Button);
if (buttonValue == LOW && lastButtonValue == HIGH) {
holdCount = millis();
} // end of JUST CLICKED
// JUST RELEASED
if (buttonValue == HIGH && lastButtonValue == LOW) {
currentHoldCount = millis();
mode_5++;
if (mode_5 > 2) mode_5 = 1;

if (currentHoldCount - holdCount < 1000) {
if (mode_5 == 1) {
midiOUT(0xB0, 0x63, 0x7D);
midiOUT(0xB0, 0x62, 0x5E);
midiOUT(0xB0, 0x06, 0x00);
midiOUT(0xB0, 0x26, 0x01);
lcd.setCursor ( 0, 1 );
lcd.print(" ");
lcd.setCursor ( 0, 2);
lcd.backlight();
lcd.print(" ");
lcd.setCursor ( 0, 3 );
lcd.print("LOOPER : STOP ");

}
else if (mode_5 == 2)
{
  lcd.setCursor ( 0, 1 );
  lcd.print("                    ");
  lcd.setCursor ( 0, 2);
  lcd.backlight();
  lcd.print("                    ");
  lcd.setCursor ( 0, 3 );
  lcd.print("LOOPER :   PLAY >>> ");
  mode_1 = 2;

  midiOUT(0xB0, 0x63, 0x7D); //play
  midiOUT(0xB0, 0x62, 0x5D);
  midiOUT(0xB0, 0x06, 0x00);
  midiOUT(0xB0, 0x26, 0x01);
}

}
// ond of SHORT CLICK
// LONG CLICK
if (currentHoldCount - holdCount >= 1000) {
lcd.setCursor ( 0, 1 );
lcd.print(" ");
lcd.setCursor ( 0, 2);
lcd.backlight();
lcd.print(" ");
lcd.setCursor ( 0, 3 );
lcd.print("LOOPER : CLEAN ");

midiOUT(0xB0, 0x63, 0x7D); //play
midiOUT(0xB0, 0x62, 0x59);
midiOUT(0xB0, 0x06, 0x00);
midiOUT(0xB0, 0x26, 0x01);

}

}
lastButtonValue = buttonValue;
}

Publish version 2.55 as a GitHub release

Can you publish a 2.55 release to GitHub? The version installed by the Arduino IDE is out of date and doesn't match documentation in the GitHub repository.

Thanks!

press and hold

I needed two functions in one button so added a duration() method to your class.
It simply returns the period [ms] the pin has been in current state (totally undisturbed - flatline)

If you think this is something you would like in your library i can make a pull request.
Or you can add it yourself, it's effectively a one-liner.

Request: Allow multiple physical pins or software-only "pin" for each Bounce2 object

In my application I have 2 different physical pins that control one logical function. It would be nice to be able to use the bounce2 methods to read and handle them as a logical input rather than in a 1:1 physical to logical mapping.

Also, it would be nice if there were a public method to programmatically set the object state via a software "pin". In my example I have a serial console so it would be nice if I could "press" a button via serial input command. This would also be really nice for test rigs to exercise the button inputs via software.

Don't know how widespread the use for these might be but I could make use of it...

Problems with currentDuration ()

Hi, I have tried using the ".currentDuration ()" function from the Bonce2 library, but the best I have achieved is using with ".rose" and ".isPressed". However, when you keep the button pressed to go to function 2, function 1 is also activated. In my example, if I want to change only the value of "toogleState", I cannot do it without also changing the value of "bank". And as you can see I also had to add a Delay in function 2 and a double condition "&& pedal_1.currentDuration ()> 500 && pedal_1.currentDuration () <1000" to avoid problems.

#include <Bounce2.h>

Bounce2::Button pedal_1 = Bounce2::Button();
int bank = 0;
int toogleState = 0;

void setup() {
  Serial.begin(9600);
  pedal_1.attach(2, INPUT_PULLUP);
  pedal_1.interval(25);
  pedal_1.setPressedState(LOW);
}

void loop() {
  pedal_1.update();
  /*FUNCTION 1*/
  if (pedal_1.rose() && pedal_1.currentDuration() < 50) {
    bank++;
    Serial.print("Bank:");
    Serial.println(bank);
  }

  /*FUNCTION 2*/
  else if (pedal_1.isPressed() && pedal_1.currentDuration() > 500 && pedal_1.currentDuration() < 1000) {
    delay(500);
    toogleState++;
    Serial.print("ToogleSate:");
    Serial.println(toogleState);
  }
}

Bounce2 and Analog Pins?

Hi
Can you please let me know if Bounce2 also works with any of the Ananlog pins? Specifically I am using A3 as an input.

Regards
Robert

"_BV" doesn't compile. Changed to "bit" and works

I'm compiling for Arduino Due, and it seems like there is no _BV macro anymore. I did a little research and determined that there is a "bit" macro with the same syntax that does the same thing, so I did a search and replace and that worked.

State methods

Why are the following methods all private:

  private:
    inline void changeState();
    inline void setStateFlag(const uint8_t flag)    {state |= flag;}
    inline void unsetStateFlag(const uint8_t flag)  {state &= ~flag;}
    inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
    inline bool getStateFlag(const uint8_t flag)    {return((state & flag) != 0);}

when the attribute that they concern is protected?

 protected:
    uint8_t state;

I'm not sure it makes sense to be more restrictive of the methods than the underlying attribute.

Add separate up/down timeouts

My use case is one where I have a light sensor that goes low when something blocks a (closing) door. When it goes low, it does so fairly immediately with little noise, but high is more noisy (because it's floating and the pullup resistor is a bit weird).

It would be very useful if I could define a different interval for going low vs going high. I care about seeing a low immediately (I don't want the door to hit whatever is in the way) and don't mind false positives, plus low is not as noisy so a smaller interval is more reliable. The high interval could be 300 ms, since I don't really care to be quick about detecting when the obstacle has left.

move Bounce2 out from subdirectory

Please consider moving Bounce2 library to new repository or move to main repository directory as it is not found by arduino IDE when your repository is attached to git repository as submodule

paths are then:

libraries\Bounce-Arduino-Wiring\Bounce2\Bounce2.h

when Arduino searches for

libraries\Bounce-Arduino-Wiring\Bounce2.h

Usage Bounce with wireless button

Hi there,

Wonderful library. I was playing with it and came across a moment when i wanted to debounce a wireless button (a nunchuck button press). Is there any way to do that with this library or is it just meant to be used with pinstates directly connected to a board ?

Thanks in advance !

Doesn't work with framework-arduinoespressif32-release_IDF4.4?

hello,

can it be, the lib doesn't run on the following setting in PlattformIO? It uses the new Arduino 2.0.x Framework.

platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master
platform_packages = framework-arduinoespressif32 @ https://github.com/tasmota/arduino-esp32/releases/download/2.0.1/framework-arduinoespressif32-release_IDF4.4.tar.gz
platformio/tool-esptoolpy @ https://github.com/tasmota/esptool/releases/download/v3.2/esptool-v3.2.zip

best regards

</a></b></dt><dd>Make Bounce2 more abstract. Split it from the hardware layer. ...

<dl class="todo"><dt><b><a class="el" href="todo.html#_todo000001">Todo:</a></b></dt><dd>Make Bounce2 more abstract. Split it from the hardware layer. Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce. </dd></dl>
<p>Basic example of the <a class="el" href="class_bounce.html" title="The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. ">Bounce</a> class.</p>
<div class="fragment"><div class="line"></div><div class="line">/*</div><div class="line"> DESCRIPTION</div><div class="line"> ====================</div><div class="line"> Simple example of the Bounce library that switches a LED when</div><div class="line"> a state change (from HIGH to LOW) is triggered (for example when a button is pressed).</div><div class="line"></div><div class="line"> Set BOUNCE_PIN to the pin attached to the input (a button for example).</div><div class="line"> Set LED_PIN to the pin attached to a LED.</div><div class="line"></div><div class="line">*/</div><div class="line"></div><div class="line">// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()</div><div class="line">#define BOUNCE_PIN 2</div><div class="line"></div><div class="line">// DEFINE THE PIN FOR THE LED :</div><div class="line">// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)</div><div class="line">#define LED_PIN LED_BUILTIN</div><div class="line">// 2) OTHERWISE SET YOUR OWN PIN</div><div class="line">// #define LED_PIN 13</div><div class="line"></div><div class="line">// Include the Bounce2 library found here :</div><div class="line">// https://github.com/thomasfredericks/Bounce2</div><div class="line">#include &lt;Bounce2.h&gt;</div><div class="line"></div><div class="line"></div><div class="line">// INSTANTIATE A Bounce OBJECT</div><div class="line">Bounce bounce = Bounce();</div><div class="line"></div><div class="line">// SET A VARIABLE TO STORE THE LED STATE</div><div class="line">bool ledState = LOW;</div><div class="line"></div><div class="line">void setup() {</div><div class="line"></div><div class="line"> // BOUNCE SETUP</div><div class="line"></div><div class="line"> // SELECT ONE OF THE FOLLOWING :</div><div class="line"> // 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP</div><div class="line"> // bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP</div><div class="line"> // 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP</div><div class="line"> bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP</div><div class="line"></div><div class="line"> // DEBOUNCE INTERVAL IN MILLISECONDS</div><div class="line"> bounce.interval(5); // interval in ms</div><div class="line"></div><div class="line"> // LED SETUP</div><div class="line"> pinMode(LED_PIN, OUTPUT);</div><div class="line"> digitalWrite(LED_PIN, ledState);</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line">void loop() {</div><div class="line"> // Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)</div><div class="line"> bounce.update();</div><div class="line"></div><div class="line"> // &lt;Bounce&gt;.changed() RETURNS true IF THE STATE CHANGED (FROM HIGH TO LOW OR LOW TO HIGH)</div><div class="line"> if ( bounce.changed() ) {</div><div class="line"> // THE STATE OF THE INPUT CHANGED</div><div class="line"> // GET THE STATE</div><div class="line"> int deboucedInput = bounce.read();</div><div class="line"> // IF THE CHANGED VALUE IS LOW</div><div class="line"> if ( deboucedInput == LOW ) {</div><div class="line"> ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState</div><div class="line"> digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">


This issue was generated by todo based on a Todo comment in 3ddefb4. It's been assigned to @thomasfredericks because they committed the code.

Make Bounce2 more abstract. Split it from the hardware layer.

Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce.


Bounce2/src/Bounce2.h

Lines 30 to 35 in 5103299

* @todo Make Bounce2 more abstract. Split it from the hardware layer.
* @body Remove deboucing code from Bounce2 and make a new Debounce class from that code. Bounce2 should extend Debounce.
*/
#ifndef Bounce2_h


This issue was generated by todo based on a @todo comment in 5103299. It's been assigned to @thomasfredericks because they committed the code.

Reverse Read logic for switches wired with pullups

It would be handy if the Bounce library had an overloaded constructor

Bounce button1 = Bounce(buttonPin1, 10, PULLUP );
so that if (button1.read()) would return true when the button was pressed

maybe it would add too much complexity though to reverse all the rising and falling functions.

Support direct cloning into library folder

When I clone this library from ~/Arduino/libraries, I end up with an extra directory:

Bounce-Arduino-Wiring/Bounce2

It would be better if the repository name would be changed to Bounce2 and no subdirectory within the repository for easier cloning.

Functions do work with the array type of setup

The example under more called bounceMore looks great as I have lots of buttons.

So I have this:
` //Setup buttons
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].attach( BUTTON_PINS[i] , INPUT_PULLUP ); //setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms

}`

But, I find that I can't use the function .isPressed with it. Like this:
buttons[I].isPressed
so I have to use the individual method for assigning each of my 13 buttons.

Best method for an array of buttons?

Hey there!

I've recently been using Bounce2 in one of my Arduino projects, and it works well! I have a query though — what's the best method for it to work on an array of buttons? I'm trying to work with a few buttons and store them as an array to work in a for loop.

I have seen the two button example, but what is the best method to work with an array of buttons, instead of hard coding every button and debouncer variable?

Many thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.