Last active
October 15, 2015 05:15
-
-
Save lmorchard/a0d14135cb31bc63d3a9 to your computer and use it in GitHub Desktop.
Random 7219 LED matrix Arduino eyes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LedControl.h" | |
LedControl lc = LedControl(12, 11, 10, 2); // Pins: DIN,CLK,CS, # of Display connected | |
const bool DEBUG = true; | |
const int NUM_EYES = 2; | |
const int NUM_COLS = 8; | |
const int NUM_ROWS = 8; | |
const int MOVE_EYE_DELAY = 100; | |
const int BLINK_EYE_DELAY = 10; | |
const int BLINK_EYE_CHANCE = 5; | |
const int MIN_PUPIL_X = 0; | |
const int MAX_PUPIL_X = 4; | |
const int MIN_PUPIL_Y = 0; | |
const int MAX_PUPIL_Y = 4; | |
const int MIN_EYELID_Y = 0; | |
const int MAX_EYELID_Y = 3; | |
const int MIN_INTENSITY = 1; | |
const int MAX_INTENSITY = 15; | |
// Templates for pupil and eyeball | |
const byte eyePupil = B01100000; | |
const byte eyeBall[8] = { | |
B00111100, | |
B01111110, | |
B11111111, | |
B11111111, | |
B11111111, | |
B11111111, | |
B01111110, | |
B00111100 | |
}; | |
// Display buffer for rendering eyes | |
byte eyes[NUM_EYES][NUM_ROWS]; | |
int intensity, pupilX, pupilY, eyelidY, frameCount; | |
void setup() { | |
if (DEBUG) { | |
Serial.begin(9600); | |
while (!Serial) { } | |
Serial.println("STARTING UP"); | |
} | |
intensity = 0; | |
pupilX = 0; | |
pupilY = 0; | |
eyelidY = 0; | |
frameCount = 0; | |
for (int i=0; i<NUM_EYES; i++) { | |
for (int j=0; j<NUM_ROWS; j++) { | |
eyes[i][j] = 0; | |
} | |
lc.shutdown(i, false); | |
lc.setIntensity(i, intensity); | |
lc.clearDisplay(i); | |
} | |
} | |
void loop() { | |
pupilX = constrain(pupilX + random(-1, 2), MIN_PUPIL_X, MAX_PUPIL_X); | |
pupilY = constrain(pupilY + random(-1, 2), MIN_PUPIL_Y, MAX_PUPIL_Y); | |
// eyelidY = constrain(pupilY + random(-1, 2), MIN_EYELID_Y, MAX_EYELID_Y); | |
intensity = constrain(intensity + random(-1, 2), MIN_INTENSITY, MAX_INTENSITY); | |
for (int i=0; i<NUM_EYES; i++) { | |
lc.setIntensity(i, intensity); | |
renderEyeball(i, pupilX, pupilY); | |
renderEyelid(i, eyelidY); | |
} | |
displayEyes(); | |
if (random(0, 100) < BLINK_EYE_CHANCE) { | |
blinkEyes(); | |
} | |
delay(MOVE_EYE_DELAY); | |
} | |
void blinkEyes() { | |
for (int j=eyelidY; j<NUM_ROWS; j++) { | |
for (int i=0; i<NUM_EYES; i++) { | |
renderEyeball(i, pupilX, pupilY); | |
renderEyelid(i, j); | |
} | |
displayEyes(); | |
delay(BLINK_EYE_DELAY); | |
} | |
for (int j=NUM_ROWS-1; j>=eyelidY; j--) { | |
for (int i=0; i<NUM_EYES; i++) { | |
renderEyeball(i, pupilX, pupilY); | |
renderEyelid(i, j); | |
} | |
displayEyes(); | |
delay(BLINK_EYE_DELAY); | |
} | |
} | |
void renderEyeball(int idx, int x, int y) { | |
byte row; | |
for (int i=0; i<NUM_ROWS; i++) { | |
row = eyeBall[i]; | |
if (i >= y+1 && i <= y+2) { | |
row ^= eyePupil >> x; | |
} | |
eyes[idx][i] = row; | |
} | |
} | |
void renderEyelid(int idx, int y) { | |
for (int i=0; i<y; i++) { | |
eyes[idx][i] = 0; | |
} | |
} | |
void displayEyes() { | |
for (int j=0; j<NUM_ROWS; j++) { | |
for (int i=0; i<NUM_EYES; i++) { | |
lc.setRow(i, j, eyes[i][j]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment