Serial moniter not working with hc-05 bluetooth module

code:

#include <Servo.h>
const byte numChars = 80;
char receivedChars[numChars];
char tempChars[numChars];  // temporary array for use when parsing

// variables to hold the parsed data

int rightShoulderX = 0;
int rightShoulderY = 0;
int rightElbowX = 0;
int rightElbowY = 0;
int joystickX = 0;
int joystickY = 0;
int left;
int right;
int pos;
Servo turn;
Servo l;
Servo r;

boolean newData = false;

//============

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

  turn.attach(4);
  turn.write(90);
  l.attach(5);
  r.attach(6);
  delay(1000);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(24, INPUT);
  pinMode(26, INPUT);
  pinMode(28, INPUT);
  pinMode(30, INPUT);
}

//============

void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();

    if (joystickY > 0) {
      digitalWrite(30, LOW);
      digitalWrite(28, HIGH);
      digitalWrite(26, LOW);
      digitalWrite(24, HIGH);
    } else if (joystickY < 0) {
      digitalWrite(30, HIGH);
      digitalWrite(28, LOW);
      digitalWrite(26, HIGH);
      digitalWrite(24, LOW);
    }


    Serial.println(pos);


    newData = false;
  }
  left = map(joystickX, 70, 110, -255, 255);
  left = abs(left);
  joystickY = abs(joystickY);
  right = (255 - joystickY) - (255 - left);

  if (joystickX < 90) {

    analogWrite(2, joystickY);
    analogWrite(3, joystickY + right);
  } else if (joystickX > 90) {

    analogWrite(2, joystickY + right);
    analogWrite(3, joystickY);
  } else
    ;
  {
    analogWrite(2, joystickY);
    analogWrite(3, joystickY);
  }


  if (pos == HIGH) {
    l.write(95);
    r.write(85);

  } else
    ;
  {
    l.write(5);
    r.write(175);
  }

  turn.write(joystickX);

  delay(5);
}
//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      } else {
        receivedChars[ndx] = '\0';  // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

//============

void parseData() {  // split the data into its parts
  char* strtokIndx;  // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");  // this continues where the previous call left off
  rightShoulderX = atoi(strtokIndx);    // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  rightShoulderY = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ",");
  rightElbowX = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ",");
  rightElbowY = atoi(strtokIndx);  // convert this part to a float

  strtokIndx = strtok(NULL, ",");
  joystickX = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ",");
  joystickY = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ",");
  pos = atoi(strtokIndx);
}

im sending ints from one arduino to another via hc 05 bluetooth module. I have everything working. The servos are all moving how they r suppose to. But whenever i open the serial moniter everything just stops and nothing is being read by the hc 05. the other arduino that is sending the data works just fine and i can open the serial moniter. but not for this arduino mega.

Sorry if the code is kinda hard to read.

You have posted this in the 2.0 IDE forum section. Which version of the IDE are you using ?

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

How is the HC-05 connected to the Arduino ? Not pins 0 and 1 by any chance ?

wow, Thanks you soo much. I looked and i had been using 1.18 version. when i downloaded 2.0 it told me to install avr something for the arduino and after it installed it was working. thx

but im getting errors trying to compile
Compilation error: Error: 2 UNKNOWN: exit status 1

Please post the code that you are using and the full error message

the code is the same one that i posted on top. this is the only error im getting. it works fine without any errors in 1.8 but gives this error in 2.0 . I suspect that it has something to do with the servo library but im not sure whats going on

I'm going to ask you to post some additional information that might help us to identify the problem.

Please do this:

  1. Right click on the black "Output" pane at the bottom of the Arduino IDE 2.x window.
  2. From the context menu, click "Copy All".
  3. In a forum reply here, click on the reply field.
  4. Click the </> button on the forum toolbar. This will add the forum's ``` code block markup to your reply.
  5. Press "Ctrl + V". This will paste the error into a code block.
  6. Move the cursor outside of the code block before you add any additional text to your reply.
Compilation error: Error: 2 UNKNOWN: exit status 1

I followed ur instruction and thats what i got. But when i remove the #include<Servo.h> then i get this error

c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:16:1: error: 'Servo' does not name a type; did you mean 'Serial'?
 Servo turn;
 ^~~~~
 Serial
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:17:1: error: 'Servo' does not name a type; did you mean 'Serial'?
 Servo l;
 ^~~~~
 Serial
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:18:1: error: 'Servo' does not name a type; did you mean 'Serial'?
 Servo r;
 ^~~~~
 Serial
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino: In function 'void setup()':
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:27:1: error: 'turn' was not declared in this scope
 turn.attach(4);
 ^~~~
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:27:1: note: suggested alternative: 'tan'
 turn.attach(4);
 ^~~~
 tan
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:29:1: error: 'l' was not declared in this scope
 l.attach(5);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:30:1: error: 'r' was not declared in this scope
 r.attach(6);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino: In function 'void loop()':
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:96:1: error: 'l' was not declared in this scope
 l.write(95);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:97:1: error: 'r' was not declared in this scope
 r.write(85);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:102:1: error: 'l' was not declared in this scope
 l.write(5);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:103:1: error: 'r' was not declared in this scope
 r.write(175);
 ^
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:106:1: error: 'turn' was not declared in this scope
 turn.write(joystickX);
 ^~~~
c:\Users\DaudH\AppData\Local\Temp\.arduinoIDE-unsaved2021328-19788-12nz8e0.w6yw\sketch_apr28a\sketch_apr28a.ino:106:1: note: suggested alternative: 'tan'
 turn.write(joystickX);
 ^~~~
 tan
Compilation error: Error: 2 UNKNOWN: exit status 1

I have no clue why i got this error, everything was working in 1.8

OK, then we'll need the verbose output.

Please do this:

  1. Select File > Preferences from the Arduino IDE's menus.
  2. Check the box next to "Show verbose output during: [ ] compilation".
  3. Click the OK button.
  4. Select Sketch > Verify/Compile from the Arduino IDE's menus.
  5. Wait for the compilation to fail.
  6. Right click on the black "Output" pane at the bottom of the Arduino IDE 2.x window.
  7. From the context menu, click Copy All.
  8. Paste the copied output in a forum reply here just as you did before (thanks!)

That's normal. If you are using the Servo library then you must have the #include directive in your sketch for Servo.h.

Using board 'mega' from platform in folder: C:\Users\DaudH\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Using core 'arduino' from platform in folder: C:\Users\DaudH\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Detecting libraries used...
"C:\\Users\\DaudH\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\\Users\\DaudH\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\cores\\arduino" "-IC:\\Users\\DaudH\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\variants\\mega" "C:\\Users\\DaudH\\AppData\\Local\\Temp\\arduino-sketch-6035CAB491FB568848D7440090A43E49\\sketch\\sketch_apr28a.ino.cpp" -o nul
Alternatives for Servo.h: []
ResolveLibrary(Servo.h)
  -> candidates: []
Compilation error: Error: 2 UNKNOWN: exit status 1

i get this error

OK, now we're cooking!

This error is caused by not having the Servo library installed. Because this library comes pre-installed with the classic Arduino IDE, the tutorials you find won't mention anything about needing to install it. But the Arduino IDE 2.x doesn't currently come with any libraries pre-installed. You need to install all of them yourself.

That's very easy to do:

  1. Select Sketch > Include Library > Manage Libraries... from the Arduino IDE's menus.
  2. In the "Filter your search..." field, type "servo".
  3. Scroll down through the list of available libraries until you see "Servo by Michael Margolis, Arduino". Click on it.
  4. Click the INSTALL button.
  5. Wait for the installation to finish.

Now you should be able to compile your sketch.

thank you soooooo much, it worked.

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

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