Test data received from bluetooth to turn on LED

Hi there

I need help to solve this issue please.

I try to turn on/off led using android /arduino.

I can get Data from Phone, I can see 1 and 2 on Bee's monitor but my led LED does'nt work (led to IN 13), Bluetooth work good . could you tell me why
the code is

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

int ledPin = 13;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
 if (Serial.available()){
  mySerial.begin(9600);
  Serial.begin(9600);
 }
}

void loop()
{
   
  // listen for new serial coming in:
  
  // print out the character:
 if (mySerial.available()>0){
 char someChar = mySerial.read();
     if( someChar=='1'){
     digitalWrite(ledPin, HIGH);
      }
    if( someChar=='2'){
     digitalWrite(ledPin, LOW);
      }
   
 mySerial.print(someChar);
  //print to Serial Monitor
 Serial.print(someChar);

  }
 
}

When I press button on mobile phone, I see 1 and 2 on monitor but led doesn't work.
is there another why to do it?

if (Serial.available()){
mySerial.begin(9600);
Serial.begin(9600);
}

How is Serial.available() supposed to return anything if Serial.begin() is not set up first? Same goes for mySerial.begin().

Also why bother with a if statement there?

I have to put it that way or like this:

if (mySerial.available()){
  mySerial.begin(9600);
  Serial.begin(9600);
 }

otherwise my monitors Bee stop to respond.

But in both codes, I can get bluetooth's data, but LED won't to work

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

int ledPin = 13;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  // listen for new serial coming in:

  // print out the character:
 if (mySerial.available()>0)
 {
   char someChar = mySerial.read();
   if( someChar=='1')
   {
     digitalWrite(ledPin, HIGH);
   }
   if( someChar=='2')
   {
     digitalWrite(ledPin, LOW);
   }

   mySerial.print(someChar);
  //print to Serial Monitor
   Serial.print(someChar);
  }
 
}

Try this code and tell me what the result is.

Edit: Dont use the copy to forum option.

Thanks but it won't work. in addition I cant monitor data sent by bluetooth

Ok, post the hardware you are using and how it is wired up. Maybe something is backwards. Do you have the TX(Bluetooth) -> RX(Arduino) and RX(Bluetooth) -> TX(Arduino) ?

If the bluetooth module is paired to the phone, then you should see something. Issues can be wiring or incorrect baud rate.

Yes

RX to TX and TX to TX, GND and 5V, I can connect to Bluetooth and receive DATA from my PHONE. LED in PIN 13 and GND.

The only problem is the test

if( someChar=='1')

Even if monitor print '1' when I push button on my phone, LED won't turning on

even if I test this code, I can see on monitor OK, or NO but I can get 1 and 2

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

int ledPin = 13;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
if (mySerial.available()){
  mySerial.begin(9600);
  Serial.begin(9600);
}
}

void loop()
{
  // listen for new serial coming in:
  // print out the character:
   char someChar = mySerial.read();
   if (mySerial.available()){
   if( someChar=='1')
   {
     Serial.print('OK');
   }
   if( someChar=='2')
   {
        Serial.print('NO');
   }
   
   mySerial.print(someChar);
  //print to Serial Monitor
   Serial.print(someChar);
   }
 
}

So you DO see OK and NO when you press 1 or 2?

Then maybe your LED is backwards. Why are you using an external LED anyway? Your arduino has a built-in LED already connected to pin 13. it is actually right next to pin 13 on the board. Does it ever go on or off?

Add this to the end of your setup()
digitalWrite(ledPin, HIGH);

No , I DO NOT see OK or NO when I press 1 or 2.

The internet LED don't work too.

Is that any thing wrong with my test?

it is like the if if( someChar=.... doesn't exist

Post a link to your BT module, and the app you are using to send the commands.

I can connect to Bluetooth and receive DATA from my PHONE

Clearly you are unable to. It is either the app or the hardware. I need to know both you are using.

Added:

Does the SoftwareSerial example code work?

/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

I post mu my Android code, but everything work well, I connect to bluetooth and I send data without problem. Then I am using Arduino UNO so RX, TX is 2 and 3 it is alos Ok.

here my code

public class ClientSocketActivity  extends Activity
{
 .
.
.
.
.
 
 public void onToggleClicked(View view) throws IOException {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();
    if(view ==lampe1) {
    if (on) {
        // Enable vibrate
     String tmpStr ="1";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Lampe1 turned on" ,
           Toast.LENGTH_LONG).show();
    } else {
        // Disable vibrate
     String tmpStr ="2";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Lampe1 turned oFF" ,
           Toast.LENGTH_LONG).show();
    }
    }
    
    if(view ==lampe2) {
    if (on) {
        // Enable vibrate
     String tmpStr ="l2";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Lampe2 turned on" ,
           Toast.LENGTH_LONG).show();
    } else {
        // Disable vibrate
     String tmpStr ="l22";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Lampe2 turned oFF" ,
           Toast.LENGTH_LONG).show();
    }
    }
   
    if(view ==appareil1) {
    if (on) {
        // Enable vibrate
     Toast.makeText(getApplicationContext(),"Appareil 1 turned on" ,
           Toast.LENGTH_LONG).show();
     String tmpStr ="a1";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
    } else {
        // Disable vibrate
     Toast.makeText(getApplicationContext(),"Appareil 1 turned oFF" ,
           Toast.LENGTH_LONG).show();
     String tmpStr ="a12";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
    }
    }
    if(view ==appareil2) {
    if (on) {
        // Enable vibrate
     String tmpStr ="a2";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Appareil 2 turned on" ,
           Toast.LENGTH_LONG).show();
    } else {
        // Disable vibrate
   String tmpStr ="a22";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Appareil 2 turned oFF" ,
           Toast.LENGTH_LONG).show();
    }
    }
    
    if(view ==smartvigil) {
    if (on) {
        // Enable vibrate
   String tmpStr ="s1";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Smart vigil turned on" ,
           Toast.LENGTH_LONG).show();
    } else {
        // Disable vibrate
   String tmpStr ="s12";
 byte bytes[] = tmpStr.getBytes();
 outputStream.write(bytes);
     Toast.makeText(getApplicationContext(),"Smart vigil turned oFF" ,
           Toast.LENGTH_LONG).show();
    }
    }
}
 
 /* after select, connect to device */
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode != REQUEST_DISCOVERY) {
 finish();
 return;
 }
 if (resultCode != RESULT_OK) {
 finish();
 return;
 }
 final BluetoothDevice device = data.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 new Thread() {
 public void run() {
 connect(device);
 };
 }.start();
 }
 
 protected void onDestroy() {
 super.onDestroy();
 try {
 socket.close();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 Log.e("EF-BTBee", ">>", e);
 }
 }
 
 protected void connect(BluetoothDevice device) {
 //BluetoothSocket socket = null;
 try {
 //Create a Socket connection: need the server's UUID number of registered
 socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
 
 socket.connect();
 Log.d("EF-BTBee", ">>Client connectted");
 
 inputStream = socket.getInputStream(); 
 outputStream = socket.getOutputStream();
 int read = -1;
 final byte[] bytes = new byte[2048];
 for (; (read = inputStream.read(bytes)) > -1;) {
 final int count = read;
 _handler.post(new Runnable() {
 public void run() {
 StringBuilder b = new StringBuilder();
 for (int i = 0; i < count; ++i) {
 String s = Integer.toString(bytes[i]);
 b.append(s);
 b.append(",");
 }
 String s = b.toString();
 String[] chars = s.split(",");
 sbu = new StringBuffer();  
 for (int i = 0; i < chars.length; i++) {  
        sbu.append((char) Integer.parseInt(chars[i]));  
    }
 Log.d("EF-BTBee", ">>inputStream");
 if(str != null)
 { 
 sTextView.setText(str + "<-- " + sbu);
 str += ("<-- " + sbu.toString());
 }
 else
 {
 sTextView.setText("<-- " + sbu);
 str = "<-- " + sbu.toString();
 }
 str += '\n';
 }
 }); 
 }
 
 } catch (IOException e) {
 Log.e("EF-BTBee", ">>", e);
 finish();
 return ;
 } finally {
 if (socket != null) {
 try {
 Log.d("EF-BTBee", ">>Client Socket Close");
 socket.close(); 
 finish();
 return ;
 } catch (IOException e) {
 Log.e("EF-BTBee", ">>", e);
 }
 }
 }
 }
}

arduino

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

int ledPin = 13;
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
  // define pin modes for tx, rx, led pins:[color=#222222][/color]
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
if (mySerial.available()){
  mySerial.begin(9600);
 Serial.begin(9600);
}
}

void loop()
{
  char someChar = 0;
  // listen for new serial coming in:
  // print out the character:
   
   if (mySerial.available()>0){
     someChar = mySerial.read();
   if( someChar=='1')
   {
     digitalWrite(ledPin, HIGH);
      mySerial.println('OK');
   }
    if( someChar='1')
   {
     digitalWrite(ledPin, HIGH);
     mySerial.print('OK');
   }
   if( someChar=='2')
   {
     digitalWrite(ledPin, LOW);
     mySerial.print('OK');
   }
   
   mySerial.println(someChar);
  //print to Serial Monitor
  Serial.print(someChar);
  
   }
}

I can get the same data I want to get from Android, but my (if test ) won't to work, what if I use case?

Test your app with the SoftwareSerial example sketch. What is your BT module's default baud rate? 9600 or 38400? And does the module have a status connection LED?

Post a link to your BT module.

Bluetooth is an HC-O6 with LED, and everything is good from this side.

I think that my problem is inside the loop , no?

Your original code is faulty, but the modified version that I gave you, is fine. The issue is not in the software unless it is using the wrong baud rate. Right now you are using 9600, maybe it is set to use 38400.

If the LED on the BT module is SOLID then it is connected. If it is connected then you need to play around with the different baud rates or contact the seller and ask what the baud rate is.

Did you write that app yourself? Maybe the app is wrong somewhere. It has been a long while since I wrote anything in Java, so I'll take a look at it later when I get home.

Yeah, Led of Bluetooth shield is solid, it is well connected.

I modified an open source Code, It work without problem. I have problems In the Arduino side

Hello,

What if you try this:

const uint8_t ledPin = 13;
bool ledState = false;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
  delay(1000);
}

Does your led blink every second ?

So it looks like it is the baud rate. Try changing it to 38400, 57600 or 115200

Hi,

Your test work, and LED blink ? changed baud as you said but it won't work.
How to change CHAR to STRING?, maybe this is

Thanks for your help, I solved it
here is code

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

int ledPin = 8; 
int state = 0;


void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

 pinMode(rxPin, INPUT);
 pinMode(txPin, OUTPUT);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
 
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() {
  
if(mySerial.available() > 0){
state = mySerial.read();

}

if (state == '2') {
digitalWrite(ledPin, LOW);
Serial.println("LED: off");

}


if (state == '1') {
digitalWrite(ledPin, HIGH);
Serial.println("LED: on");
}
}

You didn't really change much from what I gave you. Perhaps it was the character coming in from the BT module, it was a char and now it is an int.