Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Untitled

68 views

Published on

  • Be the first to comment

  • Be the first to like this

Untitled

  1. 1. /* This sketch demonstrates the following features:       1) Button Debouncing - used to eliminate random HIGH/ LOW signals during button press/release       2) Continuous BLE broadcasting until the RFduino exhibits a system reset       3) Implementation to switch between different states upon 'ACTION' press:             -> Begins at state '0' at start-up             -> State transition: 0-1-0 after single ACTION press             -> State transition: 0-2 after hold event       4) Single ACTION press -> sends sms to emergency contact with gps co-ordinates                              -> activates continuous buzzer tone until hold event occurs       5) Hold ACTION event -> calls emergency contact        6) ON/OFF switch to turn off module -> single buzzer tone to signify power ON  */ #include <RFduinoBLE.h> #define ACTIONbutton 4    // GPIO4 used to track button press events #define speaker 5         // GPIO5 used to activate speaker/buzzer #define monitBatt 6       // GIPIO used to monitor battery level of the module #define holdTime 1000     // how long to wait for press + hold event (hold period in milliseconds) #define debounce 1     // debounce period (in milliseconds) to prevent flickering when pressing or releasing the ACTION button int buttonVal = 0; // value read from ACTION button int buttonLast = 0; // buffered value of the ACTION button's previous state int ledState = -1; // Keeps track of the state of ACTION1 int stateEvent = 0; // Keeps track of the state of ACTION2 long btnDnTime=0; // time the button was pressed down long btnUpTime=0; // time the button was released boolean ignoreUp = false; // whether to ignore the button release due to click & hold being triggered float analogON_OFF; // analog voltage reading from "ON_OFF" output float voltageON_OFF; // digital voltage reading from "ON_OFF" output // Code below is run only once void setup() {   Serial.begin(9600);   RFduinoBLE.deviceName = "Victor's RFduino"; // Set the BLE device name   RFduinoBLE.customUUID = "5eaca154-b39c-4235-af14-dc4db932d10c"; // Create a unique and custom UUID   RFduinoBLE.advertisementInterval = 100;   RFduinoBLE.begin(); // Start BLE broadcasting      RFduinoBLE.txPowerLevel = -20; // Set the BLE transmit power to -20 dBm
  2. 2. (Decibels per milliwatts)   pinMode(ACTIONbutton,INPUT); // Set the ACTION button as an input (GPIO4)   pinMode(speaker,OUTPUT); // Set the speaker output (GPIO5)   startUp(); } // Code below runs forever void loop() {    analogON_OFF = analogRead(monitBatt);   voltageON_OFF = 3.5*analogON_OFF/1023;      // For arduino testing purposes ONLY   if (voltageON_OFF <= 3.25){     Serial.print("Low Battery Warning: Please recharge the battery!");   else if (voltageON_OFF > 3.25){     Serial.print("/nBattery is healthy!");   }        // At low battery, there will be two quick beeps and a long delay   // Bluetooth will also be turned off   while (voltageON_OFF < 3.25){     RFduinoBLE.end();     lowBattWarn();     delay(500);   }      buttonVal = digitalRead(ACTIONbutton);        // Test for button pressed and store the down time   if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnUpTime) > long(debounce)){     btnDnTime = millis(); // Store the down time   }       // Test for button release and store the up time    if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnDnTime) > long(debounce)){      if (ignoreUp == false){ // "FALSE" only if the ACTION button is tapped once                stateEvent = 1;                if (stateEvent == 1){          stateEvent1();        }        
  3. 3.        delay(150);                if (stateEvent == 0){          stateEvent0();        }            }      else{        ignoreUp == false;      }           btnUpTime = millis(); // Store the up time    }       // Test for button held down for longer than the hold time    if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime)){       stateEvent = 2;              while (stateEvent == 2){         stateEvent2();       }             ignoreUp = true; // "TRUE" only if the ACTION button is held      btnDnTime = millis(); // Store down time    }         buttonLast = buttonVal; } // After initiating bluetooth connection, send an INT value of 0 through BLE void RFduinoBLE_onConnect(){   Serial.println("Bluetooth has been connected!");   RFduinoBLE.sendByte(0); } // Tell the user when the device loses bluetooth connection void RFduinoBLE_onDisconnect(){   Serial.println("Bluetooth has been disconnected!"); } // 'Home' state event void stateEvent0(){   RFduinoBLE.sendByte(0); } // First ACTION state event void stateEvent1(){
  4. 4.   RFduinoBLE.sendByte(1);   tone(5,60); // Generate a tone after first ACTION event   delay(100); // Delay used for repeating beeps   stateEvent = 0; } // Second ACTION state event void stateEvent2(){   noTone(5); // Turn off tone after second ACTION event   RFduinoBLE.sendByte(2); } // Two quick beeps at POWER ON  void startUp(){   for (int i=0; i <= 2; i++){     digitalWrite(speaker,HIGH);     delay(100);     digitalWrite(speaker,LOW);   } } // Two quick beeps then a long delay at low battery void lowBattWarn(){   for (int j=0; j<=2; j++){     digitalWrite(speaker,HIGH);     delay(100);     digitalWrite(speaker,LOW);   } }   

×