Arduino - Bluesmirf: Trouble reading Serial

Hi!

I have two arduinos (One Micro and one Uno), each with one Bluesmirf Silver(configured as Master and Slave).

Setting up the connection between the modules does work (the green connect light lits up on both). My problem appears when I send a "1" from one and want to read it from the other. I get this in the seriell monitor:

Does somebody have any ideas? I just want to send from one and receive from the other, that's the problem for now.

My Codes:

MASTER

int ledPin = 12;

int val = 0;

void setup() {
  Serial.begin(115200);
  Serial.print("$$");

  delay(100);
  Serial.println("SM,1");
  delay(100);

  Serial.println("C,000666643C3D");
  delay(100);
  Serial.println("---");

  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    val = Serial.read();
    delay(2000);

    if (val == 1) {
      digitalWrite(ledPin, HIGH);
      delay(2000);
      digitalWrite(ledPin, LOW);
    }
  }
}

SLAVE

void setup() {
  
  Serial.begin(115200);
  Serial.print("$$");
  
  delay(100);
  Serial.println("SM,0");  
  delay(100);
   
  Serial.println("---");
  

}

void loop() {

  delay(2000);
  Serial.println(1);
  delay(2000);  

}

sketch_oct01a:33: error: missing terminating ' character
sketch_oct01a.ino: In function 'void loop()':
sketch_oct01a:34: error: void value not ignored as it ought to be
sketch_oct01a:34: error: expected `)' before ';' token
sketch_oct01a.ino: At global scope:
sketch_oct01a:41: error: expected declaration before '}' token

Your master code won't even compile.

You seem to be trying to talk to the PC and the other Arduino using the same serial port, and expecting that somehow each will know when you mean to talking to it. That's a no-go.

Sorry, changed the master to try something out..... changed it back now.

How do I specify that I want to talk to the other Arduino? I forgot to say that I'm connected to RX/TX (pin 0 & 1) on the boards, I thought they would replace the USB serial?

I thought they would replace the USB serial?

But you didn't remove the USB cable, did you?

How do I specify that I want to talk to the other Arduino?

Use SoftwareSerial and two other pins.

Tried it without the USB connected but going to change to the SoftwareSerial and see if that helps. I'll be back with news.

Thanks for now

Post the latest version of your two programs.

...R

EDIT:
I tried to disconnect the bluesmirf module and the code stills makes the same printout. so my bluetooth.available() and read() doesn't seem to read the bluetooth?

These are my codes that are in use right now, sorry about that.

So I'm now using SoftwareSerial and the devices connect fine, but I'm still having trouble reading values. This is what's coming out from the serial monitor on the master:

CODE FOR MASTER

#include <SoftwareSerial.h> 

int LED_RED = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int value_BT = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);

///Indicating that this is the master
  led_indicate_master();

  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  

// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.begin(115200);
  Serial.println("Debugging works");
}

void loop() {

  if (bluetooth.available() > 0) {
    value_BT = bluetooth.read();
    delay(50);
    Serial.println(value_BT);
    delay(50);
  }

}


void led_indicate_master() {
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);

  
}

CODE FOR SLAVE

#include <SoftwareSerial.h> 

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;

int bluetoothTx = 9;  // TX-O pin of bluetooth mate, Arduino D9
int bluetoothRx = 10;  // RX-I pin of bluetooth mate, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);

///Indicating that this is the slave
 
  led_indicate_slave();
  
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,0");  
  delay(100);
  bluetooth.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {
 
  delay(2000);
  bluetooth.println(1);
  delay(2000);
  
}

void led_indicate_slave() {
  digitalWrite(LED_GREEN,HIGH);
  digitalWrite(LED_RED,HIGH);
  delay(5000);
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,LOW);
  delay(50);
}

I don't think SoftwareSerial will work at 115200 baud. Try 9600 and if that works try 19200 and maybe 38400.

...R

This is what's coming out from the serial monitor on the master:

That is TEXT. That is the LAST picture of text that you will post.

Robin2:
I don't think SoftwareSerial will work at 115200 baud. Try 9600 and if that works try 19200 and maybe 38400.

...R

My BT modules won't connect at 9600, 19200 or 38400. Only 115200:S

PaulS:
That is TEXT. That is the LAST picture of text that you will post.

Didn't get this at all, have I done something to upset you? (Is there a rule that images aren't allowed to be inserted? If so than I've totally missed that. But why the anger?)

I tried to replace bluetooth.print to digitalWrite(bluetoothRx, HIGH) and on the other arduino digitalRead(bluetoothTx) but I only get 1 all the time from the read.

Is there a rule that images aren't allowed to be inserted?

Images are fine. Pictures of text are not.

But why the anger?

Two reasons. You are wasting bandwidth. And, I can't read the text in the picture.

firmaj:
My BT modules won't connect at 9600, 19200 or 38400. Only 115200:S

Then you may have arrived at the end of the SoftwareSerial road.
The Micro uses the 32U4 MCU and has a spare HardwareSerial port on pins 0 and 1.

firmaj:
I tried to replace bluetooth.print to digitalWrite(bluetoothRx, HIGH) and on the other arduino digitalRead(bluetoothTx) but I only get 1 all the time from the read.

Why would you think that might do anything useful ?

...R

PaulS:
Images are fine. Pictures of text are not.
Two reasons. You are wasting bandwidth. And, I can't read the text in the picture.

Got it, my apologies!

Robin2:
Then you may have arrived at the end of the SoftwareSerial road.
The Micro uses the 32U4 MCU and has a spare HardwareSerial port on pins 0 and 1.

Why would you think that might do anything useful ?

...R

My problem exists when using the hardware serial as well. I can't read what I send. I get the same number combination when printing what is read. So there's no difference , thats the weird part

firmaj:
My problem exists when using the hardware serial as well.

Post the code that demonstrates that and we will see if we can get it working properly.

...R

So there's no way of getting the SoftwareSerial to work? I kinda need two modems on one of the arduinos. I think its weird that it can send and receive when modules are connecting to each other but not when I read them.

Anyways, here are the codes when using the Hardware Serial (RX/TX)

MASTER CODE

int LED_GREEN = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int val = 0;

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);

  Serial.begin(115200);
  Serial.print("$$");

  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  delay(50);

  delay(100);
  Serial.println("SM,1");
  delay(100);

  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_GREEN, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_GREEN, LOW);
  delay(50);

  Serial.println("C,000666643C3D");
  delay(100);
  Serial.println("---");


// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);


}

void loop() {
  if (Serial.available() > 0) {
    val = Serial.read();
    Serial.println(val);
    delay(2000);
  }  
}

SLAVE CODE

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  
  Serial.begin(115200);
  Serial.print("$$");
  
  digitalWrite(LED_GREEN,HIGH);
  delay(500);
  digitalWrite(LED_GREEN,LOW);
  delay(50);
  
  delay(100);
  Serial.println("SM,0");  
  delay(100);
  
  digitalWrite(LED_RED,HIGH);
  digitalWrite(LED_GREEN,HIGH);
  delay(500);
  digitalWrite(LED_RED,LOW);
  digitalWrite(LED_GREEN,LOW);
  delay(50);

  
  Serial.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {
  delay(2000);
  Serial.print(1);
  delay(2000);

  
}

So I played around with the arduinos and modules and used the code below. If I press the button on the slave it should send int value 1 and light the LED on the board. The master will read and print this in the console and then light it's LED.

This works in the first few seconds and I've observed two weird things (not counting the fact that my modules can't be configured in another baud rate than 115200).

  • When I press the button (value=1 sent from the slave) my value that is displayed in the console is 76 and when not pressed (value=0 sent from the slave) it goes to 48-49.

  • The console stops updating and the LED on the master doesn't work after a few seconds.

MASTER CODE

#include <SoftwareSerial.h> 

int i = 0;

int LED_RED = 6;
int LED_YELLOW = 7;

int buttonPin = 4;
int ledPin = 12;
int buttonState = 0;

int value_BT = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);

///Indicating that this is the master
  led_indicate_master();

  Serial.begin(115200);
  bluetooth.begin(115200);
 
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  

// initialize LED pin and pusbutton
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.println("Debugging works");

}

void loop() {

  if (bluetooth.available() > 0) {
    value_BT = bluetooth.read();
    Serial.println(value_BT);

    if (value_BT >= 76) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}


void led_indicate_master() {
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  delay(500);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_RED, LOW);
  delay(50);
}

SLAVE CODE

#include <SoftwareSerial.h> 

int LED_RED = 5;
int LED_GREEN = 6;

int value_BT = 0;

int ledPin = 11;
int buttonPin = 12;
int buttonState = 0;

int bluetoothTx = 9;  // TX-O pin of Bluesmirf silver, Arduino D9
int bluetoothRx = 10;  // RX-I pin of Bluesmirf silver, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(bluetoothTx, INPUT);
  pinMode(bluetoothRx, OUTPUT);
  
///Indicating that this is the slave
  led_indicate_slave();
  
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("SM,0");  
  delay(100);
  bluetooth.println("---");
  
  pinMode(ledPin, OUTPUT);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    value_BT = 1;
    digitalWrite(ledPin, HIGH);
    bluetooth.print(value_BT);
  } else {
    value_BT = 0;
    digitalWrite(ledPin, LOW);
    bluetooth.print(value_BT);
  }
}


void led_indicate_slave() {
  digitalWrite(LED_GREEN,HIGH);
  digitalWrite(LED_RED,HIGH);
  delay(5000);
  digitalWrite(LED_GREEN,LOW);
  digitalWrite(LED_RED,LOW);
  delay(50);
}

Slow down a little. I can't keep up with you.

In Reply #13 I understood you to say that your code would not work with HardwareSerial. As far as I know you only have one Arduino that can use HardwareSerial with your Bluetooth module so I assumed you were trying to send / receive Bluetooth data to/from a PC for testing purposes.

In my opinion you should get that working before you try to communicate with another Arduino using Bluetooth.

...R

Ah no, I've been connecting from Bluesmirf to Bluesmirf all this time.

I'll go back to Bluesmirf - Mac later today and come back with results.

I was just about to start but don't get why it would do me any good. I need to use SoftwareSerieal and I have already successfully connected the two bluesmirfs which would suggest that the read and print works.

My problem is as I mentioned before: When I read the button and send it via the BT module to light a LED on the other unit. It only seems to work for 5-10s then it seems like the BT stops reading. Also the values I read are 76 and 48-49 when sending 1 and 0.

Do you still think I should go to arduino <--> Mac?