Arduino Leonardo Serial

Hi All,

I am trying to use Leonardo and trying to get the HC05 module communicate.

The connections which I have done is:

HC05 TX -> Leonardo RX
HC05 RX -> Leonardo TX

Both the Arduino and HC05 have been connected to common ground and 5V is supplied to HC05.

However I dont see anything in the COM window

Here is the Code below:


String inputString = "";
bool stringComplete = false;

int scoreLeft = 0;
int scoreRight= 0;
int timerMin  = 0;
int timerSec  = 0;

void setup() {
  delay(500);
  Serial.begin(9600);
  Serial1.begin(9600); 
  while(!Serial) {
  Serial1.begin(9600);  
  Serial.println("STARTING NOW");
  }
  strip.begin();
  strip.setBrightness(50);
  strip.clear();
  strip.show();
  
  inputString.reserve(200);
}


void loop() {
  
  if (stringComplete) {
    Serial1.println(inputString);
    if(inputString.length()!=4){
      inputString = "";
      stringComplete = false;
      Serial1.println("InCorrect LENGTH String Received!");
    }
    char cmd = inputString[0];
    char param = inputString[1]-'0';
    inputString = "";
    stringComplete = false;
    switch(cmd){
      case 'L':
        scoreLeft = scoreLeft+param;
        Serial1.print("Score LEFT = ");
        Serial1.println(scoreLeft );
        showScore(scoreLeft,1);
        break; 
      case 'l':
        scoreLeft = scoreLeft-param;
        Serial.print("Score LEFT = ");
        Serial.println(scoreLeft );
        showScore(scoreLeft,1);
        break;
      case 'R':
        scoreRight= scoreRight+param;
        Serial1.print("Score RIGHT = ");
        Serial1.println(scoreRight);
        showScore(scoreRight,2);
        break; 
      case 'r':
        scoreRight= scoreRight-param;
        Serial1.print("Score RIGHT = ");
        Serial1.println(scoreRight);
        showScore(scoreRight,2);
        break;
      default:
        Serial1.println("COMMAND NOT RECOGNIZED YET");
        break;
    }
  }
}

void serialEvent() {
  while (Serial1.available()) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

It would be great, if someone could help me solve the issue with communication. I have used the Serial Blue Tooth Terminal (Android APP) but there seems to be no message coming in COM window apart from the first one Serial.println("STARTING NOW");

Welcome to the forums. Please take a few minutes and read this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

It will help others help you.

You should also check out this tutorial: Serial Input Basics - updated

1 Like

Serial & Arduino give many a headache so years ago I built:

The QBF - "The Quick Brown Fox..." For Serial Diags - Community / Exhibition / Gallery - Arduino Forum

If you have a second arduino, you can test your coms. If you have USB (FTDI, etc) to serial adapter that will too. Or, you can tie Tx to Rx on most Arduinos and just check for local loop by using a few lines of code to check if a character is available and print the same.

How to do a loopback test – Arduino Help Center

Please edit your post and change these apostrophes into backticks so your code will be more readable.

Please change  ''' into  ```
1 Like

There are issues with SerialEvent on the Leonardo.
https://github.com/arduino/ArduinoCore-avr/issues/206

You are better off to not use it and just check Serial and Serial1 in loop.

if(Serial.available() > 0)
   {
      // Deal with serial data
   }
if(Serial1.available() > 0)
   {
      // Deal with serial1 data
   }
1 Like

Thank you very much for your help. It solved my problem with Leonardo. Now I can succesfully work with COM (serial) and Serial 1.

I will try with Mega, I think for that one my original code should work :slight_smile:

It may, but using SerialEvent is bad practice. It does not work on all platforms, and is likely to be deprecated in the future. As a function it does not do anything different than testing for .available() in loop.
You are better off to break a bad habit and move on.

1 Like

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