Go Down

Topic: Arduino - Bluesmirf: Trouble reading Serial (Read 2195 times) previous topic - next topic

firmaj

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
Code: [Select]
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

Code: [Select]
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);

  
}

firmaj

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
Code: [Select]


#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
Code: [Select]


#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);
}



Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

firmaj

#18
Oct 05, 2015, 07:30 am Last Edit: Oct 05, 2015, 07:32 am by firmaj
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.

firmaj

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?

Robin2

I was just about to start but don't get why it would do me any good.
If I am to provide any useful help I need to start from a working baseline.

If you already have a version that works with 2 Bluesmirfs post the code for that and all the connection details.

And then show the code that is causing a problem.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

firmaj

ok,  Robin2, lets start from the beginning with me giving all information :) (Stupid that I didn't share the circuits from the beginning... sorry about that.

I tried my best to to these in an online circuit builder, tell me if you want me to do a schematic instead. I'd be happy to do it.

The modules connect fine to each other (both light up green) but I have get two problems that I don't get:

1 - the value i read from the bluetooth is appr. 48 when the button on the slave is NOT pushed and appear. 76 when pushed. I have no idea why.

2 - The values stop updating in the serial monitor i.e it doesn't read the bluetooth after a couple of seconds (5-10s i would guess). I tried to put in a count (count++; Serial.println(count) ) and showed that the read (or print from the other module, I have no idea) stops after 6500-700 iterations



So here are the connection details and codes:

MASTER




SLAVE





And the codes

MASTER
Code: [Select]

#include <SoftwareSerial.h>

int LED_RED = 6;
int LED_YELLOW = 7;

int ledPin = 12;

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(ledPin, 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("---");
 

  Serial.println("Debugging works");

}

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


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: [Select]

#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);
}



Robin2

#22
Oct 10, 2015, 09:57 am Last Edit: Oct 10, 2015, 10:21 am by Robin2
ok,  Robin2, lets start from the beginning with me giving all information
Thanks for all that. I will have a careful look at your code later today when I have some more time. In the meantime can you confirm that the code you have posted is the code that you referred to in Reply #19 as "successfully connected the two bluesmirfs which would suggest that the read and print works"

And (now that I have read that quotation more closely) can you confirm whether you have actually sent and received data with that program.


I have had a quick look at the code. I don't see how it can possibly work because it is trying to use SoftwareSerial at 115200 baud. I'm pretty sure I made that point earlier.

We need to start with a piece of code that actually works. One of your Arduinos has Serial1 (HardwareSerial) which will work at 115200 baud if that is essential. Start (as I said earlier) with some code that makes that Arduino work in conjunction with your PC.

When I say "something that works" I mean the ability to send and receive text - eg "Hello World"

If you think what I am saying is wrong (it would not be the first time) please explain your reasoning and provide the evidence.


-----------

Your diagrams seem to be clear. Many of the same type are not and a photo of a pencil-drawn schematic is usually the best way to show connections.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

firmaj

Yes you did mention that earlier. I think I disregarded it because I thought all was good as it connected fine with the other bluetooth. But i'll try to take the default baud rate of the Bluesmirf modules down to 9600 and software serial.

I'll be back with the progress!

firmaj

I thought I've tried changing the baud rate to 9600 before.... but the only thing I did was typing softwareserial.begin(9600). Of course I have to change the baud rate of the modules as well........ Now it works...



You have no idea how long I've sat with this, you sir (Robin2) are an angel! Why is it that it doesn't work with a high baud rate?

My button is sent successfully and lights the Led on the other unit via BT perfectly. The only thing I'm wondering is why a '0' is sent as '48' and '1' as '49'. Why does it start at 48?

Robin2

Why is it that it doesn't work with a high baud rate?
SoftwareSerial is a very poor substitute for a hardware USART.

Quote
My button is sent successfully and lights the Led on the other unit via BT perfectly. The only thing I'm wondering is why a '0' is sent as '48' and '1' as '49'. Why does it start at 48?
48 is the Ascii code for '0' etc

Try changing
Code: [Select]
int value_BT = 0;
to
Code: [Select]
char value_BT = 0;

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

firmaj

#26
Oct 10, 2015, 05:06 pm Last Edit: Oct 10, 2015, 05:13 pm by firmaj
Thanks!

Now I can go on with the project after getting the BT to work.

Couldn't do it without you Robin2!

EDIT:

If anyone has the same difficulties as me I used this code to set the baud rate on the softwareserial and the Bluesmirf module (The last three lines are the important ones):

Code: [Select]

 Serial.begin(9600);
  bluetooth.begin(115200);
  bluetooth.print("$$$");
  delay(100); // Wait for CMD to come back
  bluetooth.println("SM,1");
  delay(100);
  bluetooth.println("C,000666643C3D");
  delay(100);
  bluetooth.println("---");
  delay(100);
  bluetooth.print("$$$");
  delay(100);
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600 (until a reboot is done)
  delay(100);
  bluetooth.begin(9600);

Go Up