Ich arbeite gerade an meinem MagicMirror.
Ein Gestensensor (APDS9960) und ein PIR-Sensor werkeln an einem Arduino und senden ihre Daten per USB an einen Raspi 3.
Hier der Arduino Code:
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// PINS
// gesture sensor interrup pin
#define APDS9960_INT 2 // Needs to be an interrupt pin
#define LED_PIN 13 // choose the pin for the LED
#define PIR_PIN 3 // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
// GLOBAL VARIABLES
// gesture sensor
SparkFun_APDS9960 gestureSensor = SparkFun_APDS9960();
// STATE
// mirror is in use or not (someone stands in front of it)
boolean inUse = false;
// interrupt was received and gesture should be read from sensor
int isr_flag = 0;
// setup function is called on boot
void setup() {
// Set interrupt pin as input for gesture sensor
pinMode(APDS9960_INT, INPUT);
pinMode(LED_PIN, OUTPUT); // declare LED pin as output
pinMode(PIR_PIN, INPUT); // declare PIR sensor pin as input
Serial.begin(9600);
digitalWrite(LED_PIN, LOW); // turn LED OFF
// Initialize gesture sensor APDS-9960 (configure I2C and initial values)
if (gestureSensor.init()) {
Serial.println(F("INFO: APDS-9960 initialization complete"));
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine1, FALLING);
enableGestureSensor();
}
else {
Serial.println(F("ERROR: Something went wrong during APDS-9960 init!"));
}
}
// main loop that is called repeatedly after setup
void loop() {
pirState = digitalRead(PIR_PIN); // read input value
digitalWrite(LED_PIN, pirState); // turn LED ON or OFF
if (!inUse && pirState == HIGH) {
enableGestureSensor();
}
else if (inUse && pirState == LOW) {
disableGestureSensor();
}
// get gestures
if (inUse) {
// check if there was an interrupt in the meantime
handleInterrupt();
}
// wait before checking distance again and activate gesture sensor
else {
// wait 50ms until next measurement
delay(50);
}
}
void enableGestureSensor() {
inUse = true;
Serial.println(F("Person: PRESENT"));
if (gestureSensor.enableGestureSensor(true)) {
Serial.println(F("INFO: Gesture sensor is now running"));
}
else {
Serial.println(F("ERROR: Something went wrong during gesture sensor enable!"));
}
}
void disableGestureSensor() {
inUse = false;
Serial.println(F("Person: AWAY"));
if (gestureSensor.disableGestureSensor()) {
Serial.println(F("INFO: Gesture sensor is now disabled"));
}
else {
Serial.println(F("ERROR: Something went wrong during gesture sensor disable!"));
}
}
// check for interrupt, if one is present it means gesture is present,
// in that case stop interrupt handler, handle gesture and attach interrupt handler again
void handleInterrupt() {
// if interrupt was set, read and print gesture, reset interrupt flag
if (isr_flag == 1) {
detachInterrupt(digitalPinToInterrupt(APDS9960_INT));
handleGesture();
isr_flag = 0;
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine1, FALLING);
}
}
// interrupt function for interrupt pin that APDS9960 is attached to
void interruptRoutine1() {
isr_flag = 1;
}
// on gestor sensor interrupt send serial message with gesture
void handleGesture() {
if (gestureSensor.isGestureAvailable()) {
switch (gestureSensor.readGesture()) {
case DIR_UP:
Serial.println(F("Gesture: UP"));
break;
case DIR_DOWN:
Serial.println(F("Gesture: DOWN"));
break;
case DIR_LEFT:
Serial.println(F("Gesture: LEFT"));
break;
case DIR_RIGHT:
Serial.println(F("Gesture: RIGHT"));
break;
case DIR_NEAR:
Serial.println(F("Gesture: NEAR"));
break;
case DIR_FAR:
Serial.println(F("Gesture: FAR"));
break;
default:
Serial.println(F("Gesture: NONE"));
}
}
}
Mein Problem:
Ich bewege mich in den Erfassungsbereich des PIR. Dabei geht der PIR_PIN auf HIGH und der Gestensensor wird aktiviert. Dann wartet das Programm auf eine Geste (UP, DOWN, LEFT, RIGHT, FAR, AWAY, NONE). In dem Moment steht aber das Programm und pirState == LOW tritt niemals ein, bevor ich nicht mit der Hand mal am APD vorbeigehuscht bin.
Die LED geht nicht aus, obwohl die Zeit des PIR schon längst abgelaufen ist. Eine Geste vor dem ADP und schon geht die LED aus.
Wie kann ich diesen Interrupt austricksen?
Bin für jede Hilfe dankbar