Professional Documents
Culture Documents
int protocol;
// initialize the serial port for the xBee with whatever pins are used for it
SoftwareSerial xBee(2, 3); // (TX, RX) : pins on XBee adapter
// declare a button to be used for input, without a pulldown resistor on the board
Button butt(7, INPUT_PULLUP);
//int action1 = butt.checkButtonAction();
int butter = 0; // counts the number of times the button has been clicked
int messages = 0; // counts the number of messages that have been received
#define NUMPIXELS 60 // This should be the total number of LEDs in your strip
#define DATAPIN 4
#define CLOCKPIN 5
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
//LCD
Adafruit_LiquidCrystal lcd(0);
//HALL EFFECT
//constants wont change
const int hallPin = 8; // the number of the hall effect sensor pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int hallState = 0; // variable for reading the hall sensor status
int pressureValue = 0;
int hallEffectValue = 0;
String lcdValue = "--";
String lcdChec k = "--";
int systemUnlock = 0;
//PRESSURE SENSOR
int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrCurrent; // the analog reading from the FSR resistor divider
int fsrInit = 0; // calibration
int fsrDiff;
//Buzzer
const int buzzerPin = 12;
int alarm = 0;
//LED Strip
int ledData = 4;
int ledClock = 5;
int prevProtocol;
void setup(void) {
//SEND
xBee.begin(9600);
//LCD
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.clear();
//HALL EFFECT
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the hall effect sensor pin as an input:
pinMode(hallPin, INPUT);
//PRESSURE SENSOR
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
calibrate();
//LED
strip.begin();
strip.show();
//millis
onTime = millis();
}
void loop() {
//SEND
systemCheck();
lcdPrint();
arm();
xBeeCode();
}
void systemCheck(){
if(systemUnlock == 0){
protocol = 0;
hallEffect();
pressureSensor();
sensorCheck();
}
else if(systemUnlock == 1){
lcdValue = "System Unlocked";
ledFade();
}
}
//HALL EFFECT
void hallEffect() {
//HALL EFFECT
hallState = digitalRead(hallPin);
if (hallState == LOW) {
noTone(12);
lcdValue = "Food Is Safe";
hallEffectValue = 0;
// sensorCheck();
}
else if (hallState == HIGH) {
hallEffectValue = 1;
// sensorCheck();
}
else {
lcd.clear();
lcdValue = "No Magnet";
noTone(12);
}
}
void lcdPrint(){
if (lcdCheck != lcdValue){
lcd.clear();
lcd.print(lcdValue);
}
lcdCheck = lcdValue;
}
//PRESSURE SENSOR
void calibrate() {
fsrInit = analogRead(fsrPin);
}
//ALARM BUZZER
void alarmBuzzer() {
// Alarm Buzzer
tone(12, 2500);
ledOff();
delay(300);
noTone(12);
ledRed();
}
void pressureSensor() {
//PRESSURE SENSOR
fsrCurrent = analogRead(fsrPin) - fsrInit;
if(millis() - onTime <= 500){
Serial.print("Analog reading = ");
Serial.print(fsrCurrent); // the raw analog reading
Serial.print("Initial reading = ");
Serial.print(fsrInit);
onTime = 0;
}
void sensorCheck(){
if((hallEffectValue == 1) && (pressureValue == 1)){
alarmBuzzer();
lcdValue = "Alarm";
protocol = 12;
}
else if((hallEffectValue == 0) && (pressureValue == 0)){
ledGreen();
lcdValue = "Food is Safe";
protocol = 10;
}
else if(pressureValue == 1){
alarmBuzzer();
lcdValue = "Alarm";
protocol = 12;
}
else if (hallEffectValue == 1){
ledAlarm();
lcdValue = "Door Open";
protocol = 9;
}
else{
ledYellow();
}
}
void remoteLock(){
if (systemUnlock == 0){
for (int i = 0; i < 3; i++){
ledOff();
delay(1000);
ledYellow();
}
}
else if(systemUnlock == 1){
for (int i = 0; i < 10; i++){
ledOff();
delay(1000);
ledGreen();
if (i == 9){
systemUnlock = 0;
}
}
}
}
void arm() {
if (systemUnlock == 1) {
int action1 = butt.checkButtonAction();
if (action1 == Button::PRESSED) {
for (int i = 0; i < 10; i++){
ledOff();
delay(1000);
ledGreen();
if (i == 9){
systemUnlock = 0;
}
}
}
}
}
void ledAlarm(){
ledOff();
delay(200);
ledRed();
}
void ledOff(){
digitalWrite(ledPin, HIGH);
for (int i=0; i <= NUMPIXELS; i=i+1) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
void ledRed(){
digitalWrite(ledPin, HIGH);
for (int i=0; i <= NUMPIXELS; i=i+1) {
strip.setPixelColor(i, 0, 255, 0);
}
strip.show();
}
void ledGreen(){
digitalWrite(ledPin, HIGH);
for (int i=0; i <= NUMPIXELS; i=i+1) {
strip.setPixelColor(i, 255, 0, 0);
}
strip.show();
}
int fadeMsgCheck() {
String msg = checkMessageReceived();
if (msg.length() > 0) {
String msgFields[MAX_FIELDS];
// intialize them all to null strings each time we start processing a new message
for (int i = 0; i < MAX_FIELDS; i++) {
msgFields[i] = "";
}
void ledFade(){
digitalWrite(ledPin, HIGH);
int exitForLoop;
for (int fadeValue=255; fadeValue >= 0; fadeValue--) {
exitForLoop = fadeMsgCheck();
if (exitForLoop == 10) {
systemUnlock = 0;
break;
}
for (int i=0; i <= NUMPIXELS; i=i+1) {
strip.setPixelColor(i, fadeValue, 0, 255);
}
strip.show();
delay(5);
}
void ledYellow(){
digitalWrite(ledPin, HIGH);
for (int i=0; i <= NUMPIXELS; i=i+1) {
strip.setPixelColor(i, 255, 255, 0);
}
strip.show();
}
void xBeeCode() {
if (protocol != 0) {
if (prevProtocol != protocol) {
String msg = myNodeName + "\t" + protocol + "\n";
Serial.print(msg);
xBee.print(msg);
prevProtocol = protocol;
}
}
if (msg.length() > 0) {
// if the result is a null string, then there is not a complete message ready
// otherwise we have received a complete message and can process it
// now that we have the message you just recieved in one big string ("msg"),
// it is very likely you might want to parse it using String object methods:
// https://www.arduino.cc/en/Reference/StringObject
// Below is some code that will split out the tab-delimited fields in that big
string
// into an array of strings ("msgFields[]"), each entry representing a different
field
// (in the order they were received)
String msgFields[MAX_FIELDS];
// intialize them all to null strings each time we start processing a new message
for (int i = 0; i < MAX_FIELDS; i++) {
msgFields[i] = "";
}
// now we should have filled the array of Strings 'msgFields' with 'fieldsFound'
entries
// Serial.print("found fields = ");
// Serial.println(fieldsFound);
// for (int i = 0; i < fieldsFound; i++) {
// Serial.println(msgFields[i]);
// }
if (msgFields[0].equals("R")){
aNode = "Remote";
}
else if (msgFields[0].equals("B")){
aNode = "Food Box";
}
else if (msgFields[0].equals("C")){
aNode = "Command Center";
}
else {
aNode = "Unknown";
}
Serial.print("Site = ");
Serial.println(aNode);
// finally, these fields are all strings, so what if you know that the second
field, e.g., is a string
// that really represents a number and you want to use it as an integer? here's
what you do:
// -- remember that the index of field #2 is really [1].
int aNumber = msgFields[1].toInt();
Serial.print("Code = ");
Serial.println(aNumber);
// and then you could do something like this, if that 2nd field is your message
type code:
switch (aNumber) {
case 1:
// message code = 1 action
Serial.println("code 1");
break;
case 2:
// message code = 2 action
Serial.println("code 2");
break;
case 3:
// message code = 3 action
Serial.println("code 3");
break;
case 4:
// message code = 4 action
Serial.println("cose 4");
break;
case 5:
// message code = 5 action
Serial.println("code 5");
break;
case 6:
// message code = 6 action
Serial.println("code 6");
break;
case 7:
// message code = 7 action
Serial.println("code 7");
break;
case 8:
// message code = 8 action
Serial.println("code 8");
protocol = 8;
systemUnlock = 1;
break;
case 9:
// message code = 9 action
Serial.println("code 9");
break;
case 10:
// message code = 10 action
Serial.println("code 10");
protocol = 0;
remoteLock();
break;
case 11:
// message code = 11 action
Serial.println("code 11");
break;
case 12:
// message code = 12 action
Serial.println("code 12");
break;
default:
// if nothing else matches, do the default
Serial.println("code unknown");
break;
}
}
}
// checks to see if a complete message (one terminated by a newLine) has been received
// if it has, the message will be returned to the caller (without the newLine)
// if not, it will keep accumulating characters from the xBee and just return a null
string.
String checkMessageReceived () {
static String msgBuffer = ""; // buffer to collect incoming message: static instead
of global !
String returnMsg = ""; // the result to return to the caller
if (xBee.available()) {
return returnMsg;