0

I am working with a combination of Teensy++ 2.0 and Sparkfun Bluetooth Modem.

The Arduino program, inside the loop, continuously reads a sensor value. If the value read is above a threshold, the Arduino transmits a "CHARACTER" over Bluetooth for any receiver.

So, inside the same loop, I check for the availability of data, and print the received character. The problem is, the received character is not what is transmitted. The received character is some garbage value.

Please let me know what is going wrong here.

#include <SoftwareSerial.h>

#define bluetooth Serial1

#define BAUDRATE 9600

// Analog read 0 is pin 38 
int sensorValue = 0;
int SENSOR_ANALOG_READ_PIN = 0;
int LED = 17;

char toPrint = '\0';
bool sent = false;
char state = ' ';

void setup() 
{

// put your setup code here, to run once:

  pinMode(LED, OUTPUT);

  Serial.begin(BAUDRATE);

  bluetooth.begin(BAUDRATE);

}


void loop() {


  // put your main code here, to run repeatedly:

  sensorValue = analogRead(SENSOR_ANALOG_READ_PIN);


  if(value > 500)

  {
      //Serial.println("HIGH");

      digitalWrite(LED, HIGH);

      bluetooth.write(state);

      bluetooth.flush();

      sent = true;

      digitalWrite(LED, LOW);

  }

  else

  {

      delay(100);

  }

  ///////////////////////////////////////////////////////////
  if((bluetooth.available()) && (sent == true))

  {

        Serial.println("printing : ");

        toPrint = (char)bluetooth.read();

        Serial.println((char)toPrint);

        sent = false;

   }

}

Output:

printing : 
1
printing : 
ˆ
printing : 
Ì
printing : 
¦
printing : 
È
printing : 
Ì
printing : 
Ì
printing : 
  • 1
    The code is really hard to read as it is right now - namely because it's not formatted. Could you go through your code and ensure it's properly formatted? It would make it much easier for us to help you. – Mlagma Jul 21 '15 at 4:30
  • @Mlagma The code is now formatted by Gerben . – Vijay Rajanna Jul 21 '15 at 10:11
  • Try without the sensor value first by sending known exact values (so you only test the bluetooth connection. – Thomas S. Aug 21 '15 at 18:54
1

Is the device on the other side of the bluetooth connection running at 9600 baud too ?

You say you transmit the state of the sensor - which BTW is only ever a space ' ', and never changes.

So that value gets written out via the bluetooth connection.

But then you read data in from the other party to the bluetooth communications.

It sounds like you expect the data written to the bluetooth output serial line to also be echoed back from the bluetooth input. The serial transmit and receive are separate channels. You are receiving on the bluetooth.read() whatever the device connected to it is sending, not what you write yourself.

What you are sending goes only to the other side of the conversation. The remote device would need to echo whatever it sees back for it to work how you describe.

1

The default baud rate for HC-05 is 38400 bauds. I was trying to set it to 9800. For some reason, that was not accepted by HC-05.

Solution: I changed the line

#define BAUDRATE 9600

to

#define BAUDRATE 38400

This solved my problem.

0

Are you sure you have defined your pins properly? I don't see anywhere where you define SENSOR_ANALOG_READ_PIN as an input. If this is the Tx/Rx pin (I forgot the pin configuration), you can define the input pin to be somewhere else to test using the SoftwareSerial library.

This is what I did and it worked for me, hope it helps.

This link isn't for your module but the concept should be the same: Arduino AND Bluetooth HC-05 Connecting easily.

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.