Interrupts Interference

Hi, I am working with TSL235 light to frequency converter sensors using an Arduino MEGA ADK. When working with one sensor at a time there is no problem with sensing. When two sensors are simultaneously connected they interfere with each other. How can i fix this problem ?, I am attaching the code being used for the simultaneous reading of two TSL235 sensors. Regards. Alejandro.

define TSL235Three 20 // Out of TSL235 connected to Digital pin 21

define TSL235One 3 // Out of TSL237 connected to Digital pin 3

int period = 1000; // Miliseconds of each light frecuency measurement

unsigned long counterThree = 0; // Counter of measurements during the test
unsigned long counterOne = 0; // Counter of measurements during the test

unsigned long currentTime = millis();
unsigned long startTime = currentTime;

volatile long pulsesThree = 0; // Counter of measurements of the TSL235R
volatile long pulsesOne = 0; // Counter of measurements of the TSL235R

unsigned long frequencyThree; // Read the frequency from the digital pin (pulses/second)
unsigned long frequencyOne; // Read the frequency from the digital pin (pulses/second)

void setup()
{

Serial.begin(115200); // Start and configure the serial port
attachInterrupt(3, PulseCountThree, RISING);
attachInterrupt(1, PulseCountOne, RISING);
pinMode(TSL235Three, INPUT); // Declare the pin such as an input of data
pinMode(TSL235One, INPUT); // Declare the pin such as an input of data
setup_parallax();
}

void loop()
{
counterOne++; // Increase the number of measurement
getfrequencyOne(); // Request to measure the frequency
pulsesOne = 0; // reset the pulses counter

//delay(.000025); // delay for measurements between the two for 1/250/16
counterThree++; // Increase the number of measurement
getfrequencyThree(); // Request to measure the frequency
pulsesThree = 0; // reset the pulses counter

parallax_output();
}

void PulseCountThree()
{
pulsesThree++;
}

void PulseCountOne()
{
pulsesOne++;
}

void parallax_output()
{

Serial.print("DATA,DATE,TIME,");
Serial.print(frequencyOne); Serial.print(","); Serial.println(frequencyThree);

}

void setup_parallax()
{
Serial.println("LABEL,Date,Time,Frequency One (Pin 3), Frequency Three (Pin 20)");
}

unsigned long getfrequencyOne ()
{

noInterrupts();
frequencyOne = pulsesOne *1000 / period; // Calculate the frequency (pulses/second)
interrupts();
return (frequencyOne);

}

unsigned long getfrequencyThree ()
{

noInterrupts();
frequencyThree = pulsesThree *1000 / period; // Calculate the frequency (pulses/second)
interrupts();
return (frequencyThree);

}

Please edit your post to include code tags ("<>" button).

When two sensors are simultaneously connected they interfere with each other.

In what way? Describe what happens.

Two interrupts cannot be handled simultaneously, one must wait for the other to finish.

You can't reset the counters like that, they are volatile, you must only read or write
them with interrupts disabled.

In fact just clearing them isn't always going to get correct answers because they may
increment between reading them and clearing them.

The best solution for you code is probably to clear them in the getFrequency methods, which
can also read the time and thus use the correct period value:

unsigned long last_one_time = 0L ;

unsigned long getfrequencyOne ()
{
  noInterrupts();
  unsigned long pulses = pulsesOne ;  // just read the value and clear it - minimum time possible
  pulsesOne = 0L ;   // without interrupts
  interrupts () ;
  unsigned long now = micros () ;  // determine actual period accurately
  unsigned long period = now - last ;
  last = now ;
  return  pulses * 1000000L / period; // Calculate the frequency (pulses/second)
}