Seeed
Published © MIT

Light Up Your Valentine with Sparkling Matrix

Valentine’s Day is a chance for you to send love messages. Why not make a fun LED face with inexpensive components to express your feeling!

BeginnerFull instructions provided1 hour694

Things used in this project

Hardware components

Seeeduino V4.2
Seeed Studio Seeeduino V4.2
×1
Base Shield V2
Seeed Studio Base Shield V2
×1
Seeed Studio Grove - Gesture(PAJ7620U2)
×1
Seeed Studio Grove - Red LED Matrix w/Driver
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Untitled file

C/C++
#include "Grove_LED_Matrix_Driver_HT16K33.h"
#include "MsTimer2.h"
#include "paj7620.h"
#include "Wire.h"

#define TIMEOUT             5000
#define GES_REG_ADDR        0x43
#define ARRAY_LENGTH(array) \
  (sizeof(array) / sizeof(array[0]))

Matrix_8x8 matrix;
uint64_t matrixDisplay = 0;
unsigned long prevTime = millis();

int8_t sIndex = 0;
bool showStatic = true;
const uint64_t STATIC[] = {
  0x00003c0000a54200,
  0x00003c000000e700,
  0x00003c004242e700
};

int8_t dIndex = 0;
const uint64_t DYNAMIC[] = {
  0x00003c000021e700,
  0x00003c000042e700,
  0x00003c000084e700
};
const uint64_t HEART[] = {
  0x00183c7e7e240000,
  0x00183c7effff6600,
  0x183c7effffffff66,
  0x00183c7effff6600 
};

void displayStatic(void) {
  if (showStatic) {
    matrixDisplay = STATIC[sIndex];
    sIndex = (sIndex + 1) % ARRAY_LENGTH(STATIC);
  }
}

void heartBeat() {
  for (uint8_t i = 0; i < ARRAY_LENGTH(HEART); ++i) {
    matrix.writeOnePicture(HEART[i]);
    matrix.display();
    delay(500);
  }
}

void displayDynamic(bool leftToRight) {
  unsigned long currentTime = millis();
  if (currentTime - prevTime > TIMEOUT) {
    showStatic = false;
    prevTime = currentTime;
    dIndex = leftToRight ? 0 : ARRAY_LENGTH(DYNAMIC) - 1;
  }

  // This is used to avoid exceed the boundary.
  if (dIndex >= ARRAY_LENGTH(DYNAMIC) || dIndex <= -1) {
    heartBeat();
    dIndex = leftToRight ? ARRAY_LENGTH(DYNAMIC) - 1 : 0;
  }
  
  matrixDisplay = DYNAMIC[leftToRight ? dIndex++ : dIndex--];
}

void setup() {
  // Initialized the serial to debug.
  Serial.begin(9600);
  while(!Serial);
  
  uint8_t errorCode = paj7620Init();
  
  Wire.begin();
  matrix.init();
  matrix.setBrightness(15);
  matrix.setBlinkRate(BLINK_OFF);
  
  MsTimer2::set(1000, displayStatic);
  MsTimer2::start();
}

void loop() {
  uint8_t data, errorCode = paj7620ReadReg(GES_REG_ADDR, 1, &data);
  
  if (!errorCode) {
    switch (data) {
      case GES_RIGHT_FLAG:
        displayDynamic(true);
        break;

      case GES_LEFT_FLAG: 
        displayDynamic(false);
        break;
    }
  }

  if (millis() - prevTime > TIMEOUT) {
    showStatic = true;
  }

  matrix.writeOnePicture(matrixDisplay);
  matrix.display();
  
  delay(100);
}

Credits

Seeed

Seeed

101 projects • 159 followers
Seeed R&D Team

Comments