Skip to content

Instantly share code, notes, and snippets.

@dmiddlecamp
Created January 22, 2015 01:49
Show Gist options
  • Save dmiddlecamp/02f2f0c2d7c36a0ab685 to your computer and use it in GitHub Desktop.
Save dmiddlecamp/02f2f0c2d7c36a0ab685 to your computer and use it in GitHub Desktop.
keen laser code
int laser = 0;
unsigned int lastPublish = 0;
bool lastState = false;
void setup() {
pinMode(A0, INPUT_PULLDOWN);
pinMode(D7, OUTPUT);
}
void loop() {
int val = analogRead(A0);
unsigned int now = millis();
unsigned int elapsed = now - lastPublish;
bool broken = val < 4000;
if (broken == lastState) {
return;
}
digitalWrite(D7, (broken) ? HIGH : LOW);
if (elapsed >= 1000) {
String topic = ((broken) ? "laser/alarm" : "laser/reset");
String message = String(val);
Spark.publish(topic, message);
lastState = broken;
lastPublish = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment