serial bluetooth

Hi, I found this code in the examples...

What I basically need to do is accept incoming data from bluetooth serial port which is terminated by "/n". So I thought this would be a good starting point.

I uploaded this code to my arduino, and paired my bluetooth phone with the bluetooth module connected to the arduino.

Do I also need to define the bluetooth serial pins (Rx,Tx). If they are define in the sketch, does that mean I've got a 2nd com port?

I'm getting a bit confused with bluetooth and serial ports. I'd like to send data using bluetooth serial com, and then check com 4 (usb connected com port I used to upload the sketch) to view my results.

/*
  Serial Event example
 
 When new serial data arrives, this sketch adds it to a String.
 When a newline is received, the loop prints the string and 
 clears it.
 
 A good test for this is to try it with a GPS receiver 
 that sends out NMEA 0183 sentences. 
 
 Created 9 May 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SerialEvent
 
 */

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

merlin2049er:
I uploaded this code to my arduino, and paired my bluetooth phone with the bluetooth module connected to the arduino.

Do I also need to define the bluetooth serial pins (Rx,Tx). If they are define in the sketch, does that mean I've got a 2nd com port?

/*

Serial Event example

SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.

This project uses hardware serial, and you don't need to specify anything. Specifying pins won't win you any prizes. If you want to use extra serial ports, you can do so by using the software serial library, which enables you to use certain other pins as well as the regulation hardware serial pins.

You have already succeeded in pairing between bluetooth and phone. If you have not already done so, all you need do now is correctly connect bluetooth to Arduino hardware serial pins, A4,A5 on Uno, or D0,D1 on Mega

On all the boards I've worked with, the Serial object is "bound" to a certain pair of rx and tx pins. Some boards do indeed have multiple serial ports, but those boards have multiple objects(Serial1, Serial2, etc), each bound to a specific set of pins.

Serial communication is so popular that it's handled by hardware in the chip, and in general, on low end microcontroller, each hardware module for serial stuff is bound to a specific set of pins.

Various serial port info, most of which you probably already know, but included anyway for completeness.

Some chips let you actually select what pins connect to the UART, but the chip in an arduino uno is not one of those to my knowledge, and if it is, the arduino library doesn't have a way to manually change the setting and you would need to do some low level register writing type of code. Pretty sure the atmega328 just plain doesn't have that feature though.

The arduino uno has only one serial port though.

You can use software serial to create fake serial ports, manging dataflow with clever code instead of special hardware. You could hook the module up to the virtual serial port and use the real one for talking to the PC. Soft serial is included in the library. For this I think you do need to define what pins to attach the virtual software port to, because you actually do have a choice.

The name COM4 is purely on the computer side. The arduino board contains a USB to serial chip that interfaces between the computer and the arduino. When you plug the USB cable in, Windows creates a virtual serial port, which appears to software like as if it were an old style rs232 serial port. It assigns the virtual port a name, something like COM1, COM4, COM752, etc.

The Leonardo has the usb to serial built into the chip, and it has a normal serial port as well, giving two ports, and the mega has a ton of serial ports.

Alternatively, if you aren't going to send anything to the Bluetooth, and you aren't going to receive anything from the computer, you can use the RX pin for the Bluetooth module, and use the TX pin for the computer. You would be splitting the two hacks of one port into two one way ports.

Ok, I got my bluetooth module hooked up to pins 14 & 15 (which I found out was serial3).

So, I can change the code to look for serial input on com 3. I'd like to print the output on com4 (virtual usb that I'm monitoring)

merlin2049er:
Ok, I got my bluetooth module hooked up to pins 14 & 15 (which I found out was serial3).

Does it work? If not, read reply #1 again. If you must put bluetooth on serial3, you need to change all your serial commands from Serial to Serial(3) .

So, I can change the code to look for serial input on com 3. I'd like to print the output on com4 (virtual usb that I'm monitoring)

You cannot cannot use the serial monitor to receive bluetooth. You need to use a proper terminal programme, like RealTerm, properly set up and connected to the bluetooth on your PC. The COM port on your PC is determined by the PC, not Arduino, and is usually around COM40.