[Help] Confusing ints transfered between two HC-05

Hey guys,
Meaps here. I just wanted to ask a question about a problem I simply cannot resolve. Skip the next paragraph for the important info. Sorry for the wall of text. Just wanted to be as thorough as possible.

I just started programming with arduinos during the past 3-4 months. Been doing good on my own for a while, but I hit a wall, and a pretty frustrating one. Usually I can work around the problems by searching google, but this time I couldn’t find much. Yes, I know, there are a lot of threads about connecting two HC-05 modules together for a bluetooth connection, and believe me, I’ve searched for over a week trying to find help. There isn’t a post or an archive that I haven’t read on the subject. On that note, let’s begin:

My goal is to send a voltage read from my arduino nano (voltage of a lipo battery pack) through the HC-05 modules to a teensy 3.2 and display an approximate voltage through an RGB anode led. My project’s goal is to create a custom remote for my diy electric longboard. That is why I have other parts of my code sending angle values for the motor speed controllers.

My issue is that no matter what I try, I seem to get inconsistent values sent or received through my HC-05. I’ll send an int (annalogValueMapped in the longboard code) and receive random values like a stream of eights and some high value like 144 every now and then on my remote code (batteryBt) when printing to the serial monitor. Please note that the teensy uses two separate serial leads for USB and tx0 and rx0 unlike the nano. As a side note, I am 100% certain that the values are reading correctly on the nano (my voltage should read 3.7volts, which the nano appropriately prints on the serial monitor while debugging without the HC-05 factored in). This was all triple checked after countless hours of testing.

Here is what I expect to happen, my nano sends out a value between 0 - 255 through bluetooth with the analogValueMapped integer. Then, my teesny should receive that value, store it in batteryBt, display it on my serial monitor and then calculate and average of all the last 25 received values by converting the 0-255 value to the reference voltage which is about 4.85V.

Here is what is even more interesting, notice my remote code also reads an analog value from a potentiometer, then remaps it to a 0-179 value, and sends it to the lonboard (nano). This is because I want to control the speed of the motors with the angle of the pot. I hooked up the longboard to the nano, and reset the max/neutral/min positions accordingly.When testing, the motors worked perfectly with the angle of the potentiometer I was inputting… Which means it was sending the appropriate values between 0 and 179 to the speed controllers without a problem through the HC-05 modules…

More info:
My remote is set as the master (teensy) and the longbaord is set as the slave (nano). I doubt this has any effect but if it helps any of you now you know :wink:

Here is the code on my longboard (nano, I realise it’s sloppy, sorry for that):

#include <Servo.h>


Servo esc;
int incThrottle;
int ledPin = 13;
int analogValue = 0;


void setup() {

  analogReference(EXTERNAL);
  analogRead(A0);
  pinMode(ledPin, OUTPUT);
  esc.attach(9);
  Serial.begin(38400); //Initialize serial bluetooth connection
  
}

void loop() {
  
  analogValue = (analogRead(A0));
  int analogValueMapped = map(analogValue, 0, 1023, 0, 255);
  Serial.write(analogValueMapped);
  if(Serial.available()>0){
  incThrottle = Serial.read(); //Updates incThrottle when receiving Serial
  }

  if(analogValue > 600){
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
  
  esc.write(incThrottle);  //Sends angle to esc
  

 
  delay(1);
  
}

and here is the code in the remote (teensy, idem):

int outThrottle;
int outThrottlePin = 14;
int ledPin = 13;
int redPin = 15;
int greenPin = 16;
int bluePin = 17;
double batteryVolt;
int batteryBt;
float avgBatt;

const int numReadings = 25;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
double total = 0;                  // the running total
double average = 0;                // the average
void setup() {

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(outThrottlePin, INPUT);
  delay(1000);
  Serial1.begin(38400);
  Serial.begin(9600);
   for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {

  
  outThrottle = analogRead(outThrottlePin);
  outThrottle = map(outThrottle, 0, 1023, 0, 179);
  Serial1.write(outThrottle);

  if(Serial1.available()>0){

       batteryBt = Serial1.read(); //Gets battery voltage
       
       avgBatt = (((batteryBt*4.85)/256L));

      // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = avgBatt;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = (total) / numReadings;
  // send it to the computer as ASCII digits
      
       Serial.println(average);
  }

  if (average > 4.1) {
    setColor(255, 255, 255);  // white
  }
  else{

  if (average > 3.9 && average <= 4.1){
    setColor(0, 255, 0);  // green
  }else{
  if (average > 3.7 && average <= 3.9){
    setColor(255, 255, 0);  // yellow
  }else {
  if (average > 3.5 && average <= 3.7){
    setColor(255, 0, 0);  // red
  }else{
  if (average > 3.4 && average <= 3.5) {
    blinkRed();
  }
  }
  }
  }
}
  delay(1);
  if(outThrottle > 90){
    digitalWrite(ledPin,HIGH);  
  }
  else{
    digitalWrite(ledPin,LOW); 
  }

}

void setColor(int red, int green, int blue)
{
  
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

void blinkRed(){

  setColor(255, 0, 255); 
 
}

Any help would be greatly appreciated. As for the actual circuit, I am 99% sure everything is fine. It’s pretty simple after all. One last note, I double checked everything except the baud rate set in for the HC-05 modules, although I am once again pretty certain of having set both to 38 400. Will be making sure tomorrow, I don’t have time anymore for tonight :slight_smile:

Will be making sure to post my full step by step guide to the remote/receiver process when I’m done, hopefully will be able to help some others out there.

Just incase, here is the specific wiring for both modules:
RX → TX
TX → RX

Same for both.

I'm not sure what the problem is but you are putting the value of avgBatt, which is a float, into readings, which an array of int.

Yea I just noticed that too. The average count wasn't my code, I took it from another user who posted a while back. Will be making my own version once I get the problem figured out. Thanks anyway :slight_smile:

First of all I would print the received batteryBt to see if it matches the value from the nano; might be a bit tricky to see if it goes too fast; you might have to add some extra delays during the testing (or write dedicated test sketches for nano and teensy to test the (possibly) problematic communication) without driving motors and so on.

Secondly, you can try to take the HC-05 modules out of the equation by hardwiring the nano and the teensy. If you get the expected results, you know that something is wrong in one (or both) HC-05(s).

The above would be my approach.

sterretje:
First of all I would print the received batteryBt to see if it matches the value from the nano; might be a bit tricky to see if it goes too fast; you might have to add some extra delays during the testing (or write dedicated test sketches for nano and teensy to test the (possibly) problematic communication) without driving motors and so on.

Secondly, you can try to take the HC-05 modules out of the equation by hardwiring the nano and the teensy. If you get the expected results, you know that something is wrong in one (or both) HC-05(s).

The above would be my approach.

Well I have already monitored the receiving end of the nano. I was unclear on that sorry. I printed on the serial monitor the receiving end of the HC-05 of the Teensy. I will try connecting directly although I do believe there is an issue with the HC-05. Luckily, I have 4 spare. Will be trying tomrrow!

Using Serial.write() and sending byte values that can be used directly by the receiving program has some attractions but it makes debugging difficult.

Unless I really need the extra performance I always send data in human readable form. So, for your example I would probably do something like

Serial1.print('<');
Serial1.print(outThrottle);
Serial1.println('>');

which will give something like “<123>”

and use the 3rd example in Serial Input Basics at the receiving end. There is also a parse example to illustrate how to turn the received text value back into a number.

You should have no problem sending data 5 or 10 times per second which should be perfectly adequate for a human control system.

…R

Robin2:
Using Serial.write() and sending byte values that can be used directly by the receiving program has some attractions but it makes debugging difficult.

Unless I really need the extra performance I always send data in human readable form. So, for your example I would probably do something like

Serial1.print('<');

Serial1.print(outThrottle);
Serial1.println(’>’);



which will give something like "<123>"

and use the 3rd example in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) at the receiving end. There is also a parse example to illustrate how to turn the received text value back into a number.

You should have no problem sending data 5 or 10 times per second which should be perfectly adequate for a human control system.

...R

Thanks, you got some great tutorials :slight_smile: I am going to try this right now.

Here is a little update on everything I tried this morning:

-Made sure that both HC-05 were set at a 38400 baud rate.
-Replaced both of them to make sure I didn’t have a defective module.
-Printed the serial output from the nano and recorded the good voltages before reaching the HC-05.
-And lastly, which is interesting, I connected the nano HC-05 module to my phone, and received values of 51-61 when they should of been around 170-180.

So the problem resides in the conversion the arduino makes when sending the int values through serial. Hopefully Robin’s solution will help me diagnose the issue!

I'll send an int (annalogValueMapped in the longboard code) and receive random values like a stream of eights and some high value like 144 every now and then on my remote code

The usual cause of this - reading a stream of values with the occasional different one - is not checking if there is data available on serial before doing a read(). Read does not block - it returns a -1 if there's no data.

I connected the nano HC-05 module to my phone, and received values of 51-61 when they should of been around 170-180.

48-57 are the ASCII codes for digits. This would make me look for something converting numbers to text.

Another possibility is that you might be writing too fast. I wrote some bluetooth code a little while back and found that it wouldn't work reliably if I tried to do more than 10 characters a second - nuts, I know. Obviously I was doing something wrong. At a guess, it was starting and ending a packet of some sort for each individual character, and of course all that takes time. Try jacking your delay(1) up to something ridiculous like delay(250). After all - how fast does this thing really need to be running, anyway?

To anyone who comes across this post later in the futur, I have to say I learned a lot in the past few days... After relaying the printed values to my phone through bluetooth, I discovered that, by just hitting the table my project was on, it made my values spike. My problem seemed to be related to poor connections. Then I realised I was naive and didn't read up on powering an arduino... I had connected the power to the arduino to the Vin instead of the 5v pin. I never thought it would make a difference. In fact, I was told by many friends that I should connect it to the Vin pin.

Solution: I connected the regulated 5v from my lipos to the 5v pin on the nano. Kept pretty much the code in the original post except I increased the delay to 50 ms like Paul recommended. This gave me an accurate analog reference voltage and a proper voltage powering the whole unit. I am so happy I figured this out. Thank you everyone for helping and giving em tricks to debug.

Thanks for the feedback

...R