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!