diff --git a/docs/EventSwitch.md b/docs/EventSwitch.md index b83d496..b73e8bc 100644 --- a/docs/EventSwitch.md +++ b/docs/EventSwitch.md @@ -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) @@ -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. diff --git a/examples/Switch/Switch.ino b/examples/Switch/Switch.ino index 5ef40e2..d3abfef 100644 --- a/examples/Switch/Switch.ino +++ b/examples/Switch/Switch.ino @@ -3,7 +3,7 @@ */ #include -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. @@ -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(); diff --git a/src/EventSwitch.cpp b/src/EventSwitch.cpp index 10041ef..74270ee 100644 --- a/src/EventSwitch.cpp +++ b/src/EventSwitch.cpp @@ -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 }