Dawn DuPriestDawn Dupriest
Published © CC BY

Bluetooth Enabled Ugly Christmas Sweater

I wanted an interactive Christmas sweater this year, and finally got it pulled together with NeoPixels and a Flora Bluefruit LE.

IntermediateFull instructions provided18 hours499

Things used in this project

Hardware components

Flora RGB Neopixel LEDs- Pack of 4
Adafruit Flora RGB Neopixel LEDs- Pack of 4
×3
Flora
Adafruit Flora
×1
Adafruit Flora Bluefruit LE
×1
Lilypad MP3 Board
SparkFun Lilypad MP3 Board
×1
Li-Ion Battery 100mAh
Li-Ion Battery 100mAh
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
Sewable Conductive Thread
Sewable Conductive Thread
×1
SparkFun speaker
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
SparkFun needle
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Hand-drawn wiring diagram

Sorry. Too lazy to use Fritzing or anything fancy :/

Code

BlueToothSweater Code

Arduino
This code gets uploaded to the Flora. This is adapted from the example code for the Flora Bluefruit LE. The Lilypad MP3 code will be attached separately.
#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(12, 9, NEO_GRB + NEO_KHZ800);
int pixelsinorder[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

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;

/**************************************************************************/
/*!
    @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)
{
  // check for time over in the minigame
  if(playinggame)
  {
  curtime = millis();
  if(curtime >= stopgametime)
  {
     colorMiniStrip(strip.Color(0,0,0), 0, 11, 800);
     for(int h = 0; h < score; h++)
     {
     colorMiniStrip(strip.Color(127,127,127), 0, 11, 300);  
     colorMiniStrip(strip.Color(0,0,0), 0, 11, 300);
     }
     playinggame = false;
     score = 0;
   }
  }
  /* Wait for new data to arrive */
  uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
  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)
      {
        playinggame = false; // stop the minigame if running
        digitalWrite(6, HIGH);
        Nightmare();
        digitalWrite(6, LOW);
      }
      else if(buttnum == 2)
      {
        playinggame = false; // stop the minigame if running
        digitalWrite(12, HIGH);
        Mariah();
        digitalWrite(12, LOW);
        
      }
      else if(buttnum == 3)
      {
        playinggame = false; // stop the minigame if running
        digitalWrite(10, HIGH);
        delay(250);
        digitalWrite(10, LOW);
      }
      else if(buttnum == 4) // play a minigame
      {
        // start the game
        playinggame = true;
        gametimer = millis();
        curtime = millis();
        stopgametime = curtime + 20000; // 20 second game
        score = 0;
        targetpos = (int)random(0,12);
        playerpos = (int)random(0,12);
        // don't let the player and target be on top of each other
        while(targetpos == playerpos)
              targetpos = (int)random(0,12);
        
        colorMiniStrip(strip.Color(0,0,0),0,11,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 4 as the "topmost" pixel. these move you toward pixel 4
        if(buttnum == 5)
        {
          
          if(playerpos == 11) 
          {
            playerpos = 0;
            changedpos = true;
          }
          else if(playerpos == 10)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos >= 0 && playerpos <= 3)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos <= 9 && playerpos >= 5)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          
        }
        // 6 is down. 10 is the bottommost pixel
        else if(buttnum == 6)
        {
          if(playerpos == 0) 
          {
            playerpos = 11;
            changedpos = true;
          }
          else if(playerpos == 11)
          {
            playerpos = 10;
            changedpos = true;
          }
          else if(playerpos >= 1 && playerpos <= 4)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos <= 9 && playerpos >= 4)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
        }
        // left. 6 is the leftmost pixel
        else if(buttnum == 7)
        {
          if(playerpos == 0) 
          {
            playerpos = 11;
            changedpos = true;
          }
          else if(playerpos == 1)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos <= 5 && playerpos >= 2)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos >= 7 && playerpos <= 11)
          {
            playerpos = playerpos - 1;
            changedpos = true; 
          }
        }
        // right. 2 is the rightmost pixel
        else if(buttnum == 8)
        {
          if(playerpos == 11) 
          {
            playerpos = 0;
            changedpos = true;
          }
          else if(playerpos == 1 || playerpos == 0)
          {
            playerpos = playerpos + 1;
            changedpos = true;
          }
          else if(playerpos <= 6 && playerpos >= 3)
          {
            playerpos = playerpos - 1;
            changedpos = true;
          }
          else if(playerpos >= 7 && playerpos <= 10)
          {
            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,12);
            targetpos = (int)random(0,12);
            while(targetpos == playerpos)
              targetpos = (int)random(0,12);
          }


          colorMiniStrip(strip.Color(0,0,0),0,11,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
    }
    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 == 12)
  {
    for(int i = 0; i <= 11; 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 <= 11; i=i+3) {
        lightnum = i+q;
        if(lightnum > 11)
        {
          lightnum = lightnum - 11 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);
      totaltime += wait;

      for (uint16_t i=0; i <= 11; i=i+3) {
        lightnum = i+q;
        if(lightnum > 11)
        {
          lightnum = lightnum - 11 - 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 <= 11; i=i+3) {
        lightnum = i+q;
        if(lightnum > 11)
        {
          lightnum = lightnum - 11 - 1;
        }
        strip.setPixelColor(pixelsinorder[lightnum], Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);
      for (uint16_t i=0; i <= 11; i=i+3) {
         lightnum = i+q;
        if(lightnum > 11)
        {
          lightnum = lightnum - 11 - 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 Mariah()
 {
  int bpm = 60;
  int msperbeat = 60000 / bpm;
  //delay(500);
  colorWipeDurationMini(strip.Color(200,0,0),0, 11, (msperbeat * 6)-100);
  if(needToCancel()) return;
  theaterChaseRainbowDuration(50,(msperbeat * 6)-50);
  if(needToCancel()) return;
  colorWipeDurationMini(strip.Color(0,127,0),0, 2, msperbeat*2);
  
  colorWipeDurationMini(strip.Color(127,0,0),6, 8, msperbeat*2);
  
  colorWipe(strip.Color(0,0,0),50);
  
  
  colorWipeDurationMini(strip.Color(127,127,127),3, 5, msperbeat);
  
  colorWipeDurationMini(strip.Color(127,127,127),9, 11, msperbeat-50);
  if(needToCancel()) return;
  theaterChaseRainbowDuration(50,msperbeat * 3);
  
  colorMiniStrip(strip.Color(0,0,127), 3, 5, msperbeat / 2);
  
  colorMiniStrip(strip.Color(0,0,127), 9, 11, msperbeat / 2);
  
  colorMiniStrip(strip.Color(0,0,127), 6, 8, msperbeat / 2);
  
  colorMiniStrip(strip.Color(0,0,127), 0, 2, msperbeat / 2);
  
  theaterChaseMiniDuration(strip.Color(127,127,127), 6,8, 50, msperbeat);
  
  theaterChaseMiniDuration(strip.Color(127,127,127), 0,2,50,  msperbeat);
  
  theaterChaseRainbowDuration(50,(msperbeat * 2)-50);
  if(needToCancel()) return;
  colorWipeDurationMini(strip.Color(0,255,0),0, 11, msperbeat * 4);
  
  theaterChaseDuration(strip.Color(127,0,127), 200, (msperbeat * 4)-50);
  if(needToCancel()) return;
  rainbowDuration(50, msperbeat * 4);
  
  theaterChaseMiniDuration(strip.Color(127,127,0),9,11, 100, msperbeat * 3 - 50);
  if(needToCancel()) return;
  colorWipeDurationMini(strip.Color(127,127,127), 3,5, msperbeat * 4);
  
  rainbowDuration(50, msperbeat * 4 - 30);
  if(needToCancel()) return;
  colorWipe(strip.Color(0,0,0),15);
  
  delay(400);
  for(int h = 0; h < 3; h++)
  {
  colorMiniStrip(strip.Color(127,127,127), 0, 11, msperbeat / 10);
  
  colorMiniStrip(strip.Color(0,0,0), 0, 11, msperbeat / 10);
  
  colorMiniStrip(strip.Color(127,127,127), 0, 11, msperbeat / 10 * 2);
  
  colorMiniStrip(strip.Color(0,0,0), 0, 11, msperbeat / 10);
  
  }
  colorMiniStrip(strip.Color(127,127,127), 0, 11, msperbeat / 10*2);
  
  colorMiniStrip(strip.Color(0,0,0), 0, 11, msperbeat / 10);
  

  // Just about perfect introduction
  // now for the main verse
  bpm = 75;
  msperbeat = (int)(60000 / bpm);

  brightenMiniDuration(3, 0, 11, (msperbeat * 4)-100);
 
  theaterChaseMiniDuration(strip.Color(127,127,127),0,11,100,msperbeat * 4);
  
  brightenMiniDuration(1, 0, 11, msperbeat * 4-50);
  if(needToCancel()) return;
  theaterChaseMiniDuration(strip.Color(127,127,127),0,11,100,msperbeat * 4-50);
  
  colorMiniStrip(strip.Color(0,127,0),6,8,msperbeat);
  
  colorMiniStrip(strip.Color(0,127,0),0,2,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),6,8,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),0,2,msperbeat);
  
  colorMiniStrip(strip.Color(0,127,0),3,5,msperbeat);
  
  colorMiniStrip(strip.Color(0,127,0),9,11,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),3,5,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),9,11,msperbeat);
  
  colorMiniStrip(strip.Color(127,0,0),6,8,msperbeat);
  
  colorMiniStrip(strip.Color(127,0,0),0,2,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),6,8,msperbeat);
  if(needToCancel()) return;
  colorMiniStrip(strip.Color(0,0,0),0,2,msperbeat);
  
  colorMiniStrip(strip.Color(127,0,0),3,5,msperbeat);
  
  colorMiniStrip(strip.Color(127,0,0),9,11,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),3,5,msperbeat);
  
  colorMiniStrip(strip.Color(0,0,0),9,11,msperbeat);
  

  theaterChaseMiniDuration(strip.Color(0,0,127),3,5,50,msperbeat);
  
  theaterChaseMiniDuration(strip.Color(0,0,127),9,11,50,msperbeat);
  
  theaterChaseMiniDuration(strip.Color(127,0,127),6,8,50,msperbeat);
 
  theaterChaseMiniDuration(strip.Color(127,0,127),0,2,50,msperbeat);
  if(needToCancel()) return;
  colorWipeDurationMini(strip.Color(127,0,0),0, 11, msperbeat * 2);
  
  colorWipeDurationMini(strip.Color(0,127,0),0, 11, msperbeat * 2);
  
...

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

LilyPad MP3 Positive Trigger Code

Arduino
When you upload this to the LilyPad MP3, use the settings for Arduino Pro or Arduino Pro Mini. You need to include libraries for the SD Card reader, available online. This code activates tracks 1 or 2 when the corresponding triggers register HIGH (or above analog 300) and stay there for 50 ms. The current track is cancelled if trigger 3 reads HIGH for 50 ms.
// "Trigger" example sketch for Lilypad MP3 Player
// Mike Grusin, SparkFun Electronics
// http://www.sparkfun.com

// This sketch (which is preloaded onto the board by default),
// will play a specific audio file when one of the five trigger
// inputs (labeled T1 - T5) is momentarily grounded.

// You can place up to five audio files on the micro-SD card.
// These files should have the desired trigger number (1 to 5)
// as the first character in the filename. The rest of the
// filename can be anything you like. Long file names will work,
// but will be translated into short 8.3 names. We recommend using
// 8.3 format names without spaces, but the following characters
// are OK: .$%'-_@~`!(){}^#&. The VS1053 can play a variety of
// audio formats, see the datasheet for information.

// By default, a new trigger will interrupt a playing file, except
// itself. (In other words, a new trigger won't restart an
// already-playing file). You can easily change this behavior by
// modifying the global variables "interrupt" and "interruptself"
// below.

// This sketch can output serial debugging information if desired
// by changing the global variable "debugging" to true. Note that
// this will take away trigger inputs 4 and 5, which are shared
// with the TX and RX lines. You can keep these lines connected to
// trigger switches and use the serial port as long as the triggers
// are normally open (not grounded) and remain ungrounded while the
// serial port is in use.

// Uses the SdFat library by William Greiman, which is supplied
// with this archive, or download from http://code.google.com/p/sdfatlib/

// Uses the SFEMP3Shield library by Bill Porter, which is supplied
// with this archive, or download from http://www.billporter.info/

// License:
// We use the "beerware" license for our firmware. You can do
// ANYTHING you want with this code. If you like it, and we meet
// someday, you can, but are under no obligation to, buy me a
// (root) beer in return.

// Have fun!
// -your friends at SparkFun

// Revision history:
// 1.0 initial release MDG 2012/11/01


// We'll need a few libraries to access all this hardware!

#include <SPI.h>            // To talk to the SD card and MP3 chip
#include <SdFat.h>          // SD card file system
#include <SFEMP3Shield.h>   // MP3 decoder chip

// Constants for the trigger input pins, which we'll place
// in an array for convenience:

const int TRIG1 = A0;
const int TRIG2 = A4;
const int TRIG3 = A5;
const int TRIG4 = 1;
const int NUMTRIGGERS = 3;
const int TRIG5 = 0;
int trigger[NUMTRIGGERS] = {TRIG1, TRIG2, TRIG3}; // TRIG4}; //dmd

// And a few outputs we'll be using:

const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
const int SD_CS = 9;     // Chip Select for SD card

// Create library objects:

SFEMP3Shield MP3player;
SdFat sd;

// Set debugging = true if you'd like status messages sent
// to the serial port. Note that this will take over trigger
// inputs 4 and 5. (You can leave triggers connected to 4 and 5
// and still use the serial port, as long as you're careful to
// NOT ground the triggers while you're using the serial port).

boolean debugging = false;

// Set interrupt = false if you would like a triggered file to
// play all the way to the end. If this is set to true, new
// triggers will stop the playing file and start a new one.

boolean interrupt = false;

// Set interruptself = true if you want the above rule to also
// apply to the same trigger. In other words, if interrupt = true
// and interruptself = false, subsequent triggers on the same
// file will NOT start the file over. However, a different trigger
// WILL stop the original file and start a new one.

boolean interruptself = false;

// We'll store the five filenames as arrays of characters.
// "Short" (8.3) filenames are used, followed by a null character.

char filename[3][13];


void setup()
{
  int x, index;
  SdFile file;
  byte result;
  char tempfilename[13];

  // Set the five trigger pins as inputs, and turn on the
  // internal pullup resistors:

  for (x = 0; x <= (NUMTRIGGERS-1); x++)
  {
    pinMode(trigger[x], INPUT);
    //digitalWrite(trigger[x],HIGH);
  }
 // pinMode(TRIG3, OUTPUT);
  

  // If serial port debugging is inconvenient, you can connect
  // a LED to the red channel of the rotary encoder to blink
  // startup error codes:

  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW); // HIGH = off

  // The board uses a single I/O pin to select the
  // mode the MP3 chip will start up in (MP3 or MIDI),
  // and to enable/disable the amplifier chip:

  pinMode(EN_GPIO1, OUTPUT);
  digitalWrite(EN_GPIO1, LOW); // MP3 mode / amp off


  // If debugging is true, initialize the serial port:
  // (The 'F' stores constant strings in flash memory to save RAM)
  Serial.begin(9600);
  if (debugging)
  {

    Serial.println(F("Lilypad MP3 Player trigger sketch"));
  }

  // Initialize the SD card; SS = pin 9, half speed at first

  if (debugging) Serial.print(F("initialize SD card... "));

  result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success

  if (result != 1) // Problem initializing the SD card
  {
    if (debugging) Serial.print(F("error, halting"));
    errorBlink(1); // Halt forever, blink LED if present.
  }
  else if (debugging) Serial.println(F("success!"));

  // Start up the MP3 library

  if (debugging) Serial.print(F("initialize MP3 chip... "));

  result = MP3player.begin(); // 0 or 6 for success

  // Check the result, see the library readme for error codes.

  if ((result != 0) && (result != 6)) // Problem starting up
  {
    if (debugging)
    {
      Serial.print(F("error code "));
      Serial.print(result);
      Serial.print(F(", halting."));
    }
    errorBlink(result); // Halt forever, blink red LED if present.
  }
  else if (debugging) Serial.println(F("success!"));

  // Now we'll access the SD card to look for any (audio) files
  // starting with the characters '1' to '5':

  if (debugging) Serial.println(F("reading root directory"));

  // Start at the first file in root and step through all of them:

  sd.chdir("/", true);
  while (file.openNext(sd.vwd(), O_READ))
  {
    // get filename

    file.getFilename(tempfilename);

    // Does the filename start with char '1' through '5'?

    if (tempfilename[0] >= '1' && tempfilename[0] <= '5')
    {
      // Yes! subtract char '1' to get an index of 0 through 4.

      index = tempfilename[0] - '1';

      // Copy the data to our filename array.

      strcpy(filename[index], tempfilename);

      if (debugging) // Print out file number and name
      {
        Serial.print(F("found a file with a leading "));
        Serial.print(index + 1);
        Serial.print(F(": "));
        Serial.println(filename[index]);
      }
    }
    else if (debugging)
    {
      Serial.print(F("found a file w/o a leading number: "));
      Serial.println(tempfilename);
    }

    file.close();
  }

  if (debugging)
    Serial.println(F("done reading root directory"));

  if (debugging) // List all the files we saved:
  {
    for (x = 0; x <= (NUMTRIGGERS-1); x++)
    {
      Serial.print(F("trigger "));
      Serial.print(x + 1);
      Serial.print(F(": "));
      Serial.println(filename[x]);
    }
  }

  // Set the VS1053 volume. 0 is loudest, 255 is lowest (off):

  MP3player.setVolume(10, 10);

  // Turn on the amplifier chip:

  digitalWrite(EN_GPIO1, HIGH);
  delay(2);
}


void loop()
{
  int t;              // current trigger
  static int last_t;  // previous (playing) trigger
  int x;
  byte result;
  boolean is_triggered;

  // Step through the trigger inputs, looking for LOW signals.
  // The internal pullup resistors will keep them HIGH when
  // there is no connection to the input.

  // If serial debugging is on, only check triggers 1-3,
  // otherwise check triggers 1-5.
  //
  if(!MP3player.isPlaying())
    digitalWrite(A1, LOW);
    //digitalWrite(ROT_LEDR, LOW);
  for (t = 1; t <= (NUMTRIGGERS); t++)
  {
    is_triggered = false;
    // The trigger pins are stored in the inputs[] array.
    // Read the pin and check if it is LOW (triggered).
    int trig = analogRead(trigger[t - 1]);
    //Serial.print("trigger: ");
    //Serial.print(t - 1);
    //Serial.print(" = ");
    //Serial.println(trig);
    if (trig > 300)//dmd
    {
      // Wait for trigger to return low for a solid 50ms
      // (necessary to avoid switch bounce on T2 and T3
      // since we need those free for I2C control of the
      // amplifier)
      is_triggered = true;
      digitalWrite(A1, HIGH);
      x = 0;
      while (x < 50)
      {
        if (analogRead(trigger[t - 1]) > 300 )
        {
          //x++;
          x++;
        }
        else
        {
          //x = 0;
          x++;
          is_triggered = false;
        }
        delay(1);
      }
      if(is_triggered)
      {
        
        if(t == 3 && MP3player.isPlaying())
        {
            MP3player. Track();
            return;
        }

        if (debugging)
        {
          Serial.print(F("got trigger "));
          Serial.println(t);
        }

      // Do we have a valid filename for this trigger?
      // (Invalid filenames will have 0 as the first character)

        if (filename[t - 1][0] == 0)
        {
          if (debugging)
            Serial.println(F("no file with that number"));
        }
        else // We do have a filename for this trigger!
        {
          // If a file is already playing, and we've chosen to
          // allow playback to be interrupted by a new trigger,
          // stop the playback before playing the new file.
  
          if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself))
          {
            if (debugging)
              Serial.println(F("stopping playback"));
  
            MP3player.stopTrack();
            return;
            
          }

          // Play the filename associated with the trigger number.
          // (If a file is already playing, this command will fail
          //  with error #2).
  
          result = MP3player.playMP3(filename[t - 1]);
          
  
          if (result == 0) last_t = t;  // Save playing trigger
  
          if (debugging)
          {
            if (result != 0)
            {
              Serial.print(F("error "));
              Serial.print(result);
              Serial.print(F(" when trying to play track "));
            }
            else
            {
              Serial.print(F("playing "));
            }
            Serial.println(filename[t - 1]);
          }
         }
       }
     }
   }
 }


void errorBlink(int blinks)
{
  // The following function will blink the red LED in the rotary
  // encoder (optional) a given number of times and repeat forever.
  // This is so you can see any startup error codes without having
  // to use the serial monitor window.

  int x;

  while (true) // Loop forever
  {
    for (x = 0; x < blinks; x++) // Blink the given number of times
    {
      digitalWrite(ROT_LEDR, LOW); // Turn LED ON
      delay(250);
      digitalWrite(ROT_LEDR, HIGH); // Turn LED OFF
      delay(250);
    }
    delay(1500); // Longer pause between blink-groups
  }
}

Credits

Dawn DuPriest

Dawn DuPriest

3 projects • 4 followers
Dawn Dupriest

Dawn Dupriest

4 projects • 5 followers
Middle School Computer Science teacher, Feminist, Maker. 2012 Poudre School District Teacher of the Year. 2016 Allen Distinguished Educator.

Comments