Serial Print not Displaying Integers

Hi,

I am trying to use an HC-05 module to map the bend of a flex sensor (essentially a potentiometer) to a servo position. But when I use the serial monitor, I am not sure why it does not display the mapped integer value. When I remove the Serial.write it seems to work fine, but I need Serial.write to interface with another HC-05 Bluetooth module on another Arduino. I have included the "broken" serial monitor output from bending the flex sensor and then the code below that.

Broken Serial Monitor Example:

23:54:22.022 -> 	10
23:54:22.131 -> 
23:54:22.244 -> 10
23:54:22.244 -> 
23:54:22.353 -> 9
23:54:22.353 -> 	8
23:54:22.463 -> 8
23:54:22.566 -> 9
23:54:22.675 -> 	9
23:54:22.781 -> 	9
23:54:22.921 -> 	10
23:54:23.032 -> 
23:54:23.138 -> 18
23:54:23.138 -> 80
23:54:23.243 -> P73
23:54:23.349 -> I130
23:54:23.456 -> ⸮139
23:54:23.567 -> ⸮146
23:54:23.674 -> ⸮157
23:54:23.781 -> ⸮167
23:54:23.930 -> ⸮170
23:54:24.038 -> ⸮162
23:54:24.142 -> ⸮171
23:54:24.250 -> ⸮139
23:54:24.365 -> ⸮127
23:54:24.477 -> 87
23:54:24.588 -> W45
23:54:24.694 -> -29
23:54:24.799 -> 5
23:54:24.899 -> 60
23:54:25.008 -> <68
23:54:25.148 -> D144
23:54:25.255 -> ⸮100
23:54:25.361 -> d35
23:54:25.466 -> #13
23:54:25.575 -> -4

Code Used:

const int flexPin = A0;
int flexValue = 0;

#include <Servo.h>
Servo servoIndex;

int state = 0;



void setup() {
  pinMode(flexPin, INPUT);
  servoIndex.attach(9);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
  state = Serial.read(); // Reads the data from the serial port
}
void loop() {
  if (Serial.available() > 0) { // Checks whether data is comming from the serial port
  }
  // Reading the potentiometer
  flexValue = analogRead(A0);
  int flexValueMapped = map(flexValue, 740, 960, 0, 180); // maps flexValue of min 740 max 960 to servo 0-180

  int avgFlexValueMapped;
  int MeasurementsToAverage = 100;
  for (int i = 0; i < MeasurementsToAverage; ++i) // averages last 100 data points to filter and smooth data
  {
    avgFlexValueMapped += flexValueMapped;
    delay(1);
  }
  avgFlexValueMapped /= MeasurementsToAverage;

  Serial.println(avgFlexValueMapped);
  //ADD A DATA TYPE?             ^^^

  Serial.write(avgFlexValueMapped); // Sends flexValue to servo motor
  //analogWrite(1, flexValueMapped);

  servoIndex.write(avgFlexValueMapped);


  delay(10);
}
  if (Serial.available() > 0)   // Checks whether data is comming from the serial port
  {
  }

What is the point of this ?

  avgFlexValueMapped /= MeasurementsToAverage;

This performs integer division. What do you see if you print the 2 values before the calculation ?
Why is avgFlexValueMapped ab int ?

UKHeliBob:

  if (Serial.available() > 0)   // Checks whether data is comming from the serial port

{
 }



What is the point of this ?




avgFlexValueMapped /= MeasurementsToAverage;



This performs integer division. What do you see if you print the 2 values before the calculation ?
Why is avgFlexValueMapped ab int ?
if (Serial.available() > 0)   // Checks whether data is comming from the serial port
  {
  }

This will be used to receive data from another Arduino in the future. Once I know that data is being received I will turn on an LED; however, it currently serves no function for this code. I apologize for including it.

The MeasurementsToAverage displays this on the serial monitor.

00:28:09.711 -> $⸮⸮120
00:28:14.847 -> %120
00:28:14.952 -> %120
00:28:15.098 -> &120
00:28:15.239 -> %120
00:28:15.347 -> &120
00:28:15.494 -> %120
00:28:15.602 -> %120
00:28:15.747 -> %120
00:28:15.894 -> &120
00:28:16.004 -> $120
00:28:16.148 -> %120
00:28:16.261 -> &120
00:28:16.404 -> $120
00:28:16.546 -> $120
00:28:16.685 -> %120
00:28:16.789 -> %120
00:28:16.934 -> $120
00:28:17.069 -> %120
00:28:17.212 -> $120
00:28:17.318 -> &120
00:28:17.462 -> $120
00:28:17.602 -> &120
00:28:17.711 -> %120
00:28:17.852 -> %120
00:28:17.990 -> $120

The avgFlexValueMapped displays this:

00:26:40.204 -> &35
00:26:40.342 -> #36
00:26:40.444 -> $38
00:26:40.580 -> &37
00:26:40.727 -> %38
00:26:40.869 -> &38
00:26:40.972 -> &37
00:26:41.112 -> %38
00:26:41.256 -> &37
00:26:41.365 -> %38
00:26:41.509 -> &38
00:26:41.646 -> &38
00:26:41.787 -> &39
00:26:41.887 -> '36
00:26:42.030 -> $65
00:26:42.173 -> A65
00:26:42.314 -> A122
00:26:42.422 -> z130
00:26:42.571 -> ⸮144
00:26:42.679 -> ⸮148
00:26:42.845 -> ⸮155
00:26:42.960 -> ⸮163
00:26:43.072 -> ⸮129
00:26:43.213 -> ⸮135
00:26:43.347 -> ⸮79
00:26:43.487 -> O52
00:26:43.595 -> 439
00:26:43.739 -> '13
00:26:43.877 -> 
36
00:26:44.020 -> $11
00:26:44.128 -> 45
00:26:44.265 -> -51
00:26:44.406 -> 3112
00:26:44.545 -> p155
00:26:44.654 -> ⸮156
00:26:44.796 -> ⸮156
00:26:44.933 -> ⸮155
00:26:45.044 -> ⸮113
00:26:45.181 -> q10
00:26:45.326 -> 
00:26:45.470 -> 39
00:26:45.470 -> '33
00:26:45.577 -> !39

Why is avgFlexValueMapped ab int ? - this is so it can be used to control a servo at an int position.

The default baud rate for communication with HC05 is 9600. 38400 is the default rate for the AT mode.

groundFungus:
The default baud rate for communication with HC05 is 9600. 38400 is the default rate for the AT mode.

Hi, would you mind elaborating a bit more? All the examples I've seen online have used 38400. What is the difference between AT mode and the "communication" mode? Thanks so much for your help I really appreciate it!

Welcome,

Here are 2 lines of your code

Serial.println(avgFlexValueMapped);
Serial.write(avgFlexValueMapped);

Serial.write will write only one byte, except when you add a second parameter to tell how much byte it must write.

But in any cases, these bytes are not meant to be printed, they should not be treated as ASCII characters, this is why you see those strange characters in the serial monitor.

Serial.write((uint8_t *)&avgFlexValueMapped, sizeof(avgFlexValueMapped));
// or
Serial.write(avgFlexValueMapped & 0xFF);
Serial.write((avgFlexValueMapped >> 8) & 0xFF);

This is what you should use if you want to send your int as bytes. If not then just remove this line, and send as ASCII characters with print() or println() as you already did on the previous line..

(Edit: fixed a mistake..)

Communication mode is for when the HC05 is talking to another Bluetooth device (another HC05). AT mode is for setting up the HC05. In AT mode you can set the communication baud rate, change the name, change the pass code and many other things.

What Arduino are you using? I seems that you are using the serial monitor and have the HC05 on hardware serial (USB) at the same time. It is better if you have the HC05 on its own serial port and reserve the hardware serial port for program upload, program output and debugging. If you are using an Uno or Nano I would recommend using a software serial port.

int flexValueMapped = map(flexValue, 740, 960, 0, 180);

I would constrain the values fed to the map function. Values outside of 740 to 960 will still be mapped and will be outside of the desired 0 to 180.

Here is test code, using software serial, that you can use to verify communication between your Bluetooth device and the HC05.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(4, 5); // RX | TX

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

void loop()
{
   if (Serial.available())
   {
      BTserial.write(Serial.read());
   }
   if (BTserial.available())
   {
      Serial.print(char(BTserial.read()));
   }
}

How to configure, connect and communicate between 2 HC05 Bluetooth modules. Except, use a software serial port (Uno, Nano) or spare hardware serial port (Serial1, 2, 3 on Mega) for the Bluetooth module to keep the the main hardware serial port (USB) for program upload and debug.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.