Skip to content

Commit 87f2d62

Browse files
committed
Added custom button demo.
1 parent ced7f6b commit 87f2d62

3 files changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Turns an LED on when the button is pressed and off when the button is released.
3+
4+
The button should be wired such that when pressed, the "buttonPin" is
5+
connected to ground.
6+
7+
The LED should be wired with the "ledPin" to the positive lead and the
8+
negative lead should be connected to ground. A current limiting resistor
9+
should be used.
10+
*/
11+
12+
#include "CustomButton.h"
13+
14+
// Change these if your button or LED are on other pins.
15+
int buttonPin = 8;
16+
int ledPin = 9;
17+
18+
// The button will automatically configure the button pin.
19+
CustomButton button(buttonPin);
20+
21+
// Used for timing.
22+
unsigned long intervalStartTime;
23+
const int fastBlinkInterval = 100;
24+
bool timing;
25+
26+
void setup()
27+
{
28+
// Setup the output LED.
29+
pinMode(ledPin, OUTPUT);
30+
digitalWrite(ledPin, LOW);
31+
}
32+
33+
void loop()
34+
{
35+
// Hold the button down to turn the LED on and release it to turn it off.
36+
switch (button.getBehavior())
37+
{
38+
case CustomButton::LIGHTOFF:
39+
{
40+
// Turn off the light and make sure timing is turned off.
41+
digitalWrite(ledPin, LOW);
42+
timing = false;
43+
break;
44+
}
45+
46+
case CustomButton::BLINKONCE:
47+
{
48+
// Turn on the light for 1 second. We won't bother with timing for this.
49+
digitalWrite(ledPin, HIGH);
50+
delay(1000);
51+
break;
52+
}
53+
54+
case CustomButton::FASTBLINK:
55+
{
56+
// If we are not timing, then we start it. That is, we have determine the button is being
57+
// held down and this is the first pass through the loop since the long press was activated.
58+
//
59+
// If we are already timing, we need to alternate the status of the light everytime the
60+
// timing interval is up.
61+
if (!timing)
62+
{
63+
// Turn on the light and start the timing.
64+
digitalWrite(ledPin, HIGH);
65+
intervalStartTime = millis();
66+
timing = true;
67+
}
68+
else
69+
{
70+
// Check if the timing interval is up.
71+
if (millis() - intervalStartTime > fastBlinkInterval)
72+
{
73+
// Swap the state of the LED light. If it's off, turn it on and vice versa.
74+
// This is done by reading the current status of the pin, negating it and
75+
// passing it to the write function.
76+
digitalWrite(ledPin, !digitalRead(ledPin));
77+
78+
// Restart the timing.
79+
intervalStartTime = millis();
80+
}
81+
}
82+
break;
83+
}
84+
}
85+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 Lance A. Endres
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
#include "CustomButton.h"
25+
26+
// Constructors.
27+
CustomButton::CustomButton(int pin) :
28+
ButtonBase(pin)
29+
{
30+
}
31+
32+
CustomButton::CustomButton(int pin, int debounceInterval) :
33+
ButtonBase(pin, debounceInterval)
34+
{
35+
}
36+
37+
// Destructor.
38+
CustomButton::~CustomButton()
39+
{
40+
}
41+
42+
CustomButton::LIGHTBEHAVIOR CustomButton::getBehavior()
43+
{
44+
switch (update())
45+
{
46+
// We want to blink the light for 1 second when the button is pressed and released.
47+
// In the ButtonSuite, this event returns the WASSHORTPRESSED.
48+
case WASSHORTPRESSED:
49+
{
50+
return BLINKONCE;
51+
}
52+
53+
// We want to blink the light fastly as long as the button has been held down long
54+
// enough to be considered a long press and still is being held down.
55+
case ISLONGPRESSED:
56+
{
57+
return FASTBLINK;
58+
}
59+
60+
// If neither of the above two things are true, we turn the light off.
61+
default:
62+
{
63+
return LIGHTOFF;
64+
}
65+
}
66+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 Lance A. Endres
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
/*
25+
This class is used to demonstrate how to derive your own custom behavior for
26+
a button. While there are many examples of button classes in the library, they
27+
are largely inherited from intermediate classes. However, many custom buttons
28+
will likely find it better to inherit direction from the ButtonBase class.
29+
*/
30+
31+
/*
32+
To use a button with this library, the button should be wired with one side
33+
connected to the Arduino pin and the other side connected to ground such that
34+
when the button is pressed, the pin is brought to ground (LOW).
35+
*/
36+
37+
#ifndef CUSTOMBUTTON_H
38+
#define CUSTOMBUTTON_H
39+
40+
#include "Arduino.h"
41+
#include "ButtonBase.h"
42+
43+
class CustomButton : public ButtonBase
44+
{
45+
public:
46+
enum LIGHTBEHAVIOR
47+
{
48+
LIGHTOFF,
49+
BLINKONCE,
50+
FASTBLINK
51+
};
52+
53+
public:
54+
CustomButton(int pin);
55+
CustomButton(int pin, int debounceInterval);
56+
57+
~CustomButton();
58+
59+
// Status access functions. Call in the "loop" to get the status of the button.
60+
public:
61+
// Returns a value to that states what the light should do.
62+
LIGHTBEHAVIOR getBehavior();
63+
};
64+
65+
#endif

0 commit comments

Comments
 (0)