11/*
2- Displays the current value of the CycleButton to the serial monitor. When
3- the value is greater than a set value, an LED is turned on.
4-
5- The value is incremented each time the button is pushed. If the value is
6- higher than the maximum value, it resets to the minimum value. Holding
7- the button down longer than the long press time will also reset the counter.
8-
9- The button should be wired such that when pressed, the "buttonPin" is
10- connected to ground.
2+ Displays the current value of the CycleButton to the serial monitor. When
3+ the value is greater than a set value, an LED is turned on.
4+
5+ The value is incremented each time the button is pushed. If the value is
6+ higher than the maximum value, it resets to the minimum value. Holding
7+ the button down longer than the long press time will also reset the counter.
8+
9+ The button should be wired such that when pressed, the "buttonPin" is
10+ connected to ground.
1111
12- The LED should be wired with the "ledPin" to the positive lead and the
13- negative lead should be connected to ground. A current limiting resistor
14- should be used.
12+ The LED should be wired with the "ledPin" to the positive lead and the
13+ negative lead should be connected to ground. A current limiting resistor
14+ should be used.
1515*/
1616
1717#include " CycleButton.h"
2323int buttonPin = 8 ;
2424int ledPin = 9 ;
2525
26- // The CycleButton will automatically configure the button pin.
26+ // The CycleButton will automatically configure the button pin. The second number is
27+ // the maximum value of the counter.
2728CycleButton button (buttonPin, 6 );
2829
2930void setup ()
3031{
31- Serial.begin (9600 );
32- Serial.println (" *** Cycle Button Demo 01 ***" );
32+ Serial.begin (9600 );
33+ Serial.println (" *** Cycle Button Demo 01 ***" );
3334
34- // Setup the output LED.
35- pinMode (ledPin, OUTPUT);
36- digitalWrite (ledPin, LOW);
35+ // Setup the output LED.
36+ pinMode (ledPin, OUTPUT);
37+ digitalWrite (ledPin, LOW);
3738}
3839
3940void loop ()
4041{
41- // Press the button once to turn it on, and again to turn it off.
42- int value = button.getValue ();
43-
44- Serial.print (" Cycle button value: " );
45- Serial.println (value);
42+ // During each loop, the number of times the button is pressed is retrieved
43+ // from the button.
44+ int value = button.getValue ();
45+
46+ Serial.print (" Cycle button value: " );
47+ Serial.println (value);
4648
47- if (value > 3 )
48- {
49- digitalWrite (ledPin, HIGH);
50- }
51- else
52- {
53- digitalWrite (ledPin, LOW);
54- }
55- }
49+ // If the count is above a specified value, turn the LED on, otherwise turn
50+ // if off. Once the button's max value is reached, it will reset to the first
51+ // value and the LED is turned off. The button can also be manually reset.
52+ if (value > 2 )
53+ {
54+ digitalWrite (ledPin, HIGH);
55+ }
56+ else
57+ {
58+ digitalWrite (ledPin, LOW);
59+ }
60+ }
0 commit comments