#include "FastLED.h"
#include "faces.h"
// Matrix size
#define NUM_ROWS 28
#define NUM_COLS 26
// Define pins
#define DATA_PIN 3
#define BUTTON_0 9
#define BUTTON_1 10
#define BUTTON_2 11
// LED brightness
#define BRIGHTNESS 255
// Define the array of leds
#define NUM_LEDS NUM_ROWS * NUM_COLS
CRGB leds[NUM_LEDS];
// Animation controls
byte pressed = 1; // Tracks which face is currently being displayed
bool button0Pressed = false;
bool button1Pressed = false;
bool button2Pressed = false;
void setup() {
pinMode(BUTTON_0, INPUT_PULLUP);
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
mid(); // Start the display with the forward-facing face
}
void loop() {
// Read the button inputs
button0Pressed = digitalRead(BUTTON_0) == LOW;
button1Pressed = digitalRead(BUTTON_1) == LOW;
button2Pressed = digitalRead(BUTTON_2) == LOW;
// If an input occured AND it's different than the current face, display that face
if (button0Pressed && !(pressed == 0)) {
pressed = 0;
left();
}
if (button1Pressed && !(pressed == 1)) {
pressed = 1;
mid();
}
if (button2Pressed && !(pressed == 2)) {
pressed = 2;
right();
}
// Blink the eyes ocassionally
EVERY_N_MILLISECONDS_I(blinktime, 1000) {
blinking();
// After blinking, return to the previous face
switch (pressed) {
case 0: left(); break;
case 1: mid(); break;
case 2: right(); break;
}
// Set the next blink delay
blinktime.setPeriod( random16(1000, 3000) );
}
}
// Face-related functions:
void mid() {
// Read the forward face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(IdleFace + i);
}
FastLED.show();
}
void left() {
// Read the left face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(LeftFace + i);
}
FastLED.show();
}
void right() {
// Read the right face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(RightFace + i);
}
FastLED.show();
}
void blinking() {
// Pick the appropriate blinking face based on the current facing
switch (pressed) {
case 0: leftB(); break;
case 1: midB(); break;
case 2: rightB(); break;
}
}
void midB() {
// Read the right face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(IdleBlink + i);
}
FastLED.show();
// Hold the blink for 50 milliseconds so the blink is visible
delay(50);
}
void leftB() {
// Read the right face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(LeftBlink + i);
}
FastLED.show();
// Hold the blink for 50 milliseconds so the blink is visible
delay(50);
}
void rightB() {
// Read the right face from PROGMEM, then display it.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = pgm_read_dword_near(RightBlink + i);
}
FastLED.show();
// Hold the blink for 50 milliseconds so the blink is visible
delay(50);
}
arduino:SCL
arduino:SDA
arduino:AREF
arduino:GND.1
arduino:13
arduino:12
arduino:11
arduino:10
arduino:9
arduino:8
arduino:7
arduino:6
arduino:5
arduino:4
arduino:3
arduino:2
arduino:1
arduino:0
arduino:14
arduino:15
arduino:16
arduino:17
arduino:18
arduino:19
arduino:20
arduino:21
arduino:5V.1
arduino:5V.2
arduino:22
arduino:23
arduino:24
arduino:25
arduino:26
arduino:27
arduino:28
arduino:29
arduino:30
arduino:31
arduino:32
arduino:33
arduino:34
arduino:35
arduino:36
arduino:37
arduino:38
arduino:39
arduino:40
arduino:41
arduino:42
arduino:43
arduino:44
arduino:45
arduino:46
arduino:47
arduino:48
arduino:49
arduino:50
arduino:51
arduino:52
arduino:53
arduino:GND.4
arduino:GND.5
arduino:IOREF
arduino:RESET
arduino:3.3V
arduino:5V
arduino:GND.2
arduino:GND.3
arduino:VIN
arduino:A0
arduino:A1
arduino:A2
arduino:A3
arduino:A4
arduino:A5
arduino:A6
arduino:A7
arduino:A8
arduino:A9
arduino:A10
arduino:A11
arduino:A12
arduino:A13
arduino:A14
arduino:A15
neopixels:DOUT
neopixels:VDD
neopixels:VSS
neopixels:DIN
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r