-
-
Save chap/4dceb021a96842bcaa24 to your computer and use it in GitHub Desktop.
network-blocking-example.ino
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
#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