Created
June 30, 2014 20:16
-
-
Save koenbollen/a87b3eca1e92f2dade70 to your computer and use it in GitHub Desktop.
Generic Panic Button - Firmware
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
/* Copyright (c) 2014, Koen Bollen <meneer@koenbollen.nl> | |
Permission to use, copy, modify, and/or distribute this software for any purpose | |
with or without fee is hereby granted, provided that the above copyright notice | |
and this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS | |
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | |
THIS SOFTWARE. */ | |
#include <SoftwareSerial.h> | |
#define BLUETOOTH_RX 10 | |
#define BLUETOOTH_TX 11 | |
#define PIN_SOURCE 8 | |
#define PIN_QUIT 9 | |
#define PIN_LED 13 | |
SoftwareSerial bluetooth = SoftwareSerial(BLUETOOTH_RX, BLUETOOTH_TX); | |
boolean down; | |
int led_timer; | |
int ping_timer; | |
unsigned long prev; | |
unsigned long test; | |
void setup() { | |
pinMode(PIN_SOURCE, INPUT); | |
pinMode(PIN_QUIT, OUTPUT); | |
pinMode(PIN_LED, OUTPUT); | |
digitalWrite(PIN_SOURCE, HIGH); // Source pin is in pull-down mode | |
digitalWrite(PIN_QUIT, LOW); | |
digitalWrite(PIN_LED, LOW); | |
down = false; | |
led_timer = -1; | |
ping_timer = 30000; // initial timeout for connecting | |
prev = millis(); | |
pinMode(BLUETOOTH_RX, INPUT); | |
pinMode(BLUETOOTH_TX, OUTPUT); | |
bluetooth.begin(9600); | |
} | |
void loop() { | |
// Yes, this is created by a game progammer: | |
unsigned long now = millis(); | |
int elapsed = min(max(0, now-prev), 1000); | |
prev = now; | |
if (bluetooth.available()) { | |
char i = bluetooth.read(); | |
switch(i) { | |
case 'P': // Ping, should be send within 30 sec; say every 20 sec | |
ping_timer = 30000; | |
bluetooth.print("P"); | |
break; | |
case 'Q': // Quit, turn off device | |
digitalWrite(PIN_QUIT, HIGH); | |
break; | |
case 'L': // Led blink | |
digitalWrite(PIN_LED, HIGH); | |
led_timer = 50; | |
break; | |
case 'N': // Led oN | |
digitalWrite(PIN_LED, HIGH); | |
break; | |
case 'F': // Led ofF | |
digitalWrite(PIN_LED, LOW); | |
break; | |
case '\r': | |
case '\n': // Dont error on newline bytes | |
break; | |
default: // Error | |
bluetooth.print("E"); | |
break; | |
} | |
} | |
int state = digitalRead(PIN_SOURCE); | |
if(state == LOW && !down) { // Is just down | |
bluetooth.print("D"); | |
down = true; | |
} else if(state == HIGH && down) { // Is just up | |
bluetooth.print("U"); | |
down = false; | |
} | |
// If 'L' is sent, led_timer is set to 50, count down and turn off the led | |
if(led_timer > 0 && (led_timer -= elapsed) <= 0) { | |
digitalWrite(PIN_LED, LOW); | |
} | |
// When this timer is run out, pull-up the quit PIN that turns off the device | |
if(ping_timer > 0 && (ping_timer -= elapsed) <= 0) { | |
digitalWrite(PIN_QUIT, HIGH); | |
} | |
delay(10); | |
} | |
// Sincerely, | |
// —Koen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment