Skip to content

Instantly share code, notes, and snippets.

@ReidCarlberg
Last active December 27, 2015 21:39
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 ReidCarlberg/7393517 to your computer and use it in GitHub Desktop.
Save ReidCarlberg/7393517 to your computer and use it in GitHub Desktop.
/*
Reid Carlberg
reid.carlberg@gmail.com
v4 - With updated alarm LED wiring and new DISCO handler
v3 - With alarm LED, Longer duration, 00 is key for "ALL DEVICES"
v2 - Custom serial scheme & fixed ID
v1 - JSON & Random ID
*/
// include the library code:
#include<stdlib.h>
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
//disco pin
//startup
boolean isSettled = false;
int startupSettleTime = 7500;
int unusedPinForRandomSeed = A5;
//Identification
String myId = "C9";
boolean isRegistered = false;
long lastRegisterRequest = 0;
int registrationInterval = 5000;
long lastLoop = 0;
long loopInterval = 5000;
long loopCount = 0;
//Serial
String lastMessage = String("");
String inboundMessage = String("");
char newChar;
boolean isMessage;
//indicators
int activityPin = 2;
int notRegisteredPin = 2;
int registeredPin = 3;
int alarmPinLED = 4;
//temperature
int tmp36Pin = A0;
long lastTempRead = 0;
long tempReadInterval = 120000; //2 minutes
//disco
int discoPin = 6;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, discoPin, NEO_GRB + NEO_KHZ800);
//alarm
//char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
//int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
int alarmPin = 5;
int alarmTone = 1915;
int alarmDuration = 10000; //seconds
void setup() {
//LCD
lcd.begin(16, 2);
updateLcd("in setup", "");
//DISCO
strip.begin();
strip.show();
Serial.begin(9600);
//configureRadio();
pinMode(activityPin, OUTPUT);
pinMode(notRegisteredPin, OUTPUT);
pinMode(registeredPin, OUTPUT);
pinMode(alarmPin, OUTPUT);
}
void loop() {
if (!isSettled) {
digitalWrite(registeredPin, HIGH);
digitalWrite(notRegisteredPin, HIGH);
updateLcd("settling...");
delay(startupSettleTime);
isSettled = true;
digitalWrite(registeredPin, LOW);
digitalWrite(notRegisteredPin, LOW);
}
if (millis() - lastLoop > loopInterval) {
loopCount++;
updateLcd("looping " + String(loopCount), String(isRegistered) + "-" + myId);
lastLoop = millis();
}
//do we have anything coming in on the wire?
handleInboundSerial();
handleRegisteredLights();
//do we have a message to work with?
handleLastMessage();
//is idRegistered
if (isRegistered ) {
//ready to be autonomous
if (millis() - lastTempRead > tempReadInterval) {
reportTemperature();
}
} else {
if (millis() - lastRegisterRequest > registrationInterval) {
handleSerialSend("hello", "tmp36,alarm");
lastRegisterRequest = millis();
}
}
delay(1000);
}
void handleSerialSend(String message) {
handleSerialSend(message, "");
}
void handleSerialSend(String message, String detail) {
String toSend = "{ \"addr\" :\"" + myId + "\", \"msg\": \"" + message + "\", \"detail\": \"" + detail + "\" }";
Serial.flush();
Serial.println(toSend);
Serial.flush();
handleActivityLight();
}
void handleSoundAlarm() {
digitalWrite(alarmPinLED, HIGH);
playTone(alarmTone,alarmDuration);
digitalWrite(alarmPinLED, LOW);
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(alarmPin, HIGH);
delayMicroseconds(tone);
digitalWrite(alarmPin, LOW);
delayMicroseconds(tone);
}
}
void handleActivityLight() {
for (int x = 0; x < 5; x++) {
digitalWrite(activityPin, HIGH);
delay(50);
digitalWrite(activityPin, LOW);
delay(50);
}
handleRegisteredLights();
}
void handleRegisteredLights() {
if (isRegistered) {
digitalWrite(registeredPin, HIGH);
digitalWrite(notRegisteredPin, LOW);
} else {
digitalWrite(registeredPin, LOW);
digitalWrite(notRegisteredPin, HIGH);
}
}
void handleLastMessage() {
if (lastMessage.length() < 8)
return;
String address = lastMessage.substring(0,2);
String message = lastMessage.substring(3);
updateLcd(address, message);
if (address.equalsIgnoreCase(myId) || address.equalsIgnoreCase("00")) {
if (message.equalsIgnoreCase("olleh")) {
isRegistered = true;
} else if (message.equalsIgnoreCase("hello")) {
handleSerialSend("olleh");
} else if (message.equalsIgnoreCase("alarm")) {
handleSoundAlarm();
} else if (message.equalsIgnoreCase("tmp36")) {
reportTemperature();
} else if (message.equalsIgnoreCase("disco")) {
handleDisco();
}
}
lastMessage = "";
//aJson.deleteItem(jsonObject);
}
void reportTemperature() {
float rawTemp = (analogRead(tmp36Pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
float centigrade = (rawTemp - .5) * 100;
char tmp[10];
dtostrf(centigrade,3,3,tmp);
String reportValue = String(tmp);
updateLcd("TMP36", reportValue);
handleSerialSend("TMP36", reportValue);
lastTempRead = millis();
}
void handleInboundSerial() {
if (Serial.available() > 5) {
while(Serial.available() > 0) {
newChar = Serial.read();
if (newChar == '^') {
isMessage = true;
} else if (newChar == '\n') {
isMessage = false;
lastMessage = String(inboundMessage);
inboundMessage = "";
}
if (isMessage && newChar != '^') {
inboundMessage += String(newChar);
}
}
handleActivityLight();
}
}
void updateLcd(String line1) {
updateLcd(line1, "");
}
void updateLcd(String line1, String line2) {
lcd.clear();
lcd.print(line1.substring(0,15));
lcd.setCursor(0,1);
lcd.print(line2.substring(0,15));
}
void handleDisco() {
for (int x = 0; x<30; x++) {
int glitter = random(0,29);
int r = 255;
int g = 255;
int b = 255;
int color = random(1,4);
if (color == 1) {
g = 0;
b = 0;
} else if (color == 2) {
r = 0;
b=0;
} else if (color == 3) {
r=0;
g=0;
}
strip.setPixelColor(glitter, strip.Color(r,g,b));
strip.show();
delay(50);
strip.setPixelColor(glitter, strip.Color(0,0,0));
strip.show();
delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment