Problem with get value from the BTLE serial

Hi guys, i m New and get into arduino to do some app with bluetooth door lock
and i meet a problem that when i key in a value to checking for switch case and after that go to other function for asking password but it will keyin previous value to my password and checking it and show invalid any way to solve it?

// Bluetooth Low Energy Lock
// (c) 2014 Don Coleman
// 
// Bluefruit LE http://adafru.it/1697
// Solenoid Lock http://adafru.it/1512
// https://github.com/adafruit/Adafruit_nRF8001

#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#define LOCK_PIN 6
#define RED_LED_PIN 7
#define GREEN_LED_PIN 8

// 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 // interrupt pin 2 or 3 on UNO
#define ADAFRUITBLE_RST 9
long secret = 12345;
long openTime = 0;
boolean test = true;
// Status from the Bluefruit LE driver
int lastStatus = ACI_EVT_DISCONNECTED;

Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

void setup() {
  Serial.begin(9600);
  Serial.println(F("BLE Safe - Adafruit Bluefruit Low Energy Edition"));
  BTLEserial.begin();
  pinMode(LOCK_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);  
  digitalWrite(LOCK_PIN, LOW);
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW); 
  BTLEserial.println("Welcome");

}


void loop() {
  // Tell the nRF8001 to do whatever it should be working on
  BTLEserial.pollACI();

  int status = BTLEserial.getState();
      
  if (status != lastStatus) {
    if (status == ACI_EVT_DEVICE_STARTED) {
      Serial.println(F("* Advertising Started"));
    } 
    else if (status == ACI_EVT_CONNECTED) {
      Serial.println(F("* Connected!"));
    }     
    else if (status == ACI_EVT_DISCONNECTED) {
      Serial.println(F("* Disconnected or advertising timed out."));
    } 
    // save for next loop
    lastStatus = status;
  }
    
  if (status == ACI_EVT_CONNECTED && test == true) {
    
    // see if there's any data from bluetooth
    if (BTLEserial.available()) {
      Serial.print("* ");
      Serial.print(BTLEserial.available());
      Serial.println(F(" bytes available from BTLE"));
    }

    // keeping u + code for compatibility with the serial api
   int var = BTLEserial.parseInt();
   
   switch (var) {
    case 1:
      openLock();
      break;
    case 2:
      changePass();
      break;
    
  }
    

    
  }

  // close lock and reset lights after x seconds
  if (openTime && millis() - openTime > 4000) {
    resetLock();
  }
  
}

void openLock() {
    test = false;
    openTime = millis();  // set even if bad code so we can reset the lights
    BTLEserial.println("What is your password");

    BTLEserial.flush();
    int code = BTLEserial.parseInt();
    Serial.println(String(code));
    if (code == secret) { 
    // open the lock
    Serial.println("Code matches, opening lock");
    digitalWrite(GREEN_LED_PIN, HIGH); 
    digitalWrite(RED_LED_PIN, LOW);     
    digitalWrite(LOCK_PIN, HIGH); // open the lock
    BTLEserial.println("unlocked");    
  } else{
    // bad code, don't open
    Serial.println("Invalid code " + code);
    digitalWrite(RED_LED_PIN, HIGH);
    BTLEserial.println("invalid code");       
  }
  
}

void changePass() {
    BTLEserial.println("Passcode"); 
}

// closes the lock and resets the lights
void resetLock() { 
  // reset the lights
  digitalWrite(RED_LED_PIN, LOW); 
  digitalWrite(GREEN_LED_PIN, LOW);
  digitalWrite(LOCK_PIN, LOW); // close the lock
  BTLEserial.println("locked");
  openTime = 0;
  test = true;
}

Your openLock() function does not check to see whether data is available before reading it.

void openLock() {
    test = false;
    openTime = millis();  // set even if bad code so we can reset the lights
    BTLEserial.println("What is your password");

    BTLEserial.flush();
    int code = BTLEserial.parseInt();
    Serial.println(String(code));

What do you see when you print the code variable and why are you turning it into a String?

By the way, Serial.flush() does not clear the input buffer.
See http://arduino.cc/en/Serial/Flush

it show me 0 and only turn to string it will show it out

it is any way to clear the input buffer that i can put a new input to the code to check the password

jackeychua:
it is any way to clear the input buffer that i can put a new input to the code to check the password

If you feel that you really must clear the input buffer then read from it until there is nothing in it.

it show me 0 and only turn to string it will show it out

Did you note what I said about reading before checking that there is something [hint] available [/hint] to read ?

if i check with using the btleserial.read it get a value -1

jackeychua:
if i check with using the btleserial.read it get a value -1

-1 indicates that no data was read because there was none available.
Earlier in your code you do

   if (BTLEserial.available())

before reading the command byte but you don't do something similar before reading the password

but if i adding the if(BTLEserial.available()) it wont go for the invalid code but i type 12345 it wont store in, may i ask what happen?

void openLock() {
    test = false;

 
    BTLEserial.println("What is your password");
    [color=red]if (BTLEserial.available()) {[/color]
    int code = BTLEserial.parseInt();
    Serial.println(String(code));
    if (code == secret) { 
    openTime = millis();  // set even if bad code so we can reset the lights
    // open the lock
    Serial.println("Code matches, opening lock");
    digitalWrite(GREEN_LED_PIN, HIGH); 
    digitalWrite(RED_LED_PIN, LOW);     
    digitalWrite(LOCK_PIN, HIGH); // open the lock
    BTLEserial.println("unlocked");    
  } else{
        openTime = millis();  // set even if bad code so we can reset the lights
    // bad code, don't open
    Serial.println("Invalid code " + code);
    digitalWrite(RED_LED_PIN, HIGH);
    BTLEserial.println("invalid code");       
  }
    [color=red]}[/color]
}

You are prompting the user to enter their password but you don't wait for them to enter it so when you check to see if anything is available nothing is there so the code in the if is not executed. One way round this is to change the test for available password data form an if to a while and wait until something is entered.

Something like this (untested)

void openLock() 
{
  test = false;
  BTLEserial.println("What is your password");
  while (BTLEserial.available() = 0)
  {
    //do nothing until password input is available
  }
  int code = BTLEserial.parseInt();
  Serial.println(String(code));
  if (code == secret) 
  { 
    openTime = millis();  // set even if bad code so we can reset the lights
    // open the lock
    Serial.println("Code matches, opening lock");
    digitalWrite(GREEN_LED_PIN, HIGH); 
    digitalWrite(RED_LED_PIN, LOW);     
    digitalWrite(LOCK_PIN, HIGH); // open the lock
    BTLEserial.println("unlocked");    
  } 
  else
  {
    openTime = millis();  // set even if bad code so we can reset the lights
    // bad code, don't open
    Serial.println("Invalid code " + code);
    digitalWrite(RED_LED_PIN, HIGH);
    BTLEserial.println("invalid code");       
  }
}

bluetooth.ino: In function 'void openLock()':
bluetooth:99: error: lvalue required as left operand of assignment

got problem with this code

while (BTLEserial.available() = 0)

should be

while (BTLEserial.available() == 0)

dun have any working when i key in 12345 not any responde

Do you see the "What is your password" prompt ?
If so then you know that the BT link is working because otherwise the program would not have reached the openLock() function. Do you see anything when you print the code variable in the function ?

I have just noticed that secret is a long whereas code is an int. The comparison between them will probably still work but they really ought to be of the same type.

ya i saw the what is your password prompt got go in the function
but dunno why it wont let me get a new input and even i type what number also didnot get reponse

who can help me solve the problem

UKHeliBob:
Do you see the "What is your password" prompt ?
If so then you know that the BT link is working because otherwise the program would not have reached the openLock() function. Do you see anything when you print the code variable in the function ?

I have just noticed that secret is a long whereas code is an int. The comparison between them will probably still work but they really ought to be of the same type.

i still cannot find any solution to let me input the password

Sorry, but I do not have any Bluetooth hardware to test this with.

UKHeliBob:
Sorry, but I do not have any Bluetooth hardware to test this with.

then nvm... hope got other people can help me solve

or u have other way to help me solve this problem? xD