Created
December 19, 2016 04:24
-
-
Save harrisonhjones/1a3a5d679d3c0a41d6f11a18a5b191fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Harrison Jones ([email protected]) | |
// Date: Dec 18, 2016 | |
// Special System Declarations | |
SYSTEM_THREAD(ENABLED); // Use system threading so the code works even while it's connecting to the internet | |
// Constants | |
const uint8_t switchPin = D2; // Switch should be connected between this pin and GND | |
const uint8_t onboardLED = D7; // Onboard LED is connected to D7 | |
const char *pubEventName = "EventName"; // The event name that is published | |
const char *pubEventSwitchOpen = "OPEN"; // The event value that is published when the switch is opened | |
const char *pubEventSwitchClosed = "CLOSED"; // The event value that is published when the switch is closed | |
const char *varNameSwitch = "switch"; // The name of the Cloud variable for the switch state | |
const char *varNameVersion = "version"; // The name of the Cloud variable for the firmware version | |
const char *varValueVersion = "1.1.0"; // The current firmware version (must be manually incremented) | |
const uint8_t switchOpenState = HIGH; // The pin state when the switch is open | |
const long debounceDelay = 100; // The debounce time, in milliseconds, for the switch. Make this smaller for shorter debouce. Should probably keep it over ~50 ms | |
// Variables | |
long lastDebounceTime = 0; // The last recorded debouce time. Needed to track debouncing | |
uint8_t lastSwitchState; // The switch state during the last loop. Needed to track debouncing | |
uint8_t stableSwitchState; // The stable switch state, debounced. | |
// Setup & Loop Functions | |
void setup() { | |
pinMode(switchPin, INPUT_PULLUP); // Initialize the switch pin as an input with an internal pull-up resistor | |
pinMode(onboardLED, OUTPUT); // Initialize D7 pin as output so we can light-up the LED | |
lastSwitchState = digitalRead(switchPin); // Grab the initial state of the switch | |
stableSwitchState = lastSwitchState; // Assume the start up state of the switch is stable | |
Particle.variable(varNameSwitch, switchOpenState); // Declare a Cloud variable for the switch state | |
Particle.variable(varNameVersion, varValueVersion); // Declare a CLoud variable for the firmware version | |
} | |
void loop() { | |
switchCheck(); // Handle updating the current switches state. | |
// Do things with stableSwitchState here. It's stable and ready to use. | |
} | |
// Helper Functions. | |
void switchCheck() { | |
// Use the onboard LED to indicate when the switchPin is stable. | |
// Comment out both `digitalWrite(...) commands below to remove this feature so you can use onboard LED elsewhere | |
uint8_t currentSwitchState = digitalRead(switchPin); | |
if(currentSwitchState != lastSwitchState) { | |
lastDebounceTime = millis(); | |
} | |
if(currentSwitchState != stableSwitchState) { | |
digitalWrite(onboardLED, LOW); | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
stableSwitchState = currentSwitchState; | |
if(stableSwitchState == switchOpenState) { | |
Particle.publish(pubEventName, pubEventSwitchOpen); | |
} else { | |
Particle.publish(pubEventName, pubEventSwitchClosed); | |
} | |
} | |
} else { | |
digitalWrite(onboardLED, HIGH); | |
} | |
lastSwitchState = currentSwitchState; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment