Skip to content

Commit 50c5779

Browse files
committed
Create SwitchRunTimeBahavior_Demo_01.ino
Created example to show how to switch button behavior at run time.
1 parent f0cbf59 commit 50c5779

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Demonstrates how to switch behavior of a button at run time. This example uses
3+
two alternating time frames. During the first, the button will turn the LED on
4+
and off. During the second, the LED will remain on and the button will have no
5+
effect. The two modes are toggled back and forth every few seconds.
6+
7+
The button should be wired such that when pressed, the "buttonPin" is
8+
connected to ground.
9+
10+
The LED should be wired with the "ledPin" to the positive lead and the
11+
negative lead should be connected to ground. A current limiting resistor
12+
should be used.
13+
*/
14+
15+
#include "MomentaryButton.h"
16+
#include "AlwaysOnButton.h"
17+
18+
// Change these if your button or LED are on other pins.
19+
int buttonPin = 8;
20+
int ledPin = 9;
21+
22+
23+
// We will access the button through a pointer. That way we can change the button
24+
// behavior by changing what type of button is being pointed to.
25+
SimpleButton *button;
26+
27+
// The MomentaryButton will automatically configure the button pin.
28+
MomentaryButton momentaryButton(buttonPin);
29+
30+
// Always on button. The button pin is require to remain compatible with the base class,
31+
// however, it is not used by this class.
32+
AlwaysOnButton onButton(buttonPin);
33+
34+
// Used for timing and switching between the two states.
35+
unsigned long intervalStartTime;
36+
const int intervalLength = 5000;
37+
int currentState;
38+
39+
40+
void setup()
41+
{
42+
// Setup the output LED.
43+
pinMode(ledPin, OUTPUT);
44+
digitalWrite(ledPin, LOW);
45+
46+
// Set our initial state as using the momentary button.
47+
button = &momentaryButton;
48+
currentState = 0;
49+
50+
// Capture the start time to be used to calculate the time ellapsed.
51+
intervalStartTime = millis();
52+
53+
// Allow printing to the serial monitor. Provides additional information and debugging.
54+
Serial.begin(9600);
55+
Serial.println("Serial monitor initialized.");
56+
}
57+
58+
59+
void loop()
60+
{
61+
if (millis() - intervalStartTime > intervalLength)
62+
{
63+
64+
65+
// Toggle between the two states. In state 0, the button can turn the LED
66+
// on or off. In state 1, the LED is always on.
67+
if (currentState == 0)
68+
{
69+
// Were in state 0, so switch to state 1.
70+
button = &onButton;
71+
currentState = 1;
72+
Serial.println("Current state: ALWAYS ON");
73+
}
74+
else
75+
{
76+
// Were in state 1, so switch to state 0.
77+
button = &momentaryButton;
78+
currentState = 0;
79+
Serial.println("Current state: BUTTON");
80+
}
81+
82+
// Reset the start time.
83+
intervalStartTime = millis();
84+
}
85+
86+
// If the button is pressed, turn the LED light on. In state 0 (responding to the button),
87+
// the actual state of the button is returned. In state 1, on (ISPRESSED), is always returned.
88+
if (button->getStatus() == ISPRESSED)
89+
{
90+
// Turns on LED.
91+
digitalWrite(ledPin, HIGH);
92+
93+
}
94+
else
95+
{
96+
// Turns off LED.
97+
digitalWrite(ledPin, LOW);
98+
}
99+
}

0 commit comments

Comments
 (0)