How to control led using bluetooth app

Hi friend
i want to control the led by using bt serial interface app, i am trying to turn on an led after receiving a character for 10 second and i want to turn off automatically after 10 second. But here in this code the led is turn on first before sending character and it turns off. But after sending a character it vl turn on but it wont turn off after 10 second. Please help me

char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)

void setup()
{
Serial.begin(9600); //baud rate intialization at 9600
pinMode(13,OUTPUT);//pin 13 ouput for led
}

void loop()
{
if(Serial.available())//if there is data being recieved
{
blueToothVal=Serial.read(); //read it
}
if (blueToothVal=='n')//if value from bluetooth serial is n
{
digitalWrite(13,HIGH); //switch on LED
if (lastValue!='n')

Serial.println(F("LED is on")); //print LED is on
lastValue=blueToothVal;
delay(10000); //on for 10 second

digitalWrite(13,LOW);
Serial.println(F("LED is oFF")); //print LED is off

}
}

Every if statement should be followed by curly braces, so the block of code to be executed is clear.

Are you trying to use THE serial port to talk to your phone OR to your PC? Both is the wrong answer. So is yes.

Thanks for reply
i am trying to receive character serially. So the led is not turning off.I am trying so much to turnoff the led.It receives only one time and later it is not responding to my app.

not tested, but more like this:

//char blueToothVal;           //value sent over via bluetooth
//char lastValue;              //stores last state of device (on/off)
unsigned long timerStart = 0;


void setup()
{
  Serial.begin(9600); //baud rate intialization at 9600
  pinMode(13, OUTPUT); //pin 13 ouput for led
}

void loop()
{
  char blueToothVal;
  if (Serial.available()) //if there is data being recieved
  {
    blueToothVal = Serial.read(); //read it
  }
  if (blueToothVal == 'n') //if value from bluetooth serial is n
  {
    digitalWrite(13, HIGH);           //switch on LED
    timerStart = millis();
    Serial.println(F("LED is on")); //print LED is on
  }
  if (millis() - timerStart > 10000UL)
  {
    if(digitalRead(13))
    {
      digitalWrite(13, LOW);
      Serial.println(F("LED is off")); //print LED is off
    }
  }
}

Thank you so much now it is working. I am actually new to arduino.What is the fault in my code

Can u explain this part of the code how does the milli() get incremented.
timerStart = millis();//here you assigned millis
Serial.println(F("LED is on")); //print LED is on
}
if (millis() - timerStart > 10000UL)//this part i am not getting
{
if(digitalRead(13)) //it reads the current status of the pin
{
digitalWrite(13, LOW);

timerStart = millis();//here you assigned millis

millis() is a function. It can not be assigned anything. The value that millis() returned was assigned to timerStart (which is a lousy name, as no timers are involved).

if (millis() - timerStart > 10000UL)//this part i am not getting

Why not? The statement subtracts then (some time in the past) from now, to get the interval since the event of interest. It compares that to 10000. If it has been more than 10 seconds since the event of interest, do the block of code in the curly braces. Pretty simple, really.

Thanks but the problem is the led is turning ON once it is get connected to Bt serial interface without sending an character first time.Why it is happening?

Why it is happening?

Something in the code makes it happen. Your code. The code you didn't post.

This is the code
unsigned long timerStart = 0;

void setup()
{
Serial.begin(9600); //baud rate intialization at 9600
pinMode(13, OUTPUT); //pin 13 ouput for led
}

void loop()
{
char blueToothVal;
if (Serial.available()) //if there is data being recieved
{
blueToothVal = Serial.read(); //read it
}
if (blueToothVal == 'n') //if value from bluetooth serial is n
{
digitalWrite(13, HIGH); //switch on LED
timerStart = millis();
Serial.println(F("LED is on")); //print LED is on
}
if (millis() - timerStart > 10000UL)
{
if(digitalRead(13))
{
digitalWrite(13, LOW);
Serial.println(F("LED is off")); //print LED is off
}
}
}

char blueToothVal;

You forgot something - an initial value. As this statement is written, you have no clue what is in blueToothVal.

char blueToothVal;
is used to receive a character right. So it stores the received characters and compares. But led is on before sending an character.

So it stores the received characters and compares. But led is on before sending an character.

IF there is data to be read (99.99999999999% of the time there will NOT be), then the data that was read is stored in that uninitialized variable, giving the memory location some known data to store.

Until there is something to read, you have NO clue what is in that memory location. Do NOT do comparisons using memory locations that you do not know what they contain.

PaulS:
(which is a lousy name, as no timers are involved).

Hey there… we created a 10second millis() timer!

one may argue that Paul is a lousy name (not that I am) :wink:

yes, OP… initialize the char…

char blueToothVal = 0;  //<< right from ASCII table

or 

char blueToothVal = '\0';        //<< char literal

or

char blueToothVal = NULL;     //<< Keyword

all will initialize the char with the ASCII null

thanks even though it is not working even after initializing the char bluetoothval.

rahulsubbu:
thanks even though it is not working even after initializing the char bluetoothval.

Well then... Add in some Serial.print(somethingOfInterest) to see what's happening.

What does your serial output look like when you send an 'n'?

Go back to the very first lot of code you posted. THE LAMP DOES TURN OFF. It's just that it turns no again after a few micro seconds. Read your own code work it through using pen and paper. But most of all auto format the code first!

Mark