Laurent's Lab
Published © GPL3+

Quick Halloween Beating Heart Using A Neopixel Matrix

Want to spruce up a Halloween costume but you are out of time - here is a quickie with a few interesting tricks.

BeginnerFull instructions provided1 hour2,148

Things used in this project

Hardware components

Adafruit NeoPixel NeoMatrix 8x8 - 64 RGB LED Pixel Matrix
×1
Adafruit Flora
×1
Breadboard trim potentiometer - 10K
×2
Lithium Ion Polymer Battery - 3.7v 1200mAh
×1

Story

Read more

Schematics

Beating Heart fritzing schematics

Code

Beating Heart Arduino code

C/C++
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

#define NEO_PIN 6           // Neopixel pin
#define GND_PIN 9           // Signal pin used as ground
#define GND_PIN2 1          // Signal pin used as ground
#define SPEED_PIN A10       // Analog (ADC) input pin to read value of speed potentiometer
#define COLOR_PIN A11       // Analog (ADC) input pin to read value of color potentiometer
#define MAX_BRIGHTNESS 50   // Maximum Neopixel brightness
#define UP_STEPS 5          // Increasing brightness steps out of the total number of steps (smaller than half total => fast rise, slow decay)
#define TOTAL_STEPS 40      // Total number of increasing/deeasing steps
#define SPEED_DIVIDER 30    // Used to divide the value of the speed ADC readout to calculate iteration delay

const int heart[] = {       // Heart bitmap
  0, 1, 1, 0, 1, 1, 0, 0,
  1, 1, 1, 1, 1, 1, 1, 0,
  1, 1, 1, 1, 1, 1, 1, 0,
  1, 1, 1, 1, 1, 1, 1, 0,
  0, 1, 1, 1, 1, 1, 0, 0,
  0, 0, 1, 1, 1, 0, 0, 0,
  0, 0, 0, 1, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0
};

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, NEO_PIN,
                            NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
                            NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
                            NEO_GRB            + NEO_KHZ800);

void setup() {
  matrix.begin();
  matrix.setBrightness(40);

  pinMode(GND_PIN, OUTPUT);
  digitalWrite(GND_PIN, LOW); // Lazy ground
  pinMode(GND_PIN2, OUTPUT);
  digitalWrite(GND_PIN2, LOW); // Lazy ground
  pinMode(SPEED_PIN, INPUT);
  pinMode(COLOR_PIN, INPUT);
}

void loop() {
  int i, j;
  int brightness;
  int color;
  for (i = 0; i < TOTAL_STEPS; i++) {
    if (i < UP_STEPS)
      brightness = MAX_BRIGHTNESS * i / UP_STEPS;
    else
      brightness = MAX_BRIGHTNESS * (TOTAL_STEPS - i - 1) / (TOTAL_STEPS - UP_STEPS);
    matrix.setBrightness(brightness);
    color = analogRead(COLOR_PIN) / 4;
    for (j = 0; j < 64; j++) {
      matrix.drawPixel(j % 8, j / 8, Wheel(color)*heart[j]);
    }
    matrix.show();
    delay(analogRead(SPEED_PIN) / SPEED_DIVIDER);
  }
}

uint32_t Wheel(byte WheelPos) { // Courtesy of multiple Adafruit examples
  if (WheelPos < 85) {
    return matrix.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return matrix.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return matrix.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Credits

Laurent's Lab

Laurent's Lab

3 projects • 4 followers

Comments