FabLab SPQwoRk
Published © CC BY-NC

Locks controlled via bluetooth

Onesheeld application. With this you can allow up to 10 person to use some resources as doors etc, with name associate to a numeric pass.

IntermediateWork in progress4,189
Locks controlled via bluetooth

Things used in this project

Hardware components

Android device
Android device
×1
LED (generic)
LED (generic)
Status Leds 2 red, 2 green
×4
1Sheeld
1Sheeld
×1
Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
HITEC servo motors HS-422
×1

Story

Read more

Custom parts and enclosures

Servo motor Lock enclosure for 3d Printer

Schematics

MakerSpaceFritzing.fzz

Code

Untitled file

C/C++
/*-----------------------------------------------------------------------------------------------------------------------------------------
  Onesheeld application that use 3 Shields : VoiceRecognition; Keypad; TextToSpeech. You must run those items in OneSheeld Android App,
  in order to use this application. With this you can allow up to 10 person to use some critical resources, doors, lock box etc, with
  name associate to a numeric password. In our case we use this program to control the cabinet doors of FabLab crew. In normal mode the
  person who want to use the resource, say is name in the VoiceRecognition section and then compone his password in the Keypad.
  All the operation are  articulated by a voice through TextToSpeech.

  Paolo Sanna Technical Manager of FabLab SPQwoRk May 20015
  
  ------------------------------------------------------------------------------------------------------------------------------------------*/



/* invoke OneSheeld library */
#include <OneSheeld.h>
#include <Servo.h>

/*definition block*/
#define maxNumberofGuy 10 /* 10 number of max users */
#define startName "noOne"

/*variables*/
int ledPin1 = 4;
int ledStatus1 = 7;
int ledPin2 = 8;
int ledStatus2 = 12;
int Actualpassword;
char *voiceCommand;
String voiceCommandString;
String actualName;
String arrayName[maxNumberofGuy] = {"paolo", "tommaso", "sergio", "fabiano", "jonathan", "lorenzo", "eleonora", "valerio"}; //members
int arrayPassword[maxNumberofGuy] = {1234, 4321, 9874, 4789, 6547, 7456, 8523, 3258}; //default passwords
int arrayLockPin[maxNumberofGuy] = {ledPin1,ledPin2, }; //led lock pairs
int arrayLockStatus[maxNumberofGuy] = {ledStatus1,ledStatus2, };
int positionArray;
int count = 0;
//create servo object for each lock
Servo lock1; // lock number 1
Servo lock2; // lock number 2

/*Commands Flags*/
bool passAquired = false;
bool vacancyFlag = false;
bool blinkFlag = false;
bool wrongName = false;
bool newCommandFlag = true;

/*initialization */
void setup()
{
  OneSheeld.begin();
  actualName.reserve(11);
  positionArray = 0;
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledStatus1, OUTPUT);
  pinMode(ledStatus2, OUTPUT);
  actualName = startName;
  lock1.attach(3);
  lock2.attach(5);
  lock1.write(0);
  lock2.write(0);
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledStatus1,HIGH);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledStatus2,HIGH);
}

void loop()
{
  searchName();
}
/*Functions definition*/

void searchName()
{
 
  if (actualName == startName){
      for(int pos = 0; pos<10; pos++){ 
    if (blinkFlag){
      digitalWrite(arrayLockStatus[pos], HIGH); OneSheeld.delay(50);
    } else 
    if (!blinkFlag){
      digitalWrite(arrayLockStatus[pos], LOW); OneSheeld.delay(50);
    }
    }
    blinkFlag = !blinkFlag;
  }

  positionArray = 0;
  passAquired = false;
  if (VoiceRecognition.isNewCommandReceived( )){
    voiceCommandString = VoiceRecognition.getCommandAsString();
    OneSheeld.delay(1000);
    actualName = String(voiceCommandString);
    if (Names(actualName) && (count < 3)){
      TextToSpeech.say(F("Insert password"));
      OneSheeld.delay(1000);
      while (!Keypad.isAnyRowPressed() && !passAquired){
        Keypad.setOnButtonChange(&password);
        OneSheeld.delay(1000);
      }
      count++;
      passControl(positionArray);
    } else 
    if (!(Names(actualName)) || (count = 3)){
      actualName = startName;
    }
  }
}

void commandMenu()
{
  newCommandFlag = true;
  TextToSpeech.say(F("Command"));
  OneSheeld.delay(2000);
  while (newCommandFlag)
  {
    OneSheeld.delay(2000);
    if ( VoiceRecognition.isNewCommandReceived()){
      OneSheeld.delay(2000);
      voiceCommand = VoiceRecognition.getLastCommand();
      if (strstr(voiceCommand, "open") != NULL) { // open lock   
        openLock(positionArray);
      } else 
      if (strstr(voiceCommand, "close") != NULL){ // close lock     
        closeLock(positionArray);
      } else 
      if (strstr(voiceCommand, "password") != NULL) { // change password      
        changePassword();
      } else 
      if (strstr(voiceCommand, "exit") != NULL){ // exit  menu
        newCommandFlag = false;
        TextToSpeech.say(F("Goodbye"));
        OneSheeld.delay(1000);
        ledBlink(arrayLockStatus[positionArray], 2);
        actualName = startName;
        searchName();
      } else {
        TextToSpeech.say(F("Repeat"));
        OneSheeld.delay(1000);
      }
    }
  }
}

bool Names(String seekName)
{
  boolean machedName = false;
  /* search the name in the list of FabLab crew members */
  do  {
    if (arrayName[positionArray] == seekName){
      machedName = true;
    } else {
      positionArray++;
    }
  }  while ((!machedName) && (positionArray < 9));
  if (!machedName){
    return false;
  } else 
  if (machedName){
    return true;
    ledBlink(arrayLockStatus[positionArray], 2);
    digitalWrite(arrayLockPin[positionArray], HIGH);
  }
}

void password(byte row, byte column)
{
  /*password will be formed by at maximum of 5 numbers */
  int newPassword[] = {0, 0, 0, 0, 0};
  int count = 5;
  bool aquiredFlag = false;
  int numberHold;
  if (!Keypad.isAnyRowPressed()) {}
  else {
    /*to insert the password you must start with the asterisk  and ends with hash */
    if (!(Keypad.isRowPressed(3) && Keypad.isColumnPressed(0))){
      TextToSpeech.say(F("Press asterisk"));
      OneSheeld.delay(2000);
    }
    else {
      TextToSpeech.say(F("Ready"));

      OneSheeld.delay(1000);
      while (!aquiredFlag && count != 0 ){
        numberHold = numberPressed();
        OneSheeld.delay(1000);
        /*create the password from buttons pushed in the Keypad */
        if (numberHold == 10) {
          aquiredFlag = true;
        } else {
          aquiredFlag = false;
        }
        if ((count == 5) && !aquiredFlag){
          {
            count--;
            newPassword[4] = numberHold;
          }
        } else 
        if ((count == 4) && !aquiredFlag){
          {
            count--;
            newPassword[3] = newPassword[4];
            newPassword[4] = numberHold;
          }
        } else 
        if ((count == 3) && !aquiredFlag){
          { count--; newPassword[2] = newPassword[3]; newPassword[3] = newPassword[4];
            newPassword[4] = numberHold;
          }
        } else 
        if ((count == 2) && !aquiredFlag){
          { count--; newPassword[1] = newPassword[2]; newPassword[2] = newPassword[3];
            newPassword[3] = newPassword[4]; newPassword[4] = numberHold;
          }
        } else 
        if ((count == 1) && !aquiredFlag){
          { count--; newPassword[0] = newPassword[1]; newPassword[1] = newPassword[2];
            newPassword[2] = newPassword[3]; newPassword[3] = newPassword[4]; newPassword[4] = numberHold;
          }
        }
      }
      Actualpassword = 10000 * newPassword[0] + 1000 * newPassword[1] + 100 * newPassword[2] + 10 * newPassword[3] + newPassword[4]; // transform the passwords digits to a number
      passAquired = true;
    }
  }
}

void passControl(int n)
{
  passAquired = false;
  int pass = arrayPassword[n];   //transfer the password value to pass for password test
  //password tests
  if (Actualpassword == pass){
    TextToSpeech.say(F("Access allowed"));
    OneSheeld.delay(2000);
    commandMenu();         //if the password is correct go to the commands menu
  } else 
  if (Actualpassword != pass){
    TextToSpeech.say(F("Access denied"));
    OneSheeld.delay(2000);
    searchName();     //if the password is wrong return to the first generic menu
  }
}

int numberPressed()
{
  TextToSpeech.say(F("Insert number"));
  /* test the keypad buttons to return the correct number pressed */
  while (!Keypad.isAnyRowPressed())
  {
    OneSheeld.delay(1500);
    if (Keypad.isRowPressed(0) && Keypad.isColumnPressed(0)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 1;
    } else 
    if (Keypad.isRowPressed(0) && Keypad.isColumnPressed(1)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 2;
    } else 
    if (Keypad.isRowPressed(0) && Keypad.isColumnPressed(2)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 3;
    } else 
    if (Keypad.isRowPressed(1) && Keypad.isColumnPressed(0)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 4;
    } else 
    if (Keypad.isRowPressed(1) && Keypad.isColumnPressed(1)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 5;
    } else 
    if (Keypad.isRowPressed(1) && Keypad.isColumnPressed(2)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 6;
    } else 
    if (Keypad.isRowPressed(2) && Keypad.isColumnPressed(0)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 7;
    } else 
    if (Keypad.isRowPressed(2) && Keypad.isColumnPressed(1)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 8;
    } else 
    if (Keypad.isRowPressed(2) && Keypad.isColumnPressed(2)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 9;
    } else 
    if (Keypad.isRowPressed(3) && Keypad.isColumnPressed(1)) {
      ledBlink(arrayLockPin[positionArray], 1);
      return 0;
    } else 
    if (Keypad.isRowPressed(3) && Keypad.isColumnPressed(2)) {
      ledBlink(arrayLockPin[positionArray], 2);
      return 10;
    } else 
    if (Keypad.isRowPressed(3) && Keypad.isColumnPressed(0)) {
      return 11;
    } else 
    if (Keypad.isRowPressed(3) && Keypad.isColumnPressed(3)) {
      return 11;
    } else 
    if (Keypad.isRowPressed(2) && Keypad.isColumnPressed(3)) {
      return 11;
    } else 
    if (Keypad.isRowPressed(1) && Keypad.isColumnPressed(3)) {
      return 11;
    } else 
    if (Keypad.isRowPressed(0) && Keypad.isColumnPressed(3)) {
      return 11;
    }
  }
}

void ledBlink(int LedtoBlink, int typeofBlink)
{
  switch (typeofBlink)
  {
    case 1: digitalWrite(LedtoBlink, HIGH); {
        OneSheeld.delay(200);
        digitalWrite(LedtoBlink, LOW);
        break;
      }
    case 2: digitalWrite(LedtoBlink, HIGH); {
        OneSheeld.delay(50); digitalWrite(LedtoBlink, LOW);
        OneSheeld.delay(50); digitalWrite(LedtoBlink, HIGH); OneSheeld.delay(100); digitalWrite(LedtoBlink, LOW); break;
      }
  }
}

void openLock(int positionLock)
{
  switch (positionLock){
   case 0: {lock1.attach(3);lock1.write(90);OneSheeld.delay(1000); lock1.detach(); digitalWrite(arrayLockPin[positionArray],HIGH);digitalWrite(arrayLockStatus[positionArray],LOW);commandMenu(); break;}
   case 1: {lock2.attach(5);lock2.write(90);OneSheeld.delay(1000); lock2.detach(); digitalWrite(arrayLockPin[positionArray],HIGH);digitalWrite(arrayLockStatus[positionArray],LOW);commandMenu(); break;}   
 }
}

void closeLock(int positionLock)
{
    switch (positionLock){
   case 0: {lock1.attach(3);lock1.write(0);OneSheeld.delay(1000); lock1.detach();digitalWrite(arrayLockPin[positionArray],LOW);digitalWrite(arrayLockStatus[positionArray],HIGH); searchName(); break;}
   case 1: {lock2.attach(5);lock2.write(0);OneSheeld.delay(1000); lock2.detach();digitalWrite(arrayLockPin[positionArray],LOW);digitalWrite(arrayLockStatus[positionArray],HIGH); searchName(); break;}   
 }  
}

void changePassword()
{
  TextToSpeech.say(F("Insert new password"));
  OneSheeld.delay(2000);
  while (!Keypad.isAnyRowPressed() && !passAquired){
    Keypad.setOnButtonChange(&password);
    OneSheeld.delay(2000);
  }
  arrayPassword[positionArray] = Actualpassword;
  ledBlink(arrayLockStatus[positionArray], 2);
  TextToSpeech.say(F("Password aquired"));
  OneSheeld.delay(2000);
  passAquired = false;
}

Credits

FabLab SPQwoRk

FabLab SPQwoRk

1 project • 5 followers
FabLab placed in Roma

Comments