Hey everyone. Im really new to this so I will do my best to be clear. I have been doing research for the last week and can't quite seem to understand just how to code this.
I am trying to get a light to turn on when connected to the bluetooth on my phone and turn off when the bluetooth disconnects. I have coded a app that allows for the light to turn on...but it is the turn off that is not working.
I have a Arduino Pro Mini...a HC-05...and a single 5vdc relay.
The sketch that I have been working with is as follows:
void loop()
{
if (Serial.available())
{
int value = Serial.read();
if (value == '1') digitalWrite(PIN, HIGH);
else if (value == '0') digitalWrite(PIN, LOW);
}
}
So far the light will just turn on...until I manually turn it off.
How do I sketch it so that the arduino turns off the relay when the phone is no longer connect to the bluetooth? So bluetooth from phone connects to bluetooth HC-05 ...light turns on...phone bluetooth disconnects from HC-05... light turns off.
if (Serial.available())
If Serial is not available, you will never enter the segment that turns the LED off. You will turn the LED on if you get a character in the receive buffer and then forever skip the test when the receive buffer is empty. You need a different test.
How would you know that no one was ever going to reply to your post? When no one is ever going to reply to your post, you want to give your Arduino to someone else. When do you do that?
You have NO idea why the other device is not sending data. It could be because it can't, because it is out of range. It could be because the other device was turned off. It COULD be because the other device wants the damned lights to stay on.
What you have to do is decide just how long you will wait for the other device to tell you to turn the lights on or off. If that amount of time passes with no communication from the other device, ignore the fact that it wants the lights on, and turn the lights off.
Of course, this means that you need to record WHEN you hear from the other device...
DKWatson:
if (Serial.available())
If Serial is not available, you will never enter the segment that turns the LED off. You will turn the LED on if you get a character in the receive buffer and then forever skip the test when the receive buffer is empty. You need a different test.
Tristan55:
I don't understand your response in reply 2.
What don't you understand?
There are many reasons why data might stop coming in. You need to decide when to turn the light off. Describe exactly what will cause you to decide that the light should be turned off.
There are many reasons why data might stop coming in. You need to decide when to turn the light off. Describe exactly what will cause you to decide that the light should be turned off.
I want it so that when the bluetooth from my phone is no longer connected to the HC-05 to then turn off the light.
So is it possible to have the Arduino board recognize that there is no longer a connection between the HC-05 and the my phone?
PaulS:
I got that the first time. The Arduino has NO way of knowing that the device on the other end of the bluetooth connection has gone away. Get over it.
Ok. Now I get it.
So what your saying is put a delay with a time frame to turn it off (like after 5 mins turn off light).
Tristan55:
I am trying to get a light to turn on when connected to the bluetooth on my phone and turn off when the bluetooth disconnects. I have coded a app that allows for the light to turn on...but it is the turn off that is not working.
PaulS:
How would you know that no one was ever going to reply to your post? When no one is ever going to reply to your post, you want to give your Arduino to someone else. When do you do that?
You have NO idea why the other device is not sending data. It could be because it can't, because it is out of range. It could be because the other device was turned off. It COULD be because the other device wants the damned lights to stay on.
What you have to do is decide just how long you will wait for the other device to tell you to turn the lights on or off. If that amount of time passes with no communication from the other device, ignore the fact that it wants the lights on, and turn the lights off.
Of course, this means that you need to record WHEN you hear from the other device...
Tristan55:
I don't understand your response in reply 2.
Maybe a translation will help you to understand. Let me try to translate.
"The Arduino has no way of knowing that you have disconnected from Bluetooth. Therefore, it is impossible for your project to function exactly the way you want it to function.
In your case, the best that the Arduino can do is to check whether or not it is still receiving data from your phone. If it is still receiving data, then it knows for sure that you are still connected to Bluetooth. However, if it is not still receiving data, that could be for any number of reasons. Maybe your phone has moved out of range, and so the Arduino can't "hear" it. Maybe your app has no more data to send, leaving the Arduino with nothing to receive.
You cannot know why the Arduino is not receiving data from your phone. What you have to do is decide exactly how long the Arduino should wait before it gives up and turns off the lights. In order to do this, the Arduino will need to keep track of when it last received data from your phone."
So what your saying is put a delay with a time frame to turn it off (like after 5 mins turn off light).
I'm not saying that, at all. If you put delay() in your code, you will do NOTHING while the delay() is happening.
Look at the blink without delay example, to learn how to do something some fixed length of time after something else, without needing to use delay().
The something else, in your case, is receiving serial data (or, rather, discovering that there is serial data to read). The something is turning off the light/relay.
odometer:
Maybe a translation will help you to understand. Let me try to translate.
"The Arduino has no way of knowing that you have disconnected from Bluetooth. Therefore, it is impossible for your project to function exactly the way you want it to function.
In your case, the best that the Arduino can do is to check whether or not it is still receiving data from your phone. If it is still receiving data, then it knows for sure that you are still connected to Bluetooth. However, if it is not still receiving data, that could be for any number of reasons. Maybe your phone has moved out of range, and so the Arduino can't "hear" it. Maybe your app has no more data to send, leaving the Arduino with nothing to receive.
You cannot know why the Arduino is not receiving data from your phone. What you have to do is decide exactly how long the Arduino should wait before it gives up and turns off the lights. In order to do this, the Arduino will need to keep track of when it last received data from your phone."
Got ya.
So how do I tell the arduino that since it is no longer receiving signal turn off relay(code the app to send the signal while the bluetooth is connected then when the app stops sending the signal cause it is disconnected from the bluetooth the arduino then would revert back to off)? Is that possible?
So how do I tell the arduino that since it is no longer receiving signal turn off relay(code the app to send the signal while the bluetooth is connected then when the app stops sending the signal cause it is disconnected from the bluetooth the arduino then would revert back to off)? Is that possible?
in the code I posted, you can turn the relay (LEDpin) on by sending '1' and off by sending '0', and if nothing is received for 3000 milliseconds, it turns off the relay (LEDpin). You can change the timeOut interval to 300000UL for 5 minutes.
Perehama:
in the code I posted, you can turn the relay (LEDpin) on by sending '1' and off by sending '0', and if nothing is received for 3000 milliseconds, it turns off the relay (LEDpin). You can change the timeOut interval to 300000UL for 5 minutes.
AHHHHH. Ok. Like I said Im new and trying to understand in plain english what things in the code mean. Thank you for that explanation.
Tristan55:
AHHHHH. Ok. Like I said Im new and trying to understand in plain english what things in the code mean. Thank you for that explanation.
"plain English" isn't the problem. If "connected" meant what you think it means, you wouldn't have started this thread. In plain English, "connected" means you will need to send '1' periodically, or the relay will turn off.
Perehama:
"plain English" isn't the problem. If "connected" meant what you think it means, you wouldn't have started this thread. In plain English, "connected" means you will need to send '1' periodically, or the relay will turn off.
True True.
Code works (obviously). Thank you for that!!! But in the line timeOutInterval = 3000UL...what does the UL stand for?