Dawn DuPriest
Published © CC BY

Stranger Things Ugly Christmas Sweater

A classic (?) Ugly Christmas Sweater with a twist - a Stranger Things-themed sweater is modded with music, lights, and minigames.

IntermediateFull instructions provided8 hours701

Things used in this project

Hardware components

Flora
Adafruit Flora
×1
Adafruit Flora Bluefruit LE
×1
Lilypad MP3 Board
SparkFun Lilypad MP3 Board
×1
Adafruit Neopixel LED Dots Strand
×1
SparkFun Thin Speaker
×1
Adafruit lithium ion polymer battery
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)
SparkFun conductive thread

Story

Read more

Schematics

Wearable Components

This picture just shows the sewing and soldering on the Flora, Bluefruit LE, and LilyPad MP3.

Code

Sweater and MP3 Player Code

Arduino
This file has two sections, so look for the /*BREAK HERE*/ line that separates the two files: the bluetooth sweater code that should run on the Flora, and the LilyPad Mp3 player that triggers the first two tracks on digital HIGH and uses T3 as a cancel button. You'll need a micro SD card with 1.mp3 and 2.mp3 on it.
You may have to dig around a bit for a LilyPad Mp3 library and SDFat library that works with this code.
#include <Adafruit_NeoPixel.h>

/*********************************************************************
 This is an example for our nRF51822 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include "BluefruitConfig.h"

#if SOFTWARE_SERIAL_AVAILABLE
  #include <SoftwareSerial.h>
#endif

/*=========================================================================
    APPLICATION SETTINGS

  FACTORYRESET_ENABLE     Perform a factory reset when running this sketch
 
                            Enabling this will put your Bluefruit LE module
                              in a 'known good' state and clear any config
                              data set in previous sketches or projects, so
                            running this at least once is a good idea.
 
                            When deploying your project, however, you will
                              want to disable factory reset by setting this
                              value to 0. If you are making changes to your
                            Bluefruit LE device via AT commands, and those
                              changes aren't persisting across resets, this
                              is the reason why. Factory reset will erase
                              the non-volatile memory where config data is
                              stored, setting it back to factory default
                              values.
   
                            Some sketches that require you to bond to a
                              central device (HID mouse, keyboard, etc.)
                              won't work at all with this feature enabled
                              since the factory reset will clear all of the
                              bonding data stored on the chip, meaning the
                              central device won't be able to reconnect.
    MINIMUM_FIRMWARE_VERSION  Minimum firmware version to have some new features
    MODE_LED_BEHAVIOUR        LED activity, valid options are
                              "DISABLE" or "MODE" or "BLEUART" or
                              "HWUART"  or "SPI"  or "MANUAL"
    -----------------------------------------------------------------------*/
    #define FACTORYRESET_ENABLE         1
    #define MINIMUM_FIRMWARE_VERSION    "0.6.6"
    #define MODE_LED_BEHAVIOUR          "MODE"
/*=========================================================================*/

// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
                      BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/

/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
 Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];


Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, 9, NEO_GRB + NEO_KHZ800);
int pixelsinorder[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

uint8_t curcolor = strip.Color(50,50,50);
boolean playinggame = false;
int playerpos = 0;
int targetpos = 0;
boolean caughttarget = false;
int score = 0;
long gametimer = 0;
long curtime = 0;
long stopgametime = 0;
uint8_t playercolor = strip.Color(0,50,0);
uint8_t targetcolor = strip.Color(50,0,0);
boolean changedpos = false;

boolean roulette = false;
boolean spinning = true;
int rlocation = 0;

uint16_t packettimeout = BLE_READPACKET_TIMEOUT;


/**************************************************************************/
/*!
    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  randomSeed(analogRead(A0));
  pinMode(6, OUTPUT);
  digitalWrite(6, LOW);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
  //while (!Serial);  // required for Flora & Micro
  delay(500);

  Serial.begin(115200);
  Serial.println(F("Adafruit Bluefruit App Controller Example"));
  Serial.println(F("-----------------------------------------"));

  /* Initialise the module */
  Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin(VERBOSE_MODE) )
  {
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  }
  Serial.println( F("OK!") );

  if ( FACTORYRESET_ENABLE )
  {
    /* Perform a factory reset to make sure everything is in a known state */
    Serial.println(F("Performing a factory reset: "));
    if ( ! ble.factoryReset() ){
      error(F("Couldn't factory reset"));
    }
  }


  /* Disable command echo from Bluefruit */
  ble.echo(false);

  Serial.println("Requesting Bluefruit info:");
  /* Print Bluefruit information */
  ble.info();

  Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
  Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
  Serial.println();

  ble.verbose(false);  // debug info is a little annoying after this point!

  /* Wait for connection */
  while (! ble.isConnected()) {
      delay(500);
  }

  Serial.println(F("******************************"));

  // LED Activity command is only supported from 0.6.6
  if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
  {
    // Change Mode LED Activity
    Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
    ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
  }

  // Set Bluefruit to DATA mode
  Serial.println( F("Switching to DATA mode!") );
  ble.setMode(BLUEFRUIT_MODE_DATA);

  Serial.println(F("******************************"));

}

/**************************************************************************/
/*!
    @brief  Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
  uint8_t len;
  // check for time over in the minigame
  if(playinggame)
  {
  curtime = millis();
  if(curtime >= stopgametime)
  {
     colorMiniStrip(strip.Color(0,0,0), 0, 19, 800);
     for(int h = 0; h < score; h++)
     {
     colorMiniStrip(strip.Color(127,127,127), 0, 19, 300);  
     colorMiniStrip(strip.Color(0,0,0), 0, 19, 300);
     }
     playinggame = false;
     score = 0;
   }
   packettimeout = 200;
  }
  else if(roulette && spinning)
  {
        colorMiniStrip(strip.Color(0,0,0),rlocation,rlocation,1);
        rlocation += 1;
        if(rlocation > 19)
        {  rlocation = 0;}
        colorMiniStrip(strip.Color(0,125,0),rlocation,rlocation, 1);
        packettimeout = 50;
  }
  else
  {
    packettimeout = BLE_READPACKET_TIMEOUT;
  }
  /* Wait for new data to arrive */
  len = readPacket(&ble, packettimeout);
  if (len == 0) return;

  /* Got a packet! */
  // printHex(packetbuffer, len);

  // Color
  
  if (packetbuffer[1] == 'C') {
    uint8_t red = packetbuffer[2];
    uint8_t green = packetbuffer[3];
    uint8_t blue = packetbuffer[4];
    Serial.print ("RGB #");
    if (red < 0x10) Serial.print("0");
    Serial.print(red, HEX);
    if (green < 0x10) Serial.print("0");
    Serial.print(green, HEX);
    if (blue < 0x10) Serial.print("0");
    Serial.println(blue, HEX);
    curcolor = strip.Color(red, green, blue);
    colorWipe(strip.Color(red, green, blue), 50);
  }

  // Buttons
  if (packetbuffer[1] == 'B') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    //Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      //Serial.println(" pressed");
      if(buttnum == 1)
      {
        
        if(!(roulette))
        {
          playinggame = false; // stop the minigame if running
          roulette = true;
          spinning = true;
          rlocation = (int)random(0,20);
          colorMiniStrip(strip.Color(0,0,0),0,19,1);
          colorMiniStrip(strip.Color(0,125,0),rlocation, rlocation, 1);
        }
        
      }
      else if(buttnum == 2)
      {
        playinggame = false; // stop the minigame if running
        roulette = false;
        digitalWrite(12, HIGH);
        delay(500);
        STThemeSong();
        digitalWrite(12, LOW);
        
      }
      else if(buttnum == 3)
      {
        playinggame = false; // stop the minigame if running
        roulette = false;
        digitalWrite(10, HIGH);
        delay(250);
        digitalWrite(10, LOW);
      }
      else if(buttnum == 4) // play a minigame
      {
        // start the game
        playinggame = true;
        roulette = false;
        spinning = false;
        gametimer = millis();
        curtime = millis();
        stopgametime = curtime + 20000; // 20 second game
        score = 0;
        targetpos = (int)random(0,20);
        playerpos = (int)random(0,20);
        // don't let the player and target be on top of each other
        while(targetpos == playerpos)
              targetpos = (int)random(0,20);
        
        colorMiniStrip(strip.Color(0,0,0),0,19,1);
        colorMiniStrip(strip.Color(50,50,0), playerpos, playerpos, 1);
        colorMiniStrip(strip.Color(0,0,50), targetpos, targetpos, 1);
        
        
      }
      
      // play the game
      if(playinggame)
      {
        // up. I defined pixel 6/7 as the "topmost" pixel. these move you toward pixel 6/7
        // in addition, "up" is what you push to stop the roulette wheel
        if(buttnum == 5)
        {
          
          
            if(playerpos == 19) 
            {
              playerpos = 0;
              changedpos = true;
            }
            else if(playerpos >= 17)
            {
              playerpos = playerpos + 1;
              changedpos = true;
            }
            else if(playerpos >= 0 && playerpos <= 5)
            {
              playerpos = playerpos + 1;
              changedpos = true;
            }
            else if(playerpos <= 16 && playerpos >= 8)
            {
              playerpos = playerpos - 1;
              changedpos = true;
            }
          
        }
        
        // 6 is down. 16/17 are the bottommost pixels
        else if(buttnum == 6)
        {
          if(playerpos == 0) 
          {
            playerpos = 19;
            changedpos = true;
          }
          else if(playerpos <= 19 && playerpos >= 18)
          {
            playerpos --;
            changedpos = true;
          }
          else if(playerpos >= 1 && playerpos <= 6)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos <= 15 && playerpos >= 7)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
        }
        // left. 11 is the leftmost pixel
        else if(buttnum == 7)
        {
          if(playerpos == 0) 
          {
            playerpos = 19;
            changedpos = true;
          }
          else if(playerpos == 1)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos <= 10 && playerpos >= 2)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos >= 12 && playerpos <= 19)
          {
            playerpos = playerpos - 1;
            changedpos = true; 
          }
        }
        // right. 2 is the rightmost pixel
        else if(buttnum == 8)
        {
          if(playerpos == 19) 
          {
            playerpos = 0;
            changedpos = true;
          }
          else if(playerpos == 1 || playerpos == 0)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos <= 11 && playerpos >= 3)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos >= 12 && playerpos <= 18)
          {
            playerpos = playerpos + 1;
            changedpos = true; 
          }
        }
        if(changedpos)
        {
          // redraw pixels if you moved.If you scored a point, randomize positions again
          if(playerpos == targetpos)
          {
            score++;
            playerpos = (int)random(0,20);
            targetpos = (int)random(0,20);
            while(targetpos == playerpos)
              targetpos = (int)random(0,20);
          }


          colorMiniStrip(strip.Color(0,0,0),0,19,1);
          colorMiniStrip(strip.Color(0,0,50), targetpos, targetpos, 1);
          colorMiniStrip(strip.Color(50,50,0), playerpos, playerpos, 1);
          

          changedpos = false;
        }
        
      }// end if playing game
      if(roulette && spinning) // if playing roulette
      {  
          if(buttnum == 5) // up
          {
            if(rlocation == 16 || rlocation == 17) // you win
            {
              roulette = false;
              spinning = false;
              digitalWrite(6, HIGH);
              delay(900);
              brightenMiniDuration(3, 16, 17, 200);
              brightenMiniDuration(2, 16, 17, 300);
              brightenMiniDuration(1, 16, 17, 400);
              delay(300);
              rainbowDuration(20, 3000);
              theaterChaseDuration(strip.Color(127, 0, 0), 50, 1000); 
              theaterChaseDuration(strip.Color(127,127,127), 10, 2400);
              twoColorWipeDurationMiniReverse(strip.Color(0, 0, 127), strip.Color(50, 0,127), 2, 11, 1000);
              twoColorWipeDurationMiniReverse(strip.Color(0, 127, 127), strip.Color(127, 127,127), 0, 13, 1000);
              twoColorWipeDurationMiniReverse(strip.Color(127, 0, 0), strip.Color(127, 50,0), 0, 19, 1000);
              brightenMiniDuration(4, 0, 19, 1000);
              colorWipe(strip.Color(0, 0, 0), 10);
              
              digitalWrite(6, LOW);
            }
            else
            {
              colorWipe(strip.Color(125, 0, 0), 70);
              roulette = false;
              spinning = false;
              delay(400);
              colorWipe(strip.Color(0, 0, 0), 70);
            }
          
        } // end if up button pressed
       
      }// end if roulette
      
      
    } // end packetbuffer is a button 
    else {
      Serial.println(" released");
    }
  }
  

  // GPS Location
  if (packetbuffer[1] == 'L') {
    float lat, lon, alt;
    lat = parsefloat(packetbuffer+2);
    lon = parsefloat(packetbuffer+6);
    alt = parsefloat(packetbuffer+10);
    Serial.print("GPS Location\t");
    Serial.print("Lat: "); Serial.print(lat, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print("Lon: "); Serial.print(lon, 4); // 4 digits of precision!
    Serial.print('\t');
    Serial.print(alt, 4); Serial.println(" meters");
  }

  // Accelerometer
  if (packetbuffer[1] == 'A') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Accel\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Magnetometer
  if (packetbuffer[1] == 'M') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Mag\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Gyroscope
  if (packetbuffer[1] == 'G') {
    float x, y, z;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    Serial.print("Gyro\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.println();
  }

  // Quaternions
  if (packetbuffer[1] == 'Q') {
    float x, y, z, w;
    x = parsefloat(packetbuffer+2);
    y = parsefloat(packetbuffer+6);
    z = parsefloat(packetbuffer+10);
    w = parsefloat(packetbuffer+14);
    Serial.print("Quat\t");
    Serial.print(x); Serial.print('\t');
    Serial.print(y); Serial.print('\t');
    Serial.print(z); Serial.print('\t');
    Serial.print(w); Serial.println();
  }

  


}



boolean needToCancel()
{
  
  uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
  if (len == 0) return false;

  // Buttons
  if (packetbuffer[1] == 'B') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      Serial.println(" pressed");
      if(buttnum == 3)
      {
        return true;
      } 
    }
 
  }
  
  return false;
}



// some example procedures
 // Some example procedures showing how to display to the pixels:
//  colorWipe(strip.Color(255, 0, 0), 50); // Red
 // colorWipe(strip.Color(0, 255, 0), 50); // Green
  //colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  // Send a theater pixel chase in...
  //theaterChase(strip.Color(127, 127, 127), 50); // White
  //theaterChase(strip.Color(127, 0, 0), 50); // Red
  //theaterChase(strip.Color(0, 0, 127), 50); // Blue

  //theaterChaseDuration(strip.Color(127, 0, 0), 50, 2000); // Red
  //theaterChaseDuration(strip.Color(0, 0, 127), 50, 5000); // Blue

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void colorWipeDurationMini(uint32_t c, int lowlight, int highlight, int duration) {
  int numlights = highlight - lowlight + 1;
  duration = (int)duration;
  int durationperlight = (int)(duration / numlights) ;

  if(numlights == 20)
  {
    for(int i = 0; i <= 19; i++)
    {
      strip.setPixelColor(pixelsinorder[i],c);
      strip.show();
      delay(durationperlight);
    }
  }
  else{
    for(int i=lowlight; i<=highlight; i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(durationperlight);
    }
  }
}


void colorWipeDurationMiniReverse(uint32_t c, int lowlight, int highlight, int duration) {
  int numlights = highlight - lowlight + 1;
  duration = (int)duration;
  int durationperlight = (int)(duration / numlights) ;
    for(int i=highlight; i>=lowlight; i--) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(durationperlight);
    }
  
}

void twoColorWipeDurationMiniReverse(uint32_t c, uint32_t c2, int lowlight, int highlight, int duration) {
  int numlights = highlight - lowlight + 1;
  duration = (int)duration;
  int durationperlight = (int)(duration / numlights) ;
    for(int i=highlight; i>=lowlight; i--) {
      if(i%2 == 0)
        strip.setPixelColor(i, c);
      else
        strip.setPixelColor(i,c2);
      strip.show();
      delay(durationperlight);
    }
  
}

void colorWipeRainbowDurationMini(int lowlight, int highlight, int duration) {
  int numlights = highlight - lowlight + 1;
  duration = (int)duration;
  int durationperlight = (int)(duration / numlights) ;
  long r = random(0,6);

  uint32_t rainbowcolors[] = {strip.Color(50,0,0),strip.Color(50,50,0),strip.Color(0,50,0),strip.Color(0,50,50),strip.Color(0,0,50),strip.Color(50,0,50)};
    for(int i=lowlight; i<=highlight; i++) {
      strip.setPixelColor(i, rainbowcolors[r]);
      r++;
      if(r == 6)
        r = 0;
      strip.show();
      delay(durationperlight);
    }
 }



void colorMiniStrip(uint32_t c, int lowlight, int highlight, int duration)
{
  int numlights = highlight - lowlight;
  int durationperlight = duration / numlights;
  for(int i=lowlight; i<=highlight; i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(duration);
  
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

void rainbowDuration(uint8_t wait, int duration) {
  uint16_t i, j;
  int totaltime = 0;
  while(totaltime < duration)
  {
  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
    totaltime += wait;
    if(totaltime >= duration)
    {
      return;
    }
  }
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights.
void theaterChaseDuration(uint32_t c, uint8_t wait, int duration) {
  int j = 0;
  int q = 0;
  uint16_t i = 0;
  int totaltime = 0;
  int lightnum;
  duration = (int)duration;
  while(totaltime < duration)
  {
      for (uint16_t i=0; i <= 19; i=i+3) {
        lightnum = i+q;
        if(lightnum > 19)
        {
          lightnum = lightnum - 19 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);
      totaltime += wait;

      for (uint16_t i=0; i <= 19; i=i+3) {
        lightnum = i+q;
        if(lightnum > 19)
        {
          lightnum = lightnum - 19 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], 0);        //turn every third pixel off
      }

    q++;
    if(q >= 3)
      q = 0;
  }
  // when time is up turn lights off
  for(uint16_t i = 0; i < strip.numPixels(); i++)
  {
    strip.setPixelColor(i,0);
  }
  strip.show();
}


void theaterChaseMiniDuration(uint32_t c, int lowlight, int highlight, uint8_t wait, int duration) {
  int j = 0;
  int q = 0;
  uint16_t i = 0;
  int totaltime = 0;
  duration = (int)duration;
  while(totaltime < duration)
  {
      int lightnum;
      for (int i=lowlight; i <= highlight; i=i+3) {
        lightnum = i+q;
        if(lightnum > highlight)
        {
          lightnum = lightnum - highlight - 1 + lowlight;
        }
        strip.setPixelColor(lightnum, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);
      totaltime += wait;

      for (uint16_t i=lowlight; i <= highlight; i=i+3) {
        lightnum = i+q;
        if(lightnum > highlight)
        {
          lightnum = lightnum - highlight - 1 + lowlight;
        }
        strip.setPixelColor(lightnum, 0);        //turn every third pixel off
      }

    q++;
    if(q >= 3)
      q = 0;
  }
  // when time is up turn lights off
  for(uint16_t i = 0; i < strip.numPixels(); i++)
  {
    strip.setPixelColor(i,0);
  }
  strip.show();
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbowDuration(uint8_t wait, int duration) {
  int totaltime = 0;
  int lightnum;
  while(totaltime < duration)
  {
  for (int j=0; j < 256; j = j + 10) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i <= 19; i=i+3) {
        lightnum = i+q;
        if(lightnum > 19)
        {
          lightnum = lightnum - 19 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);
      for (uint16_t i=0; i <= 19; i=i+3) {
         lightnum = i+q;
        if(lightnum > 19)
        {
          lightnum = lightnum - 19 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], 0);        //turn every third pixel off
      }
      
      totaltime += wait;
      if(totaltime >= duration)
      {
        for(uint16_t m = 0; m < strip.numPixels(); m++)
          strip.setPixelColor(m,0);
        strip.show();
        delay(10);
        return;
      }

      
    }
  }
  
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}


// brightens a strip of pixels over a duration
// assumes duration is over like 1000
// first parameter is 1 if red, 2 if green, 3 if blue, 4 if white
void brightenMiniDuration(int hue, int lowlight, int highlight, int duration)
{
  
  //duration = duration - 127; // need to shorten, there are delays in here
  int mspershade = (int)((duration / 100) * 0.9); 
  if(duration < 256)
    mspershade = 2;
  if(duration < 128)
     mspershade = 1;

  int totaltime = 0;
  uint32_t c;
  int i = 0;
  
  
  while(totaltime < duration)
  {
    if(hue == 1)
        c =strip.Color(i, 0, 0);
    else if (hue == 2)
        c = strip.Color(0,i,0);
    else if (hue == 3)
        c = strip.Color(0,0,i);
    else
        c = strip.Color(i,i,i);
    for(int index = lowlight; index <= highlight; index++)
    {    
      strip.setPixelColor(index, c);
    }
    strip.show();
    i+= 1;
    delay(mspershade);
    totaltime += mspershade;
  }
  return;
  
}

void heartThrobMiniDuration(int hue, int lowlight, int highlight, int duration, int pulserate)
{
  int totaltime = 0;
  uint32_t c;
  int i = 0;
  int msperpulse = 60000 / pulserate;
  int smdelay = msperpulse / 100;
  int meddelay = msperpulse / 10;
  boolean firstpulse = true;
  int longdelay;
  while(totaltime <= duration)
  {
    for(int q = 0; q < 2; q++)
    {
      while(i < 130)
    {
        if(hue == 1)
          c =strip.Color(i, 0, 0);
        else if (hue == 2)
            c = strip.Color(0,i,0);
        else if (hue == 3)
            c = strip.Color(0,0,i);
        else
            c = strip.Color(i,i,i);
        for(int index = lowlight; index <= highlight; index++)
        {    
          strip.setPixelColor(index, c);
        }
        strip.show();
        
        delay(smdelay);
        totaltime += smdelay;
        i+=25;
      }
      while(i >=0)
      {
        if(hue == 1)
          c =strip.Color(i, 0, 0);
        else if (hue == 2)
            c = strip.Color(0,i,0);
        else if (hue == 3)
            c = strip.Color(0,0,i);
        else
            c = strip.Color(i,i,i);
        for(int index = lowlight; index <= highlight; index++)
        {    
          strip.setPixelColor(index, c);
...

This file has been truncated, please download it to see its full contents.

Credits

Dawn DuPriest

Dawn DuPriest

7 projects • 17 followers
I teach math and technology to junior high and high school students at a small project-based school... and I make stuff.

Comments