Arduino Leonardo Serial communication

Hello all !

I have problems using Arduino Leonardo with Serial communication either via USB to PC or via Bluetooth Module HC-05 to android phone. I have searched a lot about Arduino Leonardo and i know it uses two classes of Serial communication. (Serial for USB to PC and Serial1 for TX, RX pins). i have tried many codes but none worked except one just to turn LED on and off :

int ledpin = 13;
String readString;

void setup() {
while (!Serial);
Serial.begin(9600);
pinMode (ledpin, OUTPUT);

}
void loop() {
while (Serial.available()) {
delay (30);
char c = Serial.read();
readString += c ;
}
if (readString.length()> 0) {
Serial.println(readString);
if (readString == "ON")
{digitalWrite(ledpin, HIGH);
}
if (readString == "OFF")
{digitalWrite(ledpin, LOW);
}
readString ="";
}
}

so i changed a little bit the code to drive a DC motor via Serial port using MC 33926 Pololu dual motor driver

int PWM = 5;
int DIR = 9;
String readString;

void setup() {
while (!Serial);
Serial.begin(9600);
pinMode (PWM, OUTPUT);
pinMode (DIR, OUTPUT);

}
void loop() {
while (Serial.available()) {
delay (30);
char c = Serial.read();
readString += c ;
}
if (readString.length()> 0) {
Serial.println(readString);
if (readString == "1")
{digitalWrite(PWM, HIGH);
digitalWrite(DIR, HIGH);
}
if (readString == "2")
{analogWrite(PWM, 127);
digitalWrite(DIR, HIGH);
}
if (readString == "3")
{analogWrite(PWM, 64);
digitalWrite(DIR, HIGH);
}
if (readString == "0")
{digitalWrite(PWM, LOW);
digitalWrite(DIR, HIGH);
}
readString ="";
}
}

but it didnt work even though the same code works perfectly with Arduino Nano !

I tried all PWM pins of Leonardo coz i was assuming maybe it is a Timer issue regarding frequency but it did not work too !

so my question is about Serial Communication on Arduino Leonardo board, anyone can help coz Leonardo drove me crazy !

thanks in advance !

I can't figure from your Post what it is that actually want help with.

You should be able to connect a HC05 to the Serial1 pins on a Leonardo and it should just-work. Much simpler than with an Uno, for example.

You can use Serial (the USB connection) to print debug messages on the Serial Monitor on your PC.

Post a diagram of how you have everything connected. And post the code for the program you are trying and the output from the program.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

There is just NO excuse for using Strings to store single characters.

Why are you not debugging your code? ASSuming that it works perfectly, and then guessing why it doesn't is foolish.

You don't say how your attempts failed; please indicate twhat the second code does and what you expect it to do.

I would start by changing

  if (readString.length()> 0) {
    Serial.println(readString);

to

  if (readString.length()> 0) {
    Serial.print("[");
    Serial.print(readString);
    Serial.println("]");

What does it print?

[n]

or

[n
]

One of the problems might be that you send a CR and/or LF (when using serial monitor). And "1\r\n" will never match "1" :wink:

And follow Robin2's advise in the thread he linked.

The description of the problem in Reply #4 does not bear much relation with the Original Post :slight_smile:

First thing I would do is use a more reliable way to receive data. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

I've tested your second code on a bare Leonardo (nothing connected) with Serial Monitor and it does what I expect it to do (printing the numbers); I'm using Windows 8.

Next I changed the PWM pin to 13 (just to have a visible indication, this only works on the Leonardo, not the Nano) and I can dim the LED. If you disconnect your driver board, can you control the on-board LED that way?

lets move to Leonardo..first code works well, i can turn the LED on and off via Serial monitor but the second one doesnt work ...so when i type 1 or 2 or 3 nothing happens...so i tried all PWM pins of Leonardo and still nothing happens

Does it print the numbers?

What is the line ending set to in serial monitor? I can get it to fail if the line ending is not set to "no line ending".

void setup() {

  Serial.begin(9600);
  while (!Serial) {

  } 
}

On Leonardo this bit is important, how have you managed to get them in reverse order?

indev2:

void setup() {

Serial.begin(9600);
  while (!Serial) {

}
}




On Leonardo this bit is important, how have you managed to get them in reverse order?

I was wondering about that; but it worked (for some reason) on my setup. Do you have an explanation?

Serial.begin() does not do much on Leonardo so the order does not matter:

void Serial_::begin(unsigned long /* baud_count */)
{
        peek_buffer = -1;
}