This code does all I want ecept for one thing

#include <SoftwareSerial.h>


SoftwareSerial BTSerial(0, 1); // RX, TX


void setup() {

  pinMode(13, OUTPUT);

  digitalWrite(13, LOW);  

  BTSerial.begin(9600);

  Serial.begin(9600);

}


void loop() {

  if (BTSerial.available()) {

    char data = BTSerial.read();

    if (data == 'N') {

      digitalWrite(13, HIGH);

      delay(500);

      digitalWrite(13, LOW);

      Serial.println("LED OFF");

    } else if (data == 'F') {

      digitalWrite(13, HIGH);

      delay(500);

      digitalWrite(13, LOW);

      Serial.println("LED ON");

    }

  }

}


The problem is when I connect the phone via bt, the led start lighting.
How can i avoid this?

Start by not using SoftwareSerial on pins 0 and 1 if you're using an Uno, Nano, ProMini or Mega. Pins 0 and 1 are the HardwareSerial pins used for communication with the PC.

If you're using another board or a Mega, don't use SoftwareSerial.

1 Like

But the rx and tx pins are 0 and 1 on the uno board (on my board it says UNO infiduino r3)

try the SoftwareSeial library with pins 2 and 3 for TX and RX. Also write the same values to your Serial monitor so you can seen whats being send. And pins 0 and 1 are already your TX and RX so the software serial wouldn't be usefull with what you are doing now.

I must inform you that I'm a complete neebie on this, so I dont understand what you are saying.

If I change the BT serial to 2 and 3 the shield no longer have a connection to the main board, and nothing happens

What shield? Is it not possible to make the connections over different pins?

And what Arduino boad?

I know how shields work, just seems odd to force use of pins 0 and 1, what are normally left unused for anything but loading code and the serial monitor.

a7

Which one of the following three boards you are using?

NANOPic

megaPic


Im usind the std arduinoboard, the first picture, and a velleman wpsh338 HM-10 shield

Comment all reference to Serial and just use BTSerial and it should work.

Your problem is you have 2 jumpers at the lower section of the shield labeled tx and rx. The labels are on the underside of the board so you would need to remove the shield to see them. Serial is conflicting with BTSerial.

Those jumpers need to be moved to 2 and 3 in order for you to use
BTSerial.begin(9600); // Software Serial
Serial.begin(9600); // Hardware Serial to the monitor
in the same sketch.

This needs to change from (0, 1) to (2, 3)

The manual and schematic can be found here
https://www.velleman.eu/products/view?id=450566

Someone needs to verify what I say above. I can't determine where TX1 and RX1 is connected on the shield from the schematic. They don't appear to connect to pins 2 and 3

The User Manual is half helpful

2 jumpers allow to select D0 and D1 or
D2 and D3 as serial communication port.

No other mention of the alleged jumpers and no picture. My assumption is that it must be documented on the board and obvious once someone points it out.

a7

1 Like

It's on the back of the board:
image

1 Like

I moved jumpers til 2 and 3, changed it in the code.
It now works as before, meaning the diode is lighting ut when bt is connected.

So I'm back to square 1

Are you sending using the IDE serial monitor? Check your line ending setting. It should be set to "off".

Here I'm using
SoftwareSerial BTSerial(2,3); // RX, TX

'N' turns it On, 'F' turns it Off (anything else results naught).

SoftwareSerial BTSerial(2,3); // RX, TX

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);  
  BTSerial.begin(9600);
  Serial.begin(9600);
}

void loop() 
{
  if (BTSerial.available()) 
  {
    char data = BTSerial.read();
    if (data == 'N') 
    {
      digitalWrite(13, HIGH);
      Serial.println("LED On");
    }
    
    if (data == 'F') 
    {
      digitalWrite(13, LOW);
      Serial.println("LED Off");
    }
  }
}

From examining the attached schematic, it doesn't appear that pins 2 & 3 of the jumper header (RX1 and TX1) are connected to anything. Verify continuity from these header pins to the Arduino header.

Keep in mind the above line will light the Uno LED, not the shield LED. The shield LED will flash when the BLE module it transmitting or receiving data.

Let's Back Up:
Follow the example on page 4 & 5. Make sure the power switch is OFF when you upload the code. Put the jumpers back in their original location and power switch ON after a successful upload.

What happens?

That code does not compile.

Try the example in the manual page 4

Do we need to include the following line at the top of your program of post #14?

#include<SoftwareSerial.h>

Of course.
(OMG)

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2,3); // RX, TX

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);  
  BTSerial.begin(9600);
  Serial.begin(9600);
}

void loop() 
{
  if (BTSerial.available()) 
  {
    char data = BTSerial.read();
    if (data == 'N') 
    {
      digitalWrite(13, HIGH);
      Serial.println("LED On");
    }
    
    if (data == 'F') 
    {
      digitalWrite(13, LOW);
      Serial.println("LED Off");
    }
  }
}
1 Like