// Defines
#define allDataBits 36 // Number of data bits to expect
// isrFlags bit numbers
#define F_HAVE_DATA 1 // 0=Nothing in read buffer, 1=Data in read buffer
#define F_GOOD_DATA 2 // 0=Unverified data, 1=Verified (2 consecutive matching reads)
#define F_CARRY_BIT 3 // Bit used to carry over bit shift from one long to the other
#define F_STATE 7 // 0=Sync mode, 1=Data mode
// Constants
const unsigned long sync_MIN = 8000; // Minimum Sync time in micro seconds
const unsigned long sync_MAX = 10000;
const unsigned long bit1_MIN = 3000;
const unsigned long bit1_MAX = 5000;
const unsigned long bit0_MIN = 1000;
const unsigned long bit0_MAX = 2800;
const unsigned long glitch_Length = 0; // Anything below this value is a glitch and will be ignored.
// Interrupt variables
unsigned long fall_Time = 0; // Placeholder for microsecond time when last falling edge occured.
unsigned long rise_Time = 0; // Placeholder for microsecond time when last rising edge occured.
byte bit_Count = 0; // Bit counter for received bits.
unsigned long build_Buffer[] = {0,0}; // Placeholder last data packet being received.
volatile unsigned long read_Buffer[] = {0,0}; // Placeholder last full data packet read.
volatile byte isrFlags = 0; // Various flag bits
void PinChangeISR0(){ // Pin 3 (Interrupt 0) service routine
unsigned long Time = micros(); // Get current time
if (digitalRead(2) == LOW) {
// Falling edge
if (Time > (rise_Time + glitch_Length)) {
// Not a glitch
Time = micros() - fall_Time; // Subtract last falling edge to get pulse time.
fall_Time = micros();//-Time; // Store fall time
}
else {
// Rising edge
if (Time > (fall_Time + glitch_Length)) {
// Not a glitch
rise_Time = Time; // Store rise time
}
}
}
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
return;
}
void PinChangeISR1(){ // Pin 2 (Interrupt 0) service routine
unsigned long Time = micros(); // Get current time
if (digitalRead(3) == LOW) {
// Falling edge
if (Time > (rise_Time + glitch_Length)) {
// Not a glitch
Time = micros() - fall_Time; // Subtract last falling edge to get pulse time.
fall_Time = micros();//-Time; // Store fall time
}
else {
// Rising edge
if (Time > (fall_Time + glitch_Length)) {
// Not a glitch
rise_Time = Time; // Store rise time
}
}
}
digitalWrite(13,HIGH);
delay(30);
digitalWrite(13,LOW);
return;
}
void setup()
{
pinMode(13,OUTPUT); // Used for debugging
Serial.begin(115200);
pinMode(2,INPUT);
Serial.println(F("ISR Pin 2 Configured For Input."));
attachInterrupt(0,PinChangeISR0,CHANGE);
Serial.println(F("Pin 2 ISR Function Attached. Here we go."));
Serial.println(F("ISR Pin 2 Configured For Input."));
attachInterrupt(1,PinChangeISR1,CHANGE);
Serial.println(F("Pin 3 ISR Function Attached. Here we go."));
}
void loop()
{
// Serial.begin(115200);
// Serial.println("Pino 2 == 1");//-rise_Time);
//Serial.println(rise_Time);
// delay(1);
}