Arduino(14): APDS9960 gesture sensor controls 8x8 LED direction lights

Following the previous article using xantorohara.github.io to generate the 8x8 LED matrix code of the direction arrow, continue to use the APS9960 gesture sensor as the direction control. When you wave your hand to the left, the direction light arrow moves to the left; When the gesture is swiped to the right, the direction of the arrow moves to the right.

At the beginning of writing the program, because of the readGesture() command, it will make the program stop here and wait for the new gesture change, and the LED lights in the loop cannot be animated. I want to switch to hardware interruption, but the result is not good. Later, I found the library of Arduino_APDS9960 to see if it could detect the reading of gestures. As a result, I found a function gestureAvailable() to judge whether there is a reading value, and then the program was successfully completed. Let's take a look at how to complete the implementation of the gesture control direction light.

APDS-9960 is an extremely small sensor with built-in ultraviolet and infrared blocking filters, four separate diodes for sensitivity in different directions, and an I2C compatible interface. Other specifications are as follows:
  • Working voltage: 3.3V
  • Ambient light and RGB color sensor
  • Proximity detection
  • Gesture detection
  • Measuring range: 4-8in (10-20CM)
  • I2C interface (I2C address: 0x39)

[Install APDS9960 Library]

The Arduino_APDS9960 library is used to read data from the APDS-9960 gesture sensor, which can detect ambient light. This implementation requires the following libraries to be installed:
You can find this library in the "management library" on the interface of the Arduino IDE. For the installation method of the library (Library), please refer to another article: Arduino: Install the Arduino IDE library .

[Material]

  • Arduino Uno x1
  • APDS-9960 color, non-contact gesture detection sensor module x1
  • MAX7219 8x8 LED matrix module x1
  • Connection line x N

[Wiring diagram]

Arduino UnoAPDS-99608x8 LED Matrix module
3.3VVCC-
GNDGNDGND
A5SCL-
A4SDA-
D2INT-
+5V-VCC
D10-CS
D11-CLK
D12-Data In(DIN)


[Code]

#include <Arduino_APDS9960.h>
#include <LedControl.h>

// Pin 12:Data in, Pin 11: Clock,  Pin 10: CS(Load)
LedControl display = LedControl(12,11,10,1);

const uint64_t R_IMAGES[] = {   //right arrow
  0x0000000100000000,
  0x0000010301000000,
  0x0001030703010000,
  0x0103070f07030100,
  0x02060f1f0f060200,
  0x040c1f3f1f0c0400,
  0x08183f7f3f180800,
  0x08183f7f3f180800,
  0x10307fff7f301000,
  0x2060fefefe602000,
  0x40c0fcfcfcc04000,
  0x8080f8f8f8808000,
  0x0000f0f0f0000000,
  0x0000e0e0e0000000,
  0x0000c0c0c0000000,
  0x0000808080000000,
  0x0000000000000000
};
const int R_IMAGES_LEN = sizeof(R_IMAGES)/8;

const uint64_t L_IMAGES[] = {   // left arrow
  0x0000008000000000,
  0x000080c080000000,
  0x0080c0e0c0800000,
  0x80c0e0f0e0c08000,
  0x4060f0f8f0604000,
  0x2030f8fcf8302000,
  0x1018fcfefc181000,
  0x080cfefffe0c0800,
  0x04067f7f7f060400,
  0x02033f3f3f030200,
  0x01011f1f1f010100,
  0x00000f0f0f000000,
  0x0000070707000000,
  0x0000030303000000,
  0x0000010101000000,
  0x0000000000000000
};
const int L_IMAGES_LEN = sizeof(L_IMAGES)/8;
int LastGesture = 0;   //Record the last gesture 
int i = 0;

void setup() {
  Serial.begin(115200);
  
  if(!APDS.begin()){
    Serial.println("failed to initialize device! Please check your wiring.");
  }
  else Serial.println("Detecting gestures ...");

  display.clearDisplay(0);    // Clear the screen 
  display.shutdown(0, false);  // Turn off power saving mode  
  display.setIntensity(0, 10); // Set the brightness to 8 (between 0 and 15)
}

void displayImage(uint64_t image) {
  for (int i = 0; i < 8; i++) {
    byte row = (image >> i * 8) & 0xFF;
    for (int j = 0; j < 8; j++) {
      display.setLed(0, i, j, bitRead(row, j));
    }
  }
}

void loop() {
  if (APDS.gestureAvailable()) {
    //read APDS9960 value
    uint8_t gesture = APDS.readGesture();
    if(gesture == GESTURE_DOWN) Serial.println("DOWN");
    if(gesture == GESTURE_UP) Serial.println("UP");
    
    if(gesture == GESTURE_LEFT) {
      Serial.println("LEFT");
      LastGesture = 1;
    }
    if(gesture == GESTURE_RIGHT) {
      Serial.println("RIGHT");
      LastGesture = 2;
    }
  }
  
  if (LastGesture == 1) {
    displayImage(L_IMAGES[i]);
    if (++i >= L_IMAGES_LEN ) {
      i = 0;
    }
    delay(10);      
  }
  if (LastGesture == 2) {
    displayImage(R_IMAGES[i]);
    if (++i >= R_IMAGES_LEN ) {
      i = 0;
    }
    delay(10);     
  }
}

[Result]


[Reference]


Post a Comment

Previous Post Next Post