Skip to content

Instantly share code, notes, and snippets.

@chap
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chap/4dceb021a96842bcaa24 to your computer and use it in GitHub Desktop.
Save chap/4dceb021a96842bcaa24 to your computer and use it in GitHub Desktop.
network-blocking-example.ino
#include "spark_disable_cloud.h" // disable cloud on start
int switchPin = D4;
int ledPin = D7;
unsigned long lastSwitchedAt = 0;
bool switched = false;
enum SparkAction {
none, sparkConnect, sparkDisconnect
};
volatile SparkAction action = none;
void switchSwitched(void);
void setup() {
pinMode(switchPin, INPUT_PULLDOWN);
pinMode(ledPin, OUTPUT);
attachInterrupt(switchPin, switchSwitched, CHANGE);
}
void loop() {
if (switched && (millis() - lastSwitchedAt > 200)) {
lastSwitchedAt = millis();
if(digitalRead(switchPin) == HIGH) {
action = sparkConnect;
digitalWrite(ledPin, HIGH);
} else {
action = sparkDisconnect;
digitalWrite(ledPin, LOW);
}
}
switched = false;
SparkAction previous = action;
action = none;
switch (previous) {
case sparkConnect: Spark.connect(); break;
case sparkDisconnect: Spark.disconnect(); break;
}
delay(10);
}
void switchSwitched() {
switched = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment