confused on using 2 serial ports on a arduino-mega at the same.

I am making a digital interface for an amplifier I have made and want to control it using a remote control and my android phone. The IR sensor sends the data via serial 1 ( pin 19 or RX1 ) I used this tutorial from ladyada to get the IR remote to work perfectly ( Sensor tutorials - IR remote receiver/decoder tutorial ). I am also using a bluesmirf module to receive the Bluetooth data from my android phone and sent it to the arduino via serial 0 ( Pins 0 and 1 or RX0 and TX0) and this works perfectly as well. However, when I combine them together weird things happen. ( see picture for rough schematic )
Here is what happens, the android commands don't work until something has been pressed on the remote haha. So when the remote button is pressed it does exactly what it needs to do. However, if a phone command was given first it does that really really fast and then covers it up with the remote control command.

In my loop function I only have 2 functions, one that reads the android phone and one that reads the IR sensor in that order.

Is there a way to give priority to serial 0? Maybe disable serial 1 when data is received on serial 0? making the android phone dominate.

one last thing, I dont understand 100% ladyada's IR code so maybe im just misunderstanding what its doing. Maybe her code is continuosly runing and not giving the android a chance to communicate? I'm not sure haha. I'm gunna read over it more.

Thanks for any help in advance! :slight_smile:

EDIT: from some reason it wont let me post the picture

The IR sensor sends the data via serial 1

This doesn't make sense. The IR sensor is read by the Arduino. It does not have the ability to send serial data.

In my loop function I only have 2 functions, one that reads the android phone and one that reads the IR sensor in that order.

But, I'm not going to show you either one.

Is there a way to give priority to serial 0? Maybe disable serial 1 when data is received on serial 0? making the android phone dominate.

Serial data is buffered. Which one you read first is up to you.

EDIT: from some reason it wont let me post the picture

The forum didn't stop you posting your code, though. So, where is it?

Wow PaulS Thanks! That was a really constructive reply! You should win some sort of award for it!

#include <MeetAndroid.h>
#include <SPI.h>
#include "ircodes.h"

/* SPI Defines */
#define DATAIN        50  //MISO
#define DATAOUT       51  //MOSI
#define SPICLOCK      52  //SCK
#define SLAVESELECT   53  //ss

#define IRpin_PIN      PIND
#define IRpin          2

// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 9000

// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20 

// What percent we will allow in variation to match the same code
#define FUZZINESS 20

// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing

// declare MeetAndroid so that you can call functions with it
MeetAndroid meetAndroid;

// we need 3 PWM pins to control the leds
int redLed = 9;   
int greenLed = 10;
int blueLed = 11;

void setup()  
{

  // use the baud rate your bluetooth module is configured to 
  // not all baud rates are working well, i.e. ATMEGA168 works best with 57600
  Serial.begin(57600); 
  
  // register callback functions, which will be called when an associated event occurs.
  meetAndroid.registerFunction(red, 'o');
  meetAndroid.registerFunction(green, 'p');  
  meetAndroid.registerFunction(blue, 'q');
  meetAndroid.registerFunction(TVb, 'r');
  meetAndroid.registerFunction(MP3b, 's');
  meetAndroid.registerFunction(AUX1b, 't');
  meetAndroid.registerFunction(AUX2b, 'u');

  // set all color leds as output pins
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);

  
  // just set all leds to high so that we see they are working well
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, HIGH);
  digitalWrite(blueLed, HIGH);

  pinMode(SLAVESELECT,OUTPUT);
  SPI.setDataMode(0);
  SPI.setBitOrder(MSBFIRST); 
  SPI.begin();


  
  //Serial.begin(9600);

}

void loop()
{
   // you need to keep this in your loop() to receive events
   meetAndroid.receive(); 
  /*   IR code  */
   ReadIR();
}

/*
 * Whenever the multicolor lamp app changes the red value
 * this function will be called
 */
void red(byte flag, byte numOfValues)
{
  analogWrite(redLed, meetAndroid.getInt());
}

/*
 * Whenever the multicolor lamp app changes the green value
 * this function will be called
 */
void green(byte flag, byte numOfValues)
{
  analogWrite(greenLed, meetAndroid.getInt());
}

/*
 * Whenever the multicolor lamp app changes the blue value
 * this function will be called
 */
void blue(byte flag, byte numOfValues)
{
  analogWrite(blueLed, meetAndroid.getInt());
}
void TVb(byte flag, byte numOfValues)
{
  if(meetAndroid.getInt())
  {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x01);
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    Serial.println("BUT1");
  }
}
void MP3b(byte flag, byte numOfValues)
{
  if(meetAndroid.getInt()){
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x02);
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    Serial.println("BUT2");
  }
}
void AUX1b(byte flag, byte numOfValues)
{
  if(meetAndroid.getInt()){
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x04);
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    Serial.println("BUT3");
  }
}
void AUX2b(byte flag, byte numOfValues)
{
  if(meetAndroid.getInt()){
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x08);
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);
    Serial.println("BUT4");
  }
}

/* ----- IR Functions -------*/

boolean IRcompare(int numpulses, int Signal[]) {
  
  for (int i=0; i< numpulses-1; i++) {
    int oncode = pulses[i][1] * RESOLUTION / 10;
    int offcode = pulses[i+1][0] * RESOLUTION / 10;
    
    /*
    Serial.print(oncode); // the ON signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 0]); // the ON signal we want 
    */
    
    // check to make sure the error is less than FUZZINESS percent
    if ( abs(oncode - Signal[i*2 + 0]) <= (Signal[i*2 + 0] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    /*
    Serial.print("  \t"); // tab
    Serial.print(offcode); // the OFF signal we heard
    Serial.print(" - ");
    Serial.print(Signal[i*2 + 1]); // the OFF signal we want 
    */
    
    if ( abs(offcode - Signal[i*2 + 1]) <= (Signal[i*2 + 1] * FUZZINESS / 100)) {
      //Serial.print(" (ok)");
    } else {
      //Serial.print(" (x)");
      // we didn't match perfectly, return a false match
      return false;
    }
    
    //Serial.println();
  }
  // Everything matched!
  return true;
}

int listenForIR(void) {
  currentpulse = 0;
  
  while (1) {
    uint16_t highpulse, lowpulse;  // temporary storage timing
    highpulse = lowpulse = 0; // start out with no pulse length
  
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
       // pin is still HIGH

       // count off another few microseconds
       highpulse++;
       delayMicroseconds(RESOLUTION);

       // If the pulse is too long, we 'timed out' - either nothing
       // was received or the code is finished, so print what
       // we've grabbed so far, and then reset
       if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    // we didn't time out so lets stash the reading
    pulses[currentpulse][0] = highpulse;
  
    // same as above
    while (! (IRpin_PIN & _BV(IRpin))) {
       // pin is still LOW
       lowpulse++;
       delayMicroseconds(RESOLUTION);
       if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
         return currentpulse;
       }
    }
    pulses[currentpulse][1] = lowpulse;

    // we read one high-low pulse successfully, continue!
    currentpulse++;
  }
}

void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
  
  // print it in a 'array' format
  Serial.println("int IRsignal[] = {");
  Serial.println("// ON, OFF (in 10's of microseconds)");
  for (uint8_t i = 0; i < currentpulse-1; i++) {
    Serial.print("\t"); // tab
    Serial.print(pulses[i][1] * RESOLUTION / 10, DEC);
    Serial.print(", ");
    Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC);
    Serial.println(",");
  }
  Serial.print("\t"); // tab
  Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC);
  Serial.print(", 0};");
}

void ReadIR(){

int numberpulses;
  
  numberpulses = listenForIR();

  if (IRcompare(numberpulses, IRsignalPower)) {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x00);
    digitalWrite(SLAVESELECT,HIGH);
    delay(1);
  }
  if (IRcompare(numberpulses, IRsignal1)) {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x01);
    digitalWrite(SLAVESELECT,HIGH);
    delay(1);
  }
  if (IRcompare(numberpulses, IRsignal2)) {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x02);
    digitalWrite(SLAVESELECT,HIGH);
    delay(1);
  }
  if (IRcompare(numberpulses, IRsignal3)) {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x04);
    digitalWrite(SLAVESELECT,HIGH);
    delay(1);
  }
  if (IRcompare(numberpulses, IRsignal4)) {
    digitalWrite(SLAVESELECT,LOW);
    SPI.transfer(0x08);
    digitalWrite(SLAVESELECT,HIGH);
    delay(1);
  }

}

OK, now that we've seen your code, we can address your issues.

The IR sensor sends the data via serial 1

The Serial1 instance is referenced nowhere in your code, so this statement is false.

In my loop function I only have 2 functions, one that reads the android phone and one that reads the IR sensor in that order.

One that deals with the android phone's requests, if any have arrived, and one that blocks, waiting for an IR signal to arrive. That's a big difference.

There is an excellent IR library (GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols) that uses interrupts to receive the IR data, so it is non-blocking. I think that that should be what you are using.