! [exclamation mark]

So this is my big exclamation mark for Halloween.

It started from soldering the sound board, which was, well, quite painful.

(this is how it looks like after two days of soldering – unsoldering – soldering again 😦

This it is time for testing how the sensor works. It turns out it works without any driver, just plug in TWO power cables and TWO ground cables and one data cable, the result can be viewed immediately in the monitor.

Then to combine the different parts and let it work. The basic idea is, let the distance sensor detect the distance and when it reaches a value (here it is actually the value of 1000/d, since the noise is plentiful and it has an exponential growth), it turns the light on and sound board starts playing the music preset in the sd card.

The 3d printed exclamation marks has 0.8 mm walls so in dark environment it can be easily seen when the led is on.

The problem here is that the sound board cannot work simultaneously with other components, so when it starts to work, the light will stay on after the music is off.

Here is the code.

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

  #include <avr/power.h>

#endif

#define PIN            6

#define NUMPIXELS      16

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

int delayval = 1;

#include <WaveHC.h>

#include <WaveUtil.h>

SdReader card;

FatVolume vol;

FatReader root;

WaveHC wave;

uint8_t dirLevel;

dir_t dirBuf;

#define error(msg) error_P(PSTR(msg))

void play(FatReader &dir);

void setup() {

#if defined (__AVR_ATtiny85__)

  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);

#endif

  pixels.begin();

  Serial.begin(9600);

  putstring_nl(“\nWave test!”);

  putstring(“Free RAM: “);

  Serial.println(FreeRam());

  //  if (!card.init(true)) {

  if (!card.init()) {

    error(“Card init. failed!”);

  }

  card.partialBlockRead(true);

  uint8_t part;

  for (part = 0; part < 5; part++) {

    if (vol.init(card, part))

      break;

  }

  if (part == 5) {

    error(“No valid FAT partition!”);

  }

  putstring(“Using partition “);

  Serial.print(part, DEC);

  putstring(, type is FAT”);

  Serial.println(vol.fatType(), DEC);

  if (!root.openRoot(vol)) {

    error(“Can’t open root dir!”);

  }

  putstring_nl(“Files found (* = fragmented):”);

  root.ls(LS_R | LS_FLAG_FRAGMENTED);

}

void loop() {

  int sensorValue = analogRead(A5);

  if (1000/sensorValue < 2){

    for(int i=0;i<NUMPIXELS;i++){

      pixels.setPixelColor(i, pixels.Color(255,0,0));

      pixels.show();

      delay(delayval);

     }

    root.rewind();

    play(root);

  }

    else {

       for(int i=0;i<NUMPIXELS;i++){

      pixels.setPixelColor(i, pixels.Color(0,0,0));

      pixels.show();

      delay(delayval);

   }

 }

}

void error_P(const char *str) {

  PgmPrint(“Error: “);

  SerialPrint_P(str);

  sdErrorCheck();

  while(1);

}

void sdErrorCheck(void) {

  if (!card.errorCode()) return;

  PgmPrint(“\r\nSD I/O error: “);

  Serial.print(card.errorCode(), HEX);

  PgmPrint(,);

  Serial.println(card.errorData(), HEX);

  while(1);

}

void play(FatReader &dir) {

  FatReader file;

  while (dir.readDir(dirBuf) > 0) {

    if (!DIR_IS_SUBDIR(dirBuf)

         && strncmp_P((char *)&dirBuf.name[8], PSTR(“WAV”), 3)) {

      continue;

    }

    Serial.println();

    for (uint8_t i = 0; i < dirLevel; i++) {

       Serial.write(‘ ‘);

    }

    if (!file.open(vol, dirBuf)) {

      error(“file.open failed”);

    }

    if (file.isDir()) {

      putstring(“Subdir: “);

      printEntryName(dirBuf);

      Serial.println();

      dirLevel += 2;

      play(file);

      dirLevel -= 2;

    }

    else {

      putstring(“Playing “);

      printEntryName(dirBuf);

      if (!wave.create(file)) {

        putstring(” Not a valid WAV”);

      } else {

        Serial.println();

        wave.play();

        uint8_t n = 0;

        while (wave.isplaying) {

          putstring(“.”);

          if (!(++n % 32))Serial.println();

          delay(100);

        }

        sdErrorCheck();

      }

    }

  }

}

Author: Hui Zheng

From China. Product designer.