Arduino mega and Arduino Leonardo Communication

I have been working on a project where the following steps have to be carried out.

  1. Receive data on the Bluetooth device onto Arduino mega. (DONE)

  2. Compute the data and according to the output make if-else statements. (DONE)

  3. The if-else statements has a particular character to be send to ARDUINO LEONARDO if the condition is true.

  4. Receive the character from Arduino Mega onto Arduino Leonardo and do further actions on Leonardo.

Now I have already completed the first 2 steps but was unable to make the Arduino's communicate. I have to send only 1 character and not a string. The RX0 and TX0 on the Mega have already been used, so either I have to use SoftwareSerial or any other RX/TX.

I already went through Robin's post about Serial Communication but couldn't make it happen. http://forum.arduino.cc/index.php?topic=288234.0

Would be of great help if you guys can help me through.

I am suggesting to the Moderator to move your Thread to the Programming section.

Please post your program(s) so we can see exactly what you are trying.

If you have the Mega connected to the Leonardo then please provide a photo of a pencil drawing showing all the connections.

Both the Mega and Leonardo have spare HardwareSerial ports so there should be no need to use the much inferior SoftwareSerial.

...R

Thank you for doing the needful.

I have not yet connected the Leonardo to the Mega. Can you guide me how can I connect them both? I already have the Serial port 0 of Mega connected to Bluetooth. So RX0/TX0 are already used. I need to use the serial ports 1-3 to communicate. It would be of great help if you can help me. Thank you in advance.

I need to use the serial ports 1-3 to communicate.

So, what is the problem? The RXn and TXn pins are clearly called out on the Mega. The n in the name corresponds to the n in the Serialn instance to use.

Thank you for answering Paul.

I already tried doing that. I’ll post the code in a moment. I couldn’t send the data though.

Arduino Leonardo Code

#include <Mouse.h>

void setup() {
  Serial.begin(9600);
  // initialize mouse control:
  Mouse.begin();
}

void loop() {
  // use serial input to control the mouse:
  if (Serial.available() > 0) {
    char inByte = Serial.read();

    switch (inByte) {
      case 'w':
        // move mouse up
        Mouse.move(0, -8);
        break;
      case 's':
        // move mouse down
        Mouse.move(0, 8);
        break;
      case 'a':
        // move mouse left
        Mouse.move(-8, 0);
        break;
      case 'd':
        // move mouse right
        Mouse.move(8, 0);
        break;
      case 'e':
        // perform mouse left click
        Mouse.click(MOUSE_LEFT);
        break;
    }
  }
}

Arduino Mega Code

#include <Brain.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Brain brain(Serial);

int att = brain.readAttention();
int med = brain.readMeditation();
int switch2 = 53;

void setup()
{
  Serial.begin(57600);
  Serial1.begin(9600);
  lcd.begin(16, 2);
}
void loop()
{
  
  int switch2state = digitalRead(switch2);

  if (brain.update() && (brain.readSignalQuality() == 0))
  {
    Serial.println(brain.readCSV());

    lcd.setCursor(0, 0);
    lcd.print("Attention :  ");
    lcd.print(brain.readAttention());
    lcd.print(" ");

    lcd.setCursor(0, 1);
    lcd.print("Meditation:  ");
    lcd.print(brain.readMeditation());
    lcd.print(" ");

    if (switch2state == HIGH) 
    {
      Serial.println("Switch 2 ON");

      if (0 <brain.readAttention() < 25)
     {
        Serial1.println('a'); //left
      }

      if (26 < brain.readAttention() < 50)
     {
        Serial1.println('d'); //right
      }

      if (51 < brain.readAttention() < 75)
     {
        Serial1.println('w'); //up   
      }

      if (76 < brain.readAttention() < 100) 
     {
        Serial1.println('s'); //down        
      }

      if (brain.readMeditation() > 70) 
     {
        Serial1.println('e'); //click
      }
    }
  }
}

Arduino Leonardo Code

Why is there no Serial1.begin()? What is connected to the Serial instance(s) on the Lenny?

Arduino Mega Code

Which hasn't a hope in hell of compiling. Come on back when you have something that does.

    Serial.println(brain.readCSV());

This is absolutely pointless. You have the hardware connected to the main hardware serial instance. You can NOT use that instance to debug your code, too.

The Arduino Mega code already compiled Sir, and I am getting the readings too.

This code was later removed, my bad. I already have the reading on my LCD.

Serial.println(brain.readCSV());

On the lenny I have connected the Mega's TX1(pin 18) to it's RX pin.

And I am very new to this, so I am not sure if I have to use Serial1.begin() on lenny as well or not. I have already used Serial.begin() on it.

On the lenny I have connected the Mega’s TX1(pin 18) to it’s RX pin.

The Leonardo’s RX and TX pins are connected to its Serial1 instance, not its Serial instance.

     if (0 <brain.readAttention() < 25)

Compare 0 to the value returned by the readAttention() method. The result is either true (1) or false (0). the last time I checked, both true and false are less than 25, so this statement will always be true.

     if (26 < brain.readAttention() < 50)

True and false are less than 50, too. So, this statement will always be true.

     if (76 < brain.readAttention() < 100)

And this one.

Why was it necessary to call the readAttention() method three times?

Ghost_StarLord: I already have the Serial port 0 of Mega connected to Bluetooth. So RX0/TX0 are already used.

That seems silly to start with. Keep Serial0 free for debug messages to the Arduino Serial Monitor.

Connect your Bluetooth device to Serial1.

Then you can use Serial2 on the Mega and Serial1 on the Leonardo for communication between the two boards.

...R