0

I am currently working on an Arduino project where the Arduino will send data to a processing program via bluetooth and processing will draw the data. However, my code doesn't seem to work.

Here is my Arduino code:

#include <SoftwareSerial.h>

int bluetoothTx = 1;
int bluetoothRx = 0;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  bluetooth.begin(9600);
}

void loop() {    
  Serial.println("Hello");
}

And here is my Processing code:

import processing.serial.*;
Serial myPort;

void setup() {
  myPort = new Serial(this, "COM42", 9600);
  myPort.bufferUntil('\n');
}

void serialEvent( Serial myPort) {
  String data = myPort.readStringUntil('\n');
  println(data);
}

I identified that the incoming port from the HC-05 module is "COM42" from my Bluetooth settings. I am expecting to see several "Hello" lines being printed on the processing console but this does not happen

1

Your whole sending program is a complete mess, sorry to say:

First you create a SoftwareSerial port using the HardwareSerial pins. (Why on earth would you do that?)

Then you "begin" that SoftwareSerial port.

Finally you try and print Hello through the HardwareSerial port!

Either use SoftwareSerial OR use the hardware UART. You can't mix and match both. And since you are using the hardware UART pins, why even consider using SoftwareSerial?

void setup() {
    Serial.begin(9600);
}

void loop() {
    Serial.println("Hello");
}

OR, to use other pins than 0 and 1:

#include <SoftwareSerial.h>

const int bluetoothTx = 2;
const int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
    bluetooth.begin(9600);
}

void loop() {    
    bluetooth.println("Hello");
}
  • This worked! But can you please explain why it didn't work at the first place? Also, the modified code didn't work till after I modified the COM port to be the "outgoing" one as seen in my Bluetooth settings, why is that? – sir_weasels Jul 21 '16 at 23:47
  • Before you had turned on the hot tap on the bath and were wondering why the toilet hadn't flushed. You had created and configured a software "bit banged" serial interface, but then proceeded to attempt to send the data down the unconfigured and disabled hardware UART. – Majenko Jul 22 '16 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.