Arduino - bluetootth

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);
}

void loop() {
if(Serial.available() > 0){
bt = Serial.read();}

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?

void loop() {

    if (bt == '1') {
        ...
    } //Yellow ON

    else if (bt == '2'){
        ...
    }

    if (bt == '2') {
        ...
    } //Yellow OFF

    else if (bt == '1'){
        ...
    }
}

this code has duplicate conditions for each case.

I don't understand. What/Where I must add to correct work my project ?

  • start with fixing post #1 (after reading the guidance on how to use the forum).
  • answer questions in #2

looks like you want the LEDs to be set differently for each value of bt.

is this what you want?

void loop() {
    if(Serial.available() > 0){
        bt = Serial.read();
    }

    if (bt == '1') {
        digitalWrite(2,HIGH); //Green ON
        digitalWrite(3,LOW); //Red OFF
        digitalWrite(4,HIGH);
    } //Yellow ON

    else if (bt == '2'){
        digitalWrite(2,LOW); //Green OFF
        digitalWrite(3,HIGH); //Red ON
        digitalWrite(4,LOW);
    } //Yellow OFF
}

post code using </> (it's a secret)

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

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. 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);
  }
}

best regards Stefan

could you please fix post #1

how do you detect this?

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

best regards Stefan

#1I was reading the forum. #2 I want a situation when I have a red led on, when bt is disconect with my phne after urgent lose a signal with bt.

I don't understand what you want to say with that.
If english is not your native language use google-translate
best regards Stefan

How?

How what?
Do you want to play the "posting single-word-Ping-Pong-game" ? or do you want efficient help?
best regards Stefan

Yes I'm using the standard serial connection which is used by the arduino-IDE to upload your program. And I'm using a program download from googleplay

I'ts the answer to person who asked me, can i fix sth in my post.

Again write understandable details: post with which number? Name of the person of this post?

I like and enjoy to help as you can see from writing the modified code.
But only if a certain condition is fullfilled:

Writing in detail what your setup is and what you tried and always a full sketch posted as code-section

What's the name of the Android-app?
What functionalities does the app have?

If you don't fullfill this requirement I will just post a link to the general description how to get the best from this forum

best regards Stefan

I'm out of here. OP does not want to fix post #1 and include code tags. I see that as a lack of respect.

bye, good luck.

speeding up finishing your project and do yourself a favor by reading
How to get the best out of this forum