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
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.
//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
}
}
}
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);
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?
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
}
}
}
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.
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!