I have a PYQ 1548 motion sensor (datasheet). I try to write the config, but can't be sure I'm doing it right:
#define PIN_DLINK 8
#define PIN_SER 20
void setup() {
Serial.begin(115200);
while (!Serial);
delay(100);
int threshold = 1;
int blindTime = 2;
int pulseCounter = 2;
int windowTime = 3;
int operationModes = 2;
int signalSource = 0;
int reserved1 = 2;
int hpfCutoff = 0;
int reserved2 = 0;
int pulseDetectionMode = 1;
int value = 0
+ (threshold << 17)
+ (blindTime << 13)
+ (pulseCounter << 11)
+ (windowTime << 9)
+ (operationModes << 7)
+ (signalSource << 5)
+ (reserved1 << 3)
+ (hpfCutoff << 2)
+ (reserved2 << 1)
+ (pulseDetectionMode << 0);
pinMode(PIN_SER, OUTPUT);
for (int i = 23; i >= 0; i--) {
writeBit((value >> i) & 0x1);
Serial.print(i);
Serial.print(": ");
Serial.println((value >> i) & 0x1);
}
pinMode(PIN_DLINK, INPUT);
}
void loop () {
delay(250);
if (digitalRead(PIN_DLINK)) {
Serial.println("Interrupt!");
// Reset interrupt.
pinMode(PIN_DLINK, OUTPUT);
digitalWrite(PIN_DLINK, LOW);
delayMicroseconds(150);
pinMode(PIN_DLINK, INPUT);
}
}
void writeBit (char bit) {
digitalWrite(PIN_SER, HIGH);
delayMicroseconds(2);
digitalWrite(PIN_SER, bit ? HIGH : LOW);
delayMicroseconds(72);
digitalWrite(PIN_SER, LOW);
delayMicroseconds(2);
}
The interrupt is never triggered, I'm guessing because the config isn't written correctly to tell the sensor to generate an interrupt (operationModes=2
). Serial output is:
23: 0
22: 0
21: 0
20: 0
19: 0
18: 0
17: 1
16: 0
15: 0
14: 1
13: 0
12: 1
11: 0
10: 1
9: 1
8: 1
7: 0
6: 0
5: 0
4: 1
3: 0
2: 0
1: 0
0: 1
I also tried to read from it, but what I read seems to be junk:
#define PIN_DLINK 8
void setup() {
Serial.begin(115200);
while (!Serial);
delay(100);
pinMode(PIN_DLINK, INPUT);
}
void loop () {
delay(250);
pinMode(PIN_DLINK, OUTPUT);
digitalWrite(PIN_DLINK, HIGH);
delayMicroseconds(120);
digitalWrite(PIN_DLINK, LOW);
bool normal = readBit() == 1;
int adc = 0;
for (int i = 13; i >= 0; i--)
adc += readBit() << i;
int config = 0;
for (int i = 23; i >= 0; i--)
config += readBit() << i;
Serial.print(normal ? "normal" : "reset");
Serial.print(", adc: ");
Serial.print(adc);
Serial.print(", config: ");
Serial.println(config);
}
char readBit () {
delayMicroseconds(2);
digitalWrite(PIN_DLINK, HIGH);
delayMicroseconds(2);
pinMode(PIN_DLINK, INPUT);
delayMicroseconds(18);
char value = digitalRead(PIN_DLINK);
pinMode(PIN_DLINK, OUTPUT);
digitalWrite(PIN_DLINK, LOW);
return value;
}
Serial output trying to read:
normal, adc: 16382, config: 0
normal, adc: 128, config: 0
normal, adc: 192, config: 0
normal, adc: 16378, config: 0
normal, adc: 16360, config: 0
normal, adc: 16378, config: 0
normal, adc: 0, config: 0
normal, adc: 0, config: 0
normal, adc: 128, config: 0
normal, adc: 0, config: 0
normal, adc: 480, config: 0
normal, adc: 320, config: 0
normal, adc: 16372, config: 0
normal, adc: 16368, config: 0
normal, adc: 16376, config: 0
normal, adc: 384, config: 0
normal, adc: 256, config: 0
normal, adc: 192, config: 0
normal, adc: 0, config: 0
normal, adc: 0, config: 0
normal, adc: 0, config: 0
normal, adc: 16383, config: 0
Any ideas what I'm doing wrong? I could not find any examples or libraries for this sensor.