Hi,
I am trying to implement a very simple program to record the time of switch changes using interrupts for rising and falling inputs on digital pins 2 and 3 and then send the switch change ON or OFF and the time in ms via a bluetooth shield to a Windows PC. When the switch changes are slow there are no problems but as soon as I start to press the switch rapidly the serial data being received develops problems e.g.
ON3226
OFF3915
ON4552
ON5067
ON8162
OFF8670
ON9650
It seems to me that the interrupts are firing before the sequence of bt.print statements complete so some events are not being transmitted. I tried sending both switch status and time in the one bt.println statement
bt.println("ON" + switchtime);
or
bt.println("OFF" + switchtime);
but the data received was indecipherable. Any assistance is greatly appreciated.
#include <SoftwareSerial.h>
SoftwareSerial bt(6, 7); // RX, TX
int pbIn1 = 0; // Interrupt 0 is on DIGITAL PIN 2!
int pbIn2 = 1; // Interrupt 1 is on DIGITAL PIN 3!
volatile boolean state = false; // The input state toggleFALSE=LOW and TRUE=HIGH
volatile unsigned long switchtime = 0; // Time of latest switch change
volatile unsigned long lastswitchtime = 0; // Time of previous switch change
void setup()
{
// Set up the digital pins 2 and 3 to an Interrupt need to pins to get interupts on HI and LO changes
pinMode(pbIn1, INPUT); //make it an input
pinMode(pbIn2, INPUT); //make it an input
//Attach the interrupt to the input pin1 and monitor for Change HIGH
attachInterrupt(pbIn1, stateChangeHigh, RISING);
//Attach the interrupt to the input pin2 and monitor for Change LOW
attachInterrupt(pbIn2, stateChangeLow, FALLING);
// initialize serial communication at 9600 bits per second:
bt.begin(9600);
}
void loop()
{
//if switchtime changes then print to BT serial
if (switchtime != lastswitchtime)
{
if (state) { bt.print("ON"); }
else { bt.print("OFF"); }
bt.println(switchtime);
switchtime = lastswitchtime;
}
}
void stateChangeHigh()
{
switchtime = millis();
state = true;
}
void stateChangeLow()
{
switchtime = millis();
state = false;
}
int pbIn1 = 0; // Interrupt 0 is on DIGITAL PIN 2!
...
pinMode(pbIn1, INPUT); //make it an input
You just said it was pin 2!
When the switch changes are slow there are no problems but as soon as I start to press the switch rapidly the serial data being received develops problems e.g.
SoftwareSerial turns interrupts off.
attachInterrupt uses interrupt numbers NOT pin numbers!
Mark
Thanks for the rapid replies 
I am using an Arduino Uno and interrupts 0 and 1 are only available on IO pins 2 and 3. The switch is attached to both so that I can detect RISING on pin2 and interrupt 0 fires not problem. I detect FALLING on pin3 and interrupt 1 fires no problem. The interrupts are working fine but if they fire during the IF statement which transmits the data using println the text is not sent. 
One solution I thought might work is store the switch states and times in an array and then transmit within the loop. This should result in no lost switch changes but I think the disrupted transmit may still occur.
pinMode(pbIn1, INPUT); //make it an input
pinMode(pbIn2, INPUT); //make it an input
The above sets pins 0 and 1 as inputs not pins 2 and 3.
Mark
Sorry Mark. Yes, error on my part. I have changed to
pinMode(2, INPUT); //make it an input
pinMode(3, INPUT); //make it an input
However this does not affect the operation of the program as pin 2 and 3 are inputs by default.
I have checked and believe the 0 and 1 interrupts are setup correctly as they are on pins 2 and 3 for the Uno.
It could be that the problem is due to switch bounce and the interrupts being triggered multiple times during a single pass through the Loop {}
The interrupts are working fine but if they fire during the IF statement which transmits the data using println the text is not sent
SoftwareSerial turns interrupts off.
When Nick Gammon speaks, you should listen.
It could be that the problem is due to switch bounce and the interrupts being triggered multiple times during a single pass through the Loop
It's quite possible that you have switch bounce which can give you multiple unexpected readings, but if interrupts are disabled during the if loop you won't see them.
Sincere apologies to Nick Gammon :-X
As my application involves very slow switch changes with minimum of 50-100 ms between transitions I have removed the interrupts and just gone with polling in the main loop. I added a software correction for switch bounce by requiring a minimum 10ms difference between switch changes. I also added counts for ON and OFF switch changes to make it easier for trouble shooting and determine if it gets out of synch. Seems to be holding up well.
Thanks to those who responded. Much appreciated.
#include <SoftwareSerial.h>
SoftwareSerial bt(6, 7); // RX, TX
const int SWITCH = 2; // The input switch is connected to pin 2
boolean lastSwitch = LOW; //variable containing previous switch state
boolean currentSwitch = LOW; //variable containing current switch state
boolean state = false; // The input state toggleFALSE=LOW and TRUE=HIGH
unsigned long switchtime = 0; // Time of latest switch change
unsigned long lastswitchtime = 0; // Time of previous switch change
int OnCount = 0; //counts the ON switches
int OffCount = 0; //counts the OFF switches
void setup()
{
// Set up digital pin 2 to be an input
pinMode(SWITCH, INPUT); //make it an input
// initialize serial communication at 9600 bits per second:
bt.begin(9600);
}
void loop()
{
currentSwitch = digitalRead(SWITCH);
if (currentSwitch != lastSwitch) //change of switch status
{
switchtime=millis();
if (switchtime - lastswitchtime >10) //try to ignore switch bounce
{
if (currentSwitch == HIGH) { OnCount ++; bt.print(OnCount); bt.print(" ON "); bt.println(switchtime); }
if (currentSwitch == LOW) { OffCount ++; bt.print(OffCount); bt.print(" OFF "); bt.println(switchtime); }
switchtime = lastswitchtime;
lastSwitch=currentSwitch;
}
}
}