Hi
Hello, I have this problem, namely I have written a program in which the red LED lights up immediately after the microcontroller (Arduino UNO R3) starts up. Then, when the assigned button '1' is pressed, the red LED goes off, and in turn, the green and yellow LEDs come on. Then, after '2' is pressed, the system works the other way round, i.e. the red diode goes on and the green and yellow ones off. The problem with all this is that I'm not able to do such a function/step where after breaking (for example sudden) the bluetooth connection (module with phone), the red diode should turn on again. I'm using HC-06 bluetooth module and I'm also sending the contents of my program.
Please for help and thanks
//Declaration
int bt; //Bluetooth
int L1=2; //Green
int L2=3; //Red
int L3=4; //Yellow
void setup() { //Start position
pinMode(L1,OUTPUT);
pinMode(L2,OUTPUT);
digitalWrite(L2,HIGH); //Red LED ON
pinMode(L3,OUTPUT);
Serial.begin(9600);
}
if (bt == '1') {
digitalWrite(2,HIGH); //Green ON
digitalWrite(3,LOW); //Red OFF
digitalWrite(4,HIGH);} //Yellow ON
else if (bt == '2'){
digitalWrite(L1,LOW);}
if (bt == '2') {
digitalWrite(2,LOW); //Green OFF
digitalWrite(3,HIGH); //Red ON
digitalWrite(4,LOW);} //Yellow OFF
else if (bt == '1'){
digitalWrite(L2,LOW);}
}
Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).
is there a constant heartbeat coming from the BT connexion? you won't be able to easily detect the BT disconnect with an HC-06
why do you perform the test against btif you have not read anything new?
Not exacly it's what I need. I want t have a situation when I have green and yellow LED on and when that I have urgent disconect commucation bluetooth with my phone I want have red LED on and green and yellow off.
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
paste clipboard into write-window of a posting
You should use constants for IO-pins.
using hardcoded numbers is bad programming style
here is a modified codeversion that uses constants to show it
and I have added serial output to show what your variable "bt" contains.
by the way: You are using the standard serial connection which is used by the arduino-IDE to upload your program. Do you you disconnect your computer and connect your bluetooth-module each time you test the program?
Or are you in the stage of simulating the bluetooth-connection with the serial monitor of the arduino-IDE?
//Declaration
int bt; //Bluetooth
const byte greenLED_Pin = 2; // no comment needed the constants name says it all
const byte redLED_Pin = 3; // no comment needed the constants name says it all
const byte yellowLED_Pin= 4; // no comment needed the constants name says it all
void setup() { //Start position
Serial.begin(9600); // first thing to do in setup start serial interface
Serial.println( F("Setup-Start") );
pinMode(greenLED_Pin, OUTPUT);
pinMode(redLED_Pin, OUTPUT);
digitalWrite(redLED_Pin, HIGH); // no comment needed the constants name says it all
pinMode(yellowLED_Pin, OUTPUT);
Serial.println( F("Setup done") );
}
void loop() {
if (Serial.available() > 0) {
bt = Serial.read();
}
Serial.print( F("bt contains#") );
Serial.print(bt);
Serial.print( F("#") );
delay(500); // slow down serial output
if (bt == '1') {
digitalWrite(greenLED_Pin, HIGH); // no comment needed the constants name says it all
digitalWrite(redLED_Pin, LOW); // no comment needed the constants name says it all
digitalWrite(yellowLED_Pin, HIGH); // no comment needed the constants name says it all
}
else if (bt == '2') {
digitalWrite(greenLED_Pin, LOW);
}
if (bt == '2') {
digitalWrite(greenLED_Pin, LOW);
digitalWrite(redLED_Pin, HIGH);
digitalWrite(yellowLED_Pin, LOW);
}
else if (bt == '1') {
digitalWrite(greenLED_Pin, LOW);
}
}
I'm trying to say in my own words what I assume I have understood:
If your smartphone disconnects from your bluetooth-module that is wired with the Arduino the green and yellow LED shall be switched of and the red LED shall be switched ON?
Is this correct?
anyway what type of bluetooth-module are you using?
name the exact type of the module and provide a link where the datasheet of this bluetooth-module can be downloaded