difficulty combining sketches

Hi,

I am working with a flex/stretch sensor that I've attached to a belt to detect breathing, and an RFID reader with a number of different RFID tags. I wrote two sketches separately for the breathing belt and the RFID reader, and both were working fine. When I tried to combine them, I had some issues:

-the values that I see for the breath sensor seem completely random.
-an occasional delay in the reading of the RFID tag. Usually the first tag is fine, but subsequent readings are not timed reliably. (I was expecting some issues in trying to run these two functions in parallel, so I also tried using interrupt, but without any success. The values from the RFID reader never show up at all.)

This is the code I wrote for the breath sensor:

int sensorPin = 0;
int rawValue;

void setup()
{
 pinMode(sensorPin, INPUT);
 
  Serial.begin(9600);
//Serial.begin(115200);
}

void loop()
{
  rawValue = 
  analogRead(sensorPin);
 Serial.print((rawValue/4), BYTE);
// Serial.println(rawValue);
  delay(1000);
  
}

This is the code for the RFID reader

// RFID reader for Arduino 
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// Modified for Arudino by djmatic--and slightly modified by me


int  val = 0; 
char code[10]; 
int bytesread = 0; 

void setup() { 

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin  

 digitalWrite(2, LOW);                  // Activate the RFID reader 
}

 void loop() { 

  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
        
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
         
        } 
      } 
      if(bytesread == 10) {              // if 10 digit read is complete 
      
       // Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.print(code[9]);            // print the TAG code--just last number from array
// Serial.println(code);   
      } 
      bytesread = 0; 
           delay(1000);                       // wait for a second 
    } 
  } 
}

This is the code in which I tried to combine the examples without interrupt. Here I got random values from the breath sensor, and would occasional get an RFID tag reading, but often with delays because of the parallel issue. As I was trying to troubleshoot it seemed like maybe the breath sensor problems were related to the pin 2 (output of the rfid reader), which doesn't make a lot of sense to me.

//current issue: seems to get random reads off of sensor in this version, where I am trying to combine two sketches

// RFID reader for Arduino 
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// Modified for Arudino by djmatic

#include <NewSoftSerial.h>
#define rxPin 3
#define txPin 4
NewSoftSerial RFID(rxPin, txPin);

int rfidPin = 2;
int sensorPin = A0;
int rawValue;
int  val = 0; 
char code[10]; 
int bytesread = 0; 

void setup() { 
 pinMode(sensorPin, INPUT);
 
Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 2400bps   
RFID.begin(2400);
pinMode(rxPin, INPUT);


pinMode(rfidPin, OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin  

digitalWrite(rfidPin, LOW);                  // Activate the RFID reader 
}

 void loop() { 
   
    rawValue = 
  analogRead(sensorPin);
  Serial.print((rawValue/4), BYTE);
  delay(1000);

  if(RFID.available() > 0) {          // if data available from reader 
    if((val = RFID.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( RFID.available() > 0) { 
          val = RFID.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
        
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
         
        } 
      } 
      if(bytesread == 10) {              // if 10 digit read is complete 
      
       // Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.print(code[9]);            // print the TAG code 
// Serial.println(code);   
      } 
      bytesread = 0; 
           //delay(1000);                       // wait for a second 
    } 
  } 
  
   
}

This is the example in which I tried to use an interrupt--this didn't work at all: I had random values coming in from the breath sensor, and no values at all coming in from the RFID tags. I haven't used interrupt before....

#include <NewSoftSerial.h>
#define rxPin 3
#define txPin 4
NewSoftSerial RFID(rxPin, txPin);

int rfidPin = 2;
int sensorPin = A0;
int rawValue;
int  val = 0; 
char code[10]; 
int bytesread = 0; 

void setup() { 
 
 attachInterrupt(1, rfid, CHANGE);
 
 Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 2400bps   
 RFID.begin(2400);
 pinMode(sensorPin, INPUT);
 pinMode(rxPin, INPUT);
 pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin  
 digitalWrite(2, LOW);                  // Activate the RFID reader 
}


void loop()
{
  rawValue = 
  analogRead(sensorPin);
 Serial.print((rawValue/4), BYTE);
// Serial.println(rawValue);
  delay(1000);
  
}

void rfid()
{ 

  if(Serial.available() > 0) {          // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
        
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit 
         
        } 
      } 
      if(bytesread == 10) {              // if 10 digit read is complete 
      
       // Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.print(code[9]);            // print the TAG code 
// Serial.println(code);   
      } 
      bytesread = 0; 
         //  delay(1000);                       // wait for a second 
    } 
  } 
}

Thank you so much for your help!

Start with the sketch that worked for the RFID reader. Change the TX and RX pin connections to pins 3 and 4, and change to using NewSoftSerial to read, with Serial being used to output the data.

Get that to work. Adding code to read the sensor should then be trivial. The delay in reading the RFID tags is caused by your delay() call. Look at the Blink Without Delay example for an idea of how to send data to the serial port at specific intervals without the need to use delay().

Of course, the value of sending the sensor output to the serial port escapes me. For that matter, the whole relationship between a force sensor and an RFID tag reader escapes me.

Thanks for your suggestions. I do have the sketches working separately (including using the NewSoftSerial library to use pins 3 and 4 for tx and rx). But, as I mentioned, the main problem in combining them seemed to be that the RFID reader was somehow interfering with the breath-sensor. In any case, I will take a look later at the example you suggested, and keep troubleshooting later...

The value of sending the data output to the serial port is that it is controlling specific elements of the sound in supercollider based on the breathing of a dancer...eventually the rfid reader/tags will control timbre based on contact between dancers. Anyway for now it is very much a prototype.

If anyone has any other ideas of any other problems I might be missing, I'd be very grateful! Thanks.