Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2014 06:08
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 anonymous/d63415e3805a684755bd to your computer and use it in GitHub Desktop.
Save anonymous/d63415e3805a684755bd to your computer and use it in GitHub Desktop.
Unfinished Bluetooth LE Lock
/*********************************************************************
This is an example for our nRF8001 Bluetooth Low Energy Breakout
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/1697
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Kevin Townsend/KTOWN for Adafruit Industries.
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in any redistribution
For more info go to: http://handya.co.nz/post/89734624902/bluetooth-enabled-tool-cupboards
*********************************************************************/
// This version uses the internal data queing so you can treat it like Serial (kinda)!
#include <CapacitiveSensor.h>
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
CapacitiveSensor cs_4_2 = CapacitiveSensor(5,6);
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
int lock1 = 8;
int lock2 = 7;
int time = 0;
int timeDelay = 2500;
int green = 16;
int orange = 15;
int red = 14;
int swi = 18;
int mode = 0;
void setup(void)
{
Serial.begin(9600);
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
pinMode(lock1, OUTPUT);
pinMode(lock2, OUTPUT);
pinMode(green, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(red, OUTPUT);
BTLEserial.begin();
time = millis();
digitalWrite(red, HIGH);
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
if(mode == 0){
}
if(mode == 1){
long total = cs_4_2.capacitiveSensor(30);
Serial.println(total);
if(total > 8000){
digitalWrite(lock2, HIGH);
digitalWrite(orange, LOW);
digitalWrite(green, HIGH);
Serial.println("cap unlock ON");
delay(1000);
digitalWrite(lock2, LOW);
digitalWrite(orange, HIGH);
digitalWrite(green, LOW);
}
}
int swi = analogRead(A5);
//if(swi > 100){
//}
if(swi < 100){
mode = 0;
digitalWrite(red, HIGH);
digitalWrite(orange, LOW);
}
delay(10);
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
if (status == ACI_EVT_CONNECTED) {
// Lets see if there's any data for us!
if (BTLEserial.available()) {
Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
String str(c);
//Serial.print(c);
if(str == "r"){
digitalWrite(lock1, LOW);
digitalWrite(green, LOW);
Serial.print("lock 1 Off");
}
else if(str == "R"){
digitalWrite(lock1, HIGH);
digitalWrite(green, HIGH);
Serial.print("lock 1 ON");
}
else if(str == "l"){
digitalWrite(lock2, LOW);
digitalWrite(green, LOW);
Serial.print("lock 2 Off");
}
else if(str == "L"){
digitalWrite(lock2, HIGH);
digitalWrite(green, HIGH);
Serial.print("lock 2 ON");
}
else if (str == "m"){
mode = 0;
digitalWrite(red, HIGH);
digitalWrite(orange, LOW);
}
else if (str == "M"){
mode = 1;
digitalWrite(orange, HIGH);
digitalWrite(red, LOW);
}
}
// Next up, see if we have any data to get from the Serial console
if (Serial.available()) {
// Read a line from Serial
Serial.setTimeout(100); // 100 millisecond timeout
String s = Serial.readString();
// We need to convert the line to bytes, no more than 20 at this time
uint8_t sendbuffer[20];
s.getBytes(sendbuffer, 20);
char sendbuffersize = min(20, s.length());
Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.println("\"");
// write the data
BTLEserial.write(sendbuffer, sendbuffersize);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment