Skip to content

Commit

Permalink
Set EventSwitch state in constructor, update docs and example. Closes…
Browse files Browse the repository at this point in the history
… issue #1
  • Loading branch information
Stutchbury committed Dec 14, 2024
1 parent 1e91f98 commit 452e066
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/EventSwitch.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# EventSwitch Class

The `EventSwitch` class is for standard on/off inputs.
The `EventSwitch` class is for standard on/off inputs. Like the `EventButton` the switch must be wired between the pin and GND. When the switch is closed (LOW) its state will be 'ON' and when open (HIGH) it will be 'OFF'.

A quick explainer: The switch pin is configured with the microcontroller's internal [`INPUT_PULLUP` resistor](https://en.wikipedia.org/wiki/Pull-up_resistor). In this configuration, without anything attached, the pin will read as HIGH because it has been 'pulled up' to the voltage of the board (either 3.3V or 5V). When you attach a switch (or button) that is wired from the pin to GND, if the switch is closed (or the button pressed), the weak pullup resistor is overcome by the closed switch 'pulling' the pin to GND (0V) or LOW.

![button](../images/switch.png)

Expand Down Expand Up @@ -71,7 +72,7 @@ See [common methods](Common.md#void-setcallbackcallbackfunction-func) for detail


#### `void reverseOnOff(bool rev=true)`
If your wiring is reversed, you can switch it with this method. The `ON` event is fired in place of the `OFF` event and vice versa.
If your wiring is reversed, you can switch it with this method. The `ON` event is fired in place of the `OFF` event and vice versa. Default is `true` so pass `false` to return to 'normal' behaviour.

#### `bool isOnOffReversed()`
Returns true if on and off are reversed.
Expand Down
8 changes: 7 additions & 1 deletion examples/Switch/Switch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#include <EventSwitch.h>

const uint8_t switchPin = 18;; //Change to suit your wiring
const uint8_t switchPin = 2; //Change to suit your wiring

/**
* Utility function to print the switch events to Serial.
Expand Down Expand Up @@ -53,6 +53,12 @@ void setup() {
delay(500);
Serial.println("EventSwitch Basic Example");

Serial.print("digitalRead is: ");
Serial.print(HIGH == digitalRead(switchPin) ? "HIGH" : "LOW");
Serial.print(" at startup.");
Serial.print(" so this means the switch is: ");
Serial.println(mySwitch.isOn() ? "ON" : "OFF");

//You can reverse the on/off events rather than change your wiring.
//mySwitch.reverseOnOff();

Expand Down
1 change: 1 addition & 0 deletions src/EventSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ EventSwitch::EventSwitch(byte buttonPin)
// the initial state
delayMicroseconds(2000); //Delay
bounce->attach(buttonPin, INPUT_PULLUP); //then attach button
currentState = bounce->read(); //Initialise switch state from Bounce2
}


Expand Down

0 comments on commit 452e066

Please sign in to comment.