From 450f95c997a786419558793a675c745b4265a7bd Mon Sep 17 00:00:00 2001 From: JChristensen Date: Wed, 26 Apr 2017 11:13:18 -0400 Subject: [PATCH] Use pinMode() to enable pullups for ESP8266. Fixes #4. Change README file to Markdown. Change example sketches to .ino files. --- Button.cpp | 5 +- README.md | 123 ++++++++++++++++++ ReadMe.txt | 108 --------------- .../LongPress/LongPress.ino | 0 .../SimpleOnOff/SimpleOnOff.ino | 0 .../UpDown.pde => examples/UpDown/UpDown.ino | 0 6 files changed, 126 insertions(+), 110 deletions(-) create mode 100644 README.md delete mode 100644 ReadMe.txt rename Examples/LongPress/LongPress.pde => examples/LongPress/LongPress.ino (100%) rename Examples/SimpleOnOff/SimpleOnOff.pde => examples/SimpleOnOff/SimpleOnOff.ino (100%) rename Examples/UpDown/UpDown.pde => examples/UpDown/UpDown.ino (100%) diff --git a/Button.cpp b/Button.cpp index 3b78045..81457e5 100644 --- a/Button.cpp +++ b/Button.cpp @@ -35,9 +35,10 @@ Button::Button(uint8_t pin, uint8_t puEnable, uint8_t invert, uint32_t dbTime) _puEnable = puEnable; _invert = invert; _dbTime = dbTime; - pinMode(_pin, INPUT); if (_puEnable != 0) - digitalWrite(_pin, HIGH); //enable pullup resistor + pinMode(_pin, INPUT_PULLUP); //enable pullup resistor + else + pinMode(_pin, INPUT); _state = digitalRead(_pin); if (_invert != 0) _state = !_state; _time = millis(); diff --git a/README.md b/README.md new file mode 100644 index 0000000..d5e97cb --- /dev/null +++ b/README.md @@ -0,0 +1,123 @@ +# Arduino Button Library v1.0 +https://github.com/JChristensen/Button +ReadMe file +Jack Christensen Mar 2012 + +![CC BY-SA](http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-sa.png) + +## Introduction +The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible. + +## Installation +To use the **Button** library: +- Go to https://github.com/JChristensen/Button, click the **Download ZIP** button and save the ZIP file to a convenient location on your PC. +- Uncompress the downloaded file. This will result in a folder containing all the files for the library, that has a name that includes the branch name, usually **Button-master**. +- Rename the folder to just **Button**. +- Copy the renamed folder to the Arduino sketchbook\libraries folder. + +## Examples +The following example sketches are included with the **Button** library: + +- **SimpleOnOff**: Just turns the Arduino's pin 13 LED on and off. +- **LongPress**: Demonstrates detecting long and short button presses. +- **UpDown**: Counts up or down, one number at a time or rapidly by holding the button down. + +## Button library methods + +### Button(pin, puEnable, invert, dbTime) +##### Description +The constructor defines a button object. +##### Syntax +`Button(pin, puEnable, invert, dbTime);` +##### Parameters +**pin:** Arduino pin number that the button is connected to *(byte)* +**puEnable:** *true* to enable the microcontroller's internal pull-up resistor, else *false* *(boolean)* +invert: *false* interprets a high logic level to mean the button is pressed, *true* interprets a low level as pressed. *true* should be used when a pull-up resistor is employed, *false* for a pull-down resistor. *(boolean)* +**dbTime:** Debounce time in milliseconds *(unsigned long)* +##### Returns +None. +##### Example +```c++ +Button myButton = Button(2, true, true, 25); //25 ms debounce +``` + +### read(void) +##### Description +Reads the button and returns a *boolean* value (*true* or *false*) to indicate whether the button is pressed. The read() function needs to execute very frequently in order for the sketch to be responsive. A good place for read() is at the top of loop(). Often, the return value from read() will not be needed if the other functions below are used. +##### Syntax +`myButton.read();` +##### Parameters +None. +##### Returns +*true* if the button is pressed, *else* false *(boolean)* +##### Example +```c++ +myButton.read(); +``` + +### isPressed(void) +### isReleased(void) +##### Description +These methods check the button state at the point in time when it was last read, and return false or true accordingly. These functions **do not** cause the button to be read. +##### Syntax +`myButton.isPressed();` +`myButton.isReleased();` +##### Parameters +None. +##### Returns +*true* or *false*, depending on whether the button has been pressed (released) or not *(boolean)* +##### Example +```c++ +if ( myButton.isPressed() ) { + //do some stuff +} +else { + //do some different stuff +} +``` + +### wasPressed(void) +### wasReleased(void) +##### Description +These methods check the button state to see if it changed between the last two reads and return false or true accordingly. These functions **do not** cause the button to be read. Note that these functions may be more useful than `isPressed()` and `isReleased()` since they actually detect a **change** in the state of the button, which is usually what we want in order to cause some action. +##### Syntax +`myButton.wasPressed();` +`myButton.wasReleased();` +##### Parameters +None. +##### Returns +*true* or *false*, depending on whether the button was pressed (released) or not *(boolean)* +##### Example +```c++ +if ( myButton.wasPressed() ) { ... +``` + +### pressedFor(ms) +### releasedFor(ms) +##### Description +These methods check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions **do not** cause the button to be read. +##### Syntax +`myButton.pressedFor(ms);` +`myButton.releasedFor(ms);` +##### Parameters +**ms:** The number of milliseconds *(unsigned long)* +##### Returns +*true* or *false*, depending on whether the button was pressed (released) for the specified time *(boolean)* +##### Example +```c++ +if ( myButton.pressedFor(1000) ) { //has the button been pressed for one second? +``` + +### lastChange(void) +##### Description +Under certain circumstances, it may be useful to know when a button last changed state. lastChange() returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function). +##### Syntax +`myButton.lastChange();` +##### Parameters +None. +##### Returns +The time in milliseconds when the button last changed state *(unsigned long)* +##### Example +```c++ +unsigned long lastChange = myButton.lastChange(); +``` diff --git a/ReadMe.txt b/ReadMe.txt deleted file mode 100644 index 111b3f0..0000000 --- a/ReadMe.txt +++ /dev/null @@ -1,108 +0,0 @@ -ReadMe file for Arduino Button Library v1.0 -https://github.com/JChristensen/Button -Jack Christensen Mar 2012 - -This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 -Unported License. To view a copy of this license, visit -http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative -Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. - --------------------------------------------------------------------------------- -Arduino library for debouncing and reading momentary contact switches like -tactile button switches. "Long presses" of arbitrary length can be detected. -Works well in state machine constructs. Use the read() function to read each -button in the main loop, which should execute as fast as possible. - --------------------------------------------------------------------------------- -To use the library: -(1) Go to https://github.com/JChristensen/Button and click the ZIP button to -download the repository as a ZIP file to a convenient location on your PC. -(2) Uncompress the downloaded file. This will result in a folder containing all -the files for the library, that has a name that includes the branch name, -for example "Button-master". -(3) Rename the folder to just "Button". -(4) Copy the renamed folder to the Arduino sketchbook\libraries folder. - --------------------------------------------------------------------------------- -The following example sketches are included with the Button library: - -SimpleOnOff: Just turns the Arduino's pin 13 LED on and off. - -LongPress: Demonstrates detecting long and short button presses. - -UpDown: Counts up or down, one number at a time or rapidly by holding the button -down. - --------------------------------------------------------------------------------- -Declare button objects as follows. - -Button(pin, puEnable, invert, dbTime) instantiates a button object. - -Where: -pin -- Is the Arduino pin the button is connected to, - -puEnable -- Enables the AVR internal pullup resistor if != 0 (can also use true -or false), - -invert -- If invert == 0, a high state is interpreted as pressed, low as -released. If invert != 0, a high state is interpreted as released, low as -pressed (can also use true or false), and - -dbTime Is the debounce time in milliseconds. - -Example. Wire a normally-open tactile button switch between Arduino pin 2 and -ground. We will use the internal pullup resistor, so the pin will be high when -the button is not pressed, and low when the button is pressed. Therefore we -should use invert == true to invert the logic: - -Button myButton = Button(2, true, true, 25); - --------------------------------------------------------------------------------- -The read() method reads the button and returns a boolean value (true or false) -to indicate whether the button is pressed. The read() function needs to execute -very frequently in order for the sketch to be responsive. A good place for -read() is at the top of loop(). I don't normally use the return value from -read(), because I use the other functions. - -Example: myButton.read(); - --------------------------------------------------------------------------------- -The isPressed() and isReleased() functions check the button state when it was -last read, and return false or true accordingly. These functions DO NOT cause -the button to be read. - -Example: if ( myButton.isPressed ) { - //do some stuff - } - else { - //do some different stuff - } - --------------------------------------------------------------------------------- -The wasPressed() and wasReleased() functions check the button state to see if it -changed between the last two reads and return false or true accordingly. These -functions DO NOT cause the button to be read. Note that these functions may be -more useful than isPressed() and isReleased() since they actually detect a -CHANGE in the state of the button, which is usually what we want in order to -cause some action. - -Example: if ( myButton.wasPressed() ) { ... - --------------------------------------------------------------------------------- -The pressedFor(ms) and releasedFor(ms) functions check to see if the button is -pressed (or released), and has been in that state for the specified time in -milliseconds. Returns false or true accordingly. These functions are useful to -detect "long presses". Note that these functions DO NOT cause the button to be -read. - -Example: if ( myButton.pressedFor(1000) ) { //has button been pressed - //for one second? - --------------------------------------------------------------------------------- -Under certain circumstances, it may be useful to know when a button last changed -state. lastChange() returns the time the button last changed state, in -mlliseconds (the value is from the Arduino millis() function). - -Example: unsigned long lastChange = myButton.lastChange(); - --------------------------------------------------------------------------------- diff --git a/Examples/LongPress/LongPress.pde b/examples/LongPress/LongPress.ino similarity index 100% rename from Examples/LongPress/LongPress.pde rename to examples/LongPress/LongPress.ino diff --git a/Examples/SimpleOnOff/SimpleOnOff.pde b/examples/SimpleOnOff/SimpleOnOff.ino similarity index 100% rename from Examples/SimpleOnOff/SimpleOnOff.pde rename to examples/SimpleOnOff/SimpleOnOff.ino diff --git a/Examples/UpDown/UpDown.pde b/examples/UpDown/UpDown.ino similarity index 100% rename from Examples/UpDown/UpDown.pde rename to examples/UpDown/UpDown.ino