Dave Thompson
Published © MIT

Pac Attack

Pinky from Pacman with motion activated eye animation.

BeginnerShowcase (no instructions)4 hours599
Pac Attack

Things used in this project

Hardware components

Spark Core
Particle Spark Core
×1

Story

Read more

Schematics

Wiring

Code

Code

C/C++
Core Dev app code
#include "application.h"
#include "neopixel/neopixel.h"

SYSTEM_MODE(AUTOMATIC);

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define LEFT_PIXEL_PIN D1
#define RIGHT_PIXEL_PIN D7
#define MOTION D4
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel leftStrip = Adafruit_NeoPixel(PIXEL_COUNT, LEFT_PIXEL_PIN, PIXEL_TYPE);
Adafruit_NeoPixel rightStrip = Adafruit_NeoPixel(PIXEL_COUNT, RIGHT_PIXEL_PIN, PIXEL_TYPE);

int lastMotionTime = 0;
int lastAnimationTime = 0;

void setup()
{
  pinMode(RIGHT_PIXEL_PIN, OUTPUT);
  pinMode(LEFT_PIXEL_PIN, OUTPUT);
  pinMode(MOTION, INPUT);
    
  leftStrip.begin();
  leftStrip.setBrightness(32);
  leftStrip.show(); // Initialize all pixels to 'off'
  
  rightStrip.begin();
  rightStrip.setBrightness(32);
  rightStrip.show(); // Initialize all pixels to 'off'
}

void loop()
{
  checkMotion();
  animateMotion();
}

void checkMotion() {
    uint16_t now = millis();
    
    if (digitalRead(MOTION) == HIGH) {
        lastMotionTime = now;
    }
}

void animateMotion() {
    uint16_t now = millis();
    uint16_t duration = 250;
    uint16_t i;
    
    if (lastMotionTime > 0 && (now - lastMotionTime) < duration) {
        lastMotionTime = 0;
        animateEyes();
    }
    
    allPixelsOff();
}

void animateEyes() {
    uint16_t frameDelay = 1000;
    while (frameDelay > 25) {
        around(frameDelay);
        around(frameDelay);
        frameDelay /= 2;
    }
}

void around(uint16_t frameDelay) {
    whiteFrame();
    leftFrame();
    leftStrip.show();
    rightStrip.show();
    delay(frameDelay);
    
    whiteFrame();
    upFrame();
    leftStrip.show();
    rightStrip.show();
    delay(frameDelay);
        
    whiteFrame();
    rightFrame();
    leftStrip.show();
    rightStrip.show();
    delay(frameDelay);
        
    whiteFrame();
    downFrame();
    leftStrip.show();
    rightStrip.show();
    delay(frameDelay); 
}

void whiteFrame() {
    uint16_t i;
    for(i=0; i < leftStrip.numPixels(); i++) {
        leftStrip.setPixelColor(i, 128, 128, 128);
    }
    
    leftStrip.setPixelColor(0, 0, 0, 0);
    leftStrip.setPixelColor(3, 0, 0, 0);
    leftStrip.setPixelColor(12, 0, 0, 0);
    leftStrip.setPixelColor(15, 0, 0, 0);
    
    
    for(i=0; i < rightStrip.numPixels(); i++) {
        rightStrip.setPixelColor(i, 128, 128, 128);
    }
    
    rightStrip.setPixelColor(0, 0, 0, 0);
    rightStrip.setPixelColor(3, 0, 0, 0);
    rightStrip.setPixelColor(12, 0, 0, 0);
    rightStrip.setPixelColor(15, 0, 0, 0);
}

void setLeftEyePixel(uint16_t i) {
    leftStrip.setPixelColor(i, 0, 0, 128);
}

void setRightEyePixel(uint16_t i) {
    rightStrip.setPixelColor(i, 0, 0, 128);
}

void leftFrame() {
    setLeftEyePixel(6);
    setLeftEyePixel(7);
    setLeftEyePixel(8);
    setLeftEyePixel(9);
    
    setRightEyePixel(4);
    setRightEyePixel(5);
    setRightEyePixel(10);
    setRightEyePixel(11);
}

void upFrame() {
    setLeftEyePixel(1);
    setLeftEyePixel(2);
    setLeftEyePixel(5);
    setLeftEyePixel(6);
    
    setRightEyePixel(9);
    setRightEyePixel(10);
    setRightEyePixel(13);
    setRightEyePixel(14);
}

void middleFrame() {
    setLeftEyePixel(5);
    setLeftEyePixel(6);
    setLeftEyePixel(9);
    setLeftEyePixel(10);
}

void rightFrame() {
    setLeftEyePixel(4);
    setLeftEyePixel(5);
    setLeftEyePixel(10);
    setLeftEyePixel(11);
    
    setRightEyePixel(6);
    setRightEyePixel(7);
    setRightEyePixel(8);
    setRightEyePixel(9);
}

void downFrame() {
    setLeftEyePixel(9);
    setLeftEyePixel(10);
    setLeftEyePixel(13);
    setLeftEyePixel(14);
    
    setRightEyePixel(1);
    setRightEyePixel(2);
    setRightEyePixel(5);
    setRightEyePixel(6);
}

void allPixelsOff() {
    uint16_t i;
    for(i=0; i < leftStrip.numPixels(); i++) {
        leftStrip.setPixelColor(i, 0, 0, 0);
    }
    leftStrip.show();
    
    for(i=0; i < rightStrip.numPixels(); i++) {
        rightStrip.setPixelColor(i, 0, 0, 0);
    }
    rightStrip.show();
}

Credits

Dave Thompson

Dave Thompson

2 projects • 2 followers
I like to ride my bike and occasionally write some code.

Comments