Dana Kemp Liao
Published

Sorting Hat - Harry Potter Wonderland

Take the quiz now, which house would you have been sorted into at Hogwarts School of Witchcraft and Wizardry in Harry Potter?

BeginnerProtip3 hours444
Sorting Hat - Harry Potter Wonderland

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
https://www.seeedstudio.com/Arduino-Uno-Rev3-p-2995.html
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Seeed Studio Grove Base Shield V2.0 for Arduino
Seeed Studio Grove - MP3 V3 -Music Player
Seeed Studio Grove - WS2813 RGB LED Strip Waterproof - 60 LED/m - 1m
Seeed Studio Grove - Universal 4 Pin Buckled 20cm Cable (5 PCs pack)
Seeed Studio Mono Enclosed Speaker - 2W 6 Ohm
Amazon Web Services Hat

Story

Read more

Schematics

rawenclod_aK2rxDzvDE.mp3

azkban_Abo5cmWwa7.mp3

greenfendo_4wBVnDGPiT.mp3

halfpath2_soImKKsxff.mp3

silatelin_lOBBfdGu0F.mp3

Code

Hat_Player.ino

C/C++
#include <MP3Player_KT403A.h>

#include "WT2003S_Player.h"

#include "Adafruit_NeoPixel.h"
 
#define BUTTON_PIN   5    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.
 
#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.
 
#define PIXEL_COUNT 42
 
// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
 
bool oldState = LOW; 
int showType = 0;
int setupReady = 0;
#ifdef __AVR__
    #include <SoftwareSerial.h>
    SoftwareSerial SSerial(2, 3); // RX, TX
    #define COMSerial SSerial
    #define ShowSerial Serial

    WT2003S<SoftwareSerial> Mp3Player;
#endif

#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
    #define COMSerial Serial1
    #define ShowSerial SerialUSB

    WT2003S<Uart> Mp3Player;
#endif

#ifdef ARDUINO_ARCH_STM32F4
    #define COMSerial Serial
    #define ShowSerial SerialUSB

    WT2003S<HardwareSerial> Mp3Player;
#endif


uint8_t vol = 10;
uint32_t spi_flash_songs = 0;
uint32_t sd_songs = 0;
STROAGE workdisk = SD;
struct Play_history {
    uint8_t disk;
    uint16_t index;
    char name[8];
}* SPISong, *SDSong;

void readSongName(struct Play_history* ph, uint32_t num, STROAGE disk) {
    Mp3Player.volume(0);
    // delay(10);
    switch (disk) {
        case SPIFLASH:
            Mp3Player.playSPIFlashSong(0x0001);
            break;
        case SD:
            Mp3Player.playSDRootSong(0x0001);
            break;
        case UDISK:
            Mp3Player.playUDiskRootSong(0x0001);
            break;
    }
    for (int i = 0; i < num ; i++) {
        // delay(300);
        ph[i].disk = disk;
        ph[i].index = Mp3Player.getTracks();
        Mp3Player.getSongName(ph[i].name);
        Mp3Player.next();
    }
    Mp3Player.pause_or_play();
    // Mp3Player.volume(14);
    // delay(100);
}

void getAllSong() {
    uint8_t diskstatus = Mp3Player.getDiskStatus();
    // ShowSerial.println(diskstatus);
    spi_flash_songs = Mp3Player.getSPIFlashMp3FileNumber();
    // ShowSerial.print("SPIFlash:");
    // ShowSerial.println(spi_flash_songs);
    // if (spi_flash_songs > 0) {
    //     startShow(3);
    //     SPISong = (struct Play_history*)malloc((spi_flash_songs + 1) * sizeof(struct Play_history));
    //     readSongName(SPISong, spi_flash_songs, SPIFLASH);
    // }
    if (diskstatus && 0x02) { // have SD
        startShow(4);
        sd_songs = Mp3Player.getSDMp3FileNumber();
        if (sd_songs > 0) {
            startShow(5);
            SDSong = (struct Play_history*)malloc((sd_songs + 1) * sizeof(struct Play_history));
            readSongName(SDSong, sd_songs, SD);
        }
    }  
}


void setup() {
    // while (!ShowSerial);
    // ShowSerial.begin(9600);
    pinMode(BUTTON_PIN, INPUT_PULLUP);
    strip.setBrightness(30);
    strip.begin();
    strip.show();
    startShow(1);
    COMSerial.begin(9600);
    Mp3Player.init(COMSerial);
    getAllSong();
    setupReady = 1;
    startShow(0);  
}

void loop() {
  if (setupReady == 0) {
    return;
  }
  bool newState = digitalRead(BUTTON_PIN);
  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    delay(100);
    if (setupReady == 1) {
      setupReady = 2;
      Mp3Player.volume(10);
    }
    
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType+=1;
      if (showType >= sd_songs) {
          showType=0;
      }
      int randNumber = random(1, 12);
      if (randNumber % 2 == 0) {
        randNumber = (randNumber + 1) % 12;
      }
      startShow(randNumber);
      Mp3Player.playSDRootSong(randNumber); 
    }
  }
  oldState = newState;
  return;
    if (1) {
        char cmd = 'n';
        Mp3Player.playSDRootSong(5);
        switch (cmd) {
            case '+': {
                    vol = Mp3Player.getVolume();
                    Mp3Player.volume(++vol);
                    break;
                }
            case '-': {
                    vol = Mp3Player.getVolume();
                    if (--vol > 31) {
                        vol = 0;
                    }
                    Mp3Player.volume(vol);
                    break;
                }
            case 't': {
                    uint8_t status;
                    status = Mp3Player.getStatus();
                    if (status == 0x01) {
                    }
                    if (status == 0x02) {
                    }
                    if (status == 0x03) {
                    }
                    break;
                }
            case 'n': {
                    Mp3Player.next();
                    break;
                }
            case 'p': {
                    Mp3Player.pause_or_play();
                    break;
                }
            case 'w': {
                    Mp3Player.playMode(SINGLE_CYCLE);
                    break;
                }
            case 'x': {
                    Mp3Player.playMode(SINGLE_CYCLE);
                    break;
                }
            case 'y': {
                    Mp3Player.playMode(CYCLE);
                    break;
                }
            case 'z': {
                    Mp3Player.playMode(RANDOM);
                    break;
                }
            case 'c': {
                    // ShowSerial.print(Mp3Player.copySDtoSPIFlash());
                    break;
                }
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                // ShowSerial.print("play:");
                if (workdisk == SD) {
                    Mp3Player.playSDRootSong(cmd - '0' - 1);
                    // ShowSerial.print(cmd + ": ");
                    // ShowSerial.print(SDSong[cmd - '0'].name);
                }
                if (workdisk == SPIFLASH) {
                    Mp3Player.playSPIFlashSong(cmd - '0' - 1);
                    // ShowSerial.print(cmd + ": ");
                    // ShowSerial.print(SPISong[cmd - '0'].name);
                }
                // ShowSerial.println();
                break;
            default:
                break;
        }
    }
}

void startShow(int i) {
  i = i % 12;
  switch(i) {
    case 0: colorWipe(strip.Color(0, 0, 0), 10);    // 
            break;
    case 1: colorWipe(strip.Color(0, 255, 255), 10);  // 
            break;
    case 2: colorWipe(strip.Color(255, 215, 0), 10);  // 
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 10);  // Blue
            break;
    case 4: theaterChase(strip.Color(127, 127, 127), 10); // White
            break;
    case 5: theaterChase(strip.Color(155,   97,   0), 10); // 
            break;
    case 6: theaterChase(strip.Color(127,   0, 127), 10); // 
            break;
    case 7: theaterChase(strip.Color(127, 127, 0), 10); // 
            break;
    case 8: theaterChase(strip.Color(  0,   127, 127), 10); // 
            break;
    case 9: theaterChase(strip.Color(  255,   0, 0), 10); // 
            break;
    case 10: theaterChase(strip.Color(  0,   255, 0), 10); // 
            break;
    case 11: theaterChase(strip.Color(  0,   0, 255), 10); // 
            break;
    case 12: theaterChase(strip.Color(  0,   0, 127), 10); // Blue
            break;
  }
}
 
// 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 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);
  }
}
 
// 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 (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
 
      delay(wait);
 
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}
 
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();
 
      delay(wait);
 
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}
 
// 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);
}

Credits

Dana Kemp Liao

Dana Kemp Liao

1 project • 1 follower
Thanks to Hardware design and production.

Comments