Problem serial communication between Arduino Leonardo and Arduino Mega ADK

I was trying to communicate from the Mega ADK’s TX2 pin (16) to the Leonardo’s RX1 pin (0). The Leonardo supposed to receive serial commands from the Mega, but it looks like it won’t receive anything.

Here is the code for the Leonardo (based off one of Human Hard Drive’s tutorials):

String cmd;

void setup()
{

	Mouse.begin();
	Keyboard.begin();
	Serial1.begin(115200);

	while (!Serial1);


}



void loop()
{
	while (Serial1.available())
	{
		char c = Serial1.read();
		if (c == '\n')
		{

			parseCmd(cmd);
			cmd = "";
		}
		else
		{
			cmd += c;
		}
	}
}void parseCmd(String i)
{
	String com;
	String arg;

	int spaceIndex = com.indexOf(" ");
	com = i.substring(0, spaceIndex);
	arg = i.substring(spaceIndex + 1);

	if (com.equals("LHX"))
	{
		int val;
		int mval;
		val = arg.toInt();
		if (val < 117)
		{
			map(mval, 0, 116, -10, 1);
		}
		if (val > 137)
		{
			map(mval, 138, 255, 1, 10);
		}

		Mouse.move(mval, 0, 0);

	}

	if (com.equals("LHY"))
	{
		int val;
		int mval;
		val = arg.toInt();
		if (val < 117)
		{
			map(mval, 0, 116, -10, 1);
		}
		if (val > 137)
		{
			map(mval, 138, 255, 1, 10);
		}

		Mouse.move(0, mval, 0);
	}

	if (com.equals( "SQUARE"))
	{
		Keyboard.press(KEY_LEFT_ARROW);
		Keyboard.release(KEY_LEFT_ARROW);
	}

	if (com.equals("TRIANGLE"))
	{
		Keyboard.press(KEY_UP_ARROW);
		Keyboard.release(KEY_UP_ARROW);
	}

	if (com.equals("CIRCLE"))
	{
		Keyboard.press(KEY_RIGHT_ARROW);
		Keyboard.release(KEY_RIGHT_ARROW);
	}

	if (com.equals("CROSS"))
	{
		Keyboard.press(KEY_DOWN_ARROW);
		Keyboard.release(KEY_DOWN_ARROW);
	}

	if (com.equals("START"))
	{
		Keyboard.press(KEY_ESC);
		Keyboard.release(KEY_ESC);
		delay(1000);
	}

	if (com.equals( "L1"))
	{
		Keyboard.press('Q');
		Keyboard.release('Q');
	}

	if (com.equals("L2"))
	{
		Keyboard.press('E');
		Keyboard.release('E');
	}

	if (com.equals("UP"))
	{
		Keyboard.press(' ');
		Keyboard.release(' ');
	}

}

And here’s for the Mega:

#include <usbhub.h>
#include <PS3USB.h>

USB Usb;

PS3USB PS3(&Usb);



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

	while (!Serial2);
	if (!Usb.Init() == -1)
	{
		Serial.println("OSC did not start :(");
		while (1);
	}

	
}

void loop()
{
	Usb.Task();
	if (PS3.PS3Connected) 
	{

		//						Processes Left Joystick
		if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117)
		{
			Serial2.print("LHX ");
			Serial2.println(PS3.getAnalogHat(LeftHatX), DEC);
			Serial.print("LHX ");
			Serial.println(PS3.getAnalogHat(LeftHatX), DEC);
		}
		
		if (PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117)
		{
			Serial2.print("LHY ");
			Serial2.println(PS3.getAnalogHat(LeftHatY), DEC);
			Serial.print("LHY ");
			Serial.println(PS3.getAnalogHat(LeftHatY), DEC);
		}
     // etc.
}

I used for the Mega’s code the “USB Host Library v2”.

Is there something wrong with my code?

Thank you for the support!

Have you the GNDs on the two boards connected?

I suggest you write a very short program for the Leonardo that just receives the data and prints it to the Serial Monitor.

The examples in serial input basics may be useful.

...R