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 !