I am trying to read a digital signal from my domotica system by using an arduino nano. I was able to read the signal without any problems using an atmega1284P and some bascom code. Now i would like to do the same using the arduino nano.
The signal (bittrain) i am trying to read starts with a long low pulse (between 3000 and 3200 microseconds long).
After that there is a signal of 22 bits. A short lowgoing signal is a 0 , a longer (more than 1000microseconds) is a 1
This signal is put 3 times on the domotica bus.
The routine is called from an interrupt activated in setup() : attachInterrupt(0, NB_get, RISING); (the domotica system is connected to pin 2)
The routine i try is:
unsigned long knop;
int NB_Bus_in = 2;
void NB_get() //starts when puls is going high
{ unsigned long puls;
detachInterrupt(0);
knop=0;
do
{ puls = pulseIn(NB_Bus_in, LOW,3600); // wait until pin gets low, then time the low pulse
}while (!((puls>3000)&&(puls<3200))&&!(puls==0)) ; // loop until start pulse is passed, is between300*10 and 320*10 long
if (puls>0) // no error
{ for (int NB_i=1;NB_i<=24;NB_i++)
{ puls = pulseIn(NB_Bus_in, LOW,3600);
knop=knop << 1;
if (puls>=1000&&puls!=0){knop|=0b1;}
}
} else goto einde;
Serial.println(knop,HEX); //for testing the result
einde:
attachInterrupt(0, NB_get, RISING);
}
The problem is that is get different results back for the same signal and never the correct one.
Someone told me to try some other code:
byte NB_data[8]={0x1A,0xDD,0xC0};
byte NB_bytes;
#define TimeStart 3080
#define TimeZero 650
#define TimeOne 1450
#define TimeOff 3600
// check if the time was in range +/- 25%
#define IS_X(t,x) ((t > 3*(x)/4) && (t < 5*(x)/4) && (t!=0))
#define IS_S(t) IS_X(t,TimeStart)
#define IS_0(t) IS_X(t,TimeZero)
#define IS_1(t) IS_X(t,TimeOne)
#define IS_E(t) IS_X(t,TimeOff)
void NB_get() //starts when puls is going high
{ unsigned long puls;
byte NB_i=0, NB_byte=0;
NB_bytes=0;
detachInterrupt(0);
do
{ puls = pulseIn(NB_Bus_in, LOW,3600); // wait until pin gets low, then time the low pulse
}while (!IS_S(puls)) ; // loop until start pulse is passed
if (puls>0) // no error
{do
{ puls = pulseIn(NB_Bus_in, LOW,3600);
if (IS_1(puls))NB_byte|=0b1;
NB_byte=NB_byte << 1;
if (NB_i==7)
{ Nikobus_data[NB_bytes++]=NB_byte;
NB_byte=0; NB_i=0;
} else NB_i++;
} while (!IS_E(puls)) ;
};
attachInterrupt(0, NB_get, RISING);
Serial.println(Nikobus_data[0]);
}
But this code simply crashes the arduino...
Can anyone help me to get this working?