natron77

LED_face.ino

Sep 5th, 2020
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "FastLED.h"
  2. #include "faces.h"
  3.  
  4. // Matrix size
  5. #define NUM_ROWS 28
  6. #define NUM_COLS 26
  7.  
  8. // Define pins
  9. #define DATA_PIN 3
  10. #define BUTTON_0 9
  11. #define BUTTON_1 10
  12. #define BUTTON_2 11
  13.  
  14. // LED brightness
  15. #define BRIGHTNESS 255
  16.  
  17. // Define the array of leds
  18. #define NUM_LEDS NUM_ROWS * NUM_COLS
  19. CRGB leds[NUM_LEDS];
  20.  
  21. // Animation controls
  22. byte pressed = 1; // Tracks which face is currently being displayed
  23. bool button0Pressed = false;
  24. bool button1Pressed = false;
  25. bool button2Pressed = false;
  26.  
  27.  
  28. void setup() {
  29.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  30.   FastLED.setBrightness(BRIGHTNESS);
  31.   mid(); // Start the display with the forward-facing face
  32. }
  33.  
  34. void loop() {
  35.   // Read the button inputs
  36.   button0Pressed = digitalRead(BUTTON_0) == LOW;
  37.   button1Pressed = digitalRead(BUTTON_1) == LOW;
  38.   button2Pressed = digitalRead(BUTTON_2) == LOW;
  39.  
  40.   // If an input occured AND it's different than the current face, display that face
  41.   if (button0Pressed && !(pressed==0)) { pressed = 0; left(); }
  42.   if (button1Pressed && !(pressed==1)) { pressed = 1; mid();  }
  43.   if (button2Pressed && !(pressed==2)) { pressed = 2; right();}
  44.  
  45.   // Blink the eyes ocassionally
  46.   EVERY_N_MILLISECONDS_I(blinktime, 1000) {
  47.     blinking();
  48.     // After blinking, return to the previous face
  49.     switch (pressed) {
  50.         case 0: left(); break;
  51.         case 1: mid(); break;
  52.         case 2: right(); break;
  53.     }
  54.     // Set the next blink delay
  55.     blinktime.setPeriod( random16(1000,3000) );
  56.   }
  57. }
  58.  
  59. // Face-related functions:
  60. void mid() {
  61.   // Read the forward face from PROGMEM, then display it.
  62.   for (int i = 0; i < NUM_LEDS; i++) {
  63.     leds[i] = pgm_read_dword_near(IdleFace + i);
  64.  }
  65.   FastLED.show();
  66. }
  67. void left() {
  68.   // Read the left face from PROGMEM, then display it.
  69.   for (int i = 0; i < NUM_LEDS; i++) {
  70.     leds[i] = pgm_read_dword_near(LeftFace + i);
  71.  }
  72.   FastLED.show();
  73. }
  74. void right() {
  75.   // Read the right face from PROGMEM, then display it.
  76.   for (int i = 0; i < NUM_LEDS; i++) {
  77.     leds[i] = pgm_read_dword_near(RightFace + i);
  78.  }
  79.   FastLED.show();
  80. }
  81. void blinking() {
  82.   // Pick the appropriate blinking face based on the current facing
  83.   switch (pressed) {
  84.     case 0: leftB();   break;
  85.     case 1: midB();    break;
  86.     case 2: rightB();  break;
  87.   }
  88. }
  89. void midB() {
  90.   // Read the right face from PROGMEM, then display it.
  91.   for (int i = 0; i < NUM_LEDS; i++) {
  92.     leds[i] = pgm_read_dword_near(IdleBlink + i);
  93.  }
  94.   FastLED.show();
  95.   // Hold the blink for 50 milliseconds so the blink is visible
  96.   delay(50);
  97. }
  98. void leftB() {
  99.   // Read the right face from PROGMEM, then display it.
  100.   for (int i = 0; i < NUM_LEDS; i++) {
  101.     leds[i] = pgm_read_dword_near(LeftBlink + i);
  102.  }
  103.   FastLED.show();
  104.   // Hold the blink for 50 milliseconds so the blink is visible
  105.   delay(50);
  106. }
  107. void rightB() {
  108.   // Read the right face from PROGMEM, then display it.
  109.   for (int i = 0; i < NUM_LEDS; i++) {
  110.     leds[i] = pgm_read_dword_near(RightBlink + i);
  111.  }
  112.   FastLED.show();
  113.   // Hold the blink for 50 milliseconds so the blink is visible
  114.   delay(50);
  115. }
Add Comment
Please, Sign In to add comment