First Arduino Project WIP II

My first Arduino project, coded “LW” (short for Last Words, before I come up with a proper name), will be an interactive installation allowing the participant to witness the last moment of a fictional character from a film. The experience will be highly symbolic. The character’s death will be represented by a light that turns on and off, and a robotic voice uttering the last words.


Last week I made some significant progress.

Here is a video that shows what I’ve got so far:

First Arduino Project WIP II from Carrie Sijia Wang on Vimeo.

Got a Light?

The most important visual element of this piece would be the light. I want something of simple, minimal design, but with a vintage touch.

I decided to go with a table lamp, which would be easy to install in any environment, and would look natural (it’s a common household item), and yet not natural (with its minimal design and deliberate placement).

I bought a vintage looking LED light bulb online, which is perfect because LED lights are more durable and better energy savers.  Then I combined the light bulb with a beautiful vintage light fixture I bought on Etsy.

light

 

Wiring it up!

Instead of making my own relay, I used a powerswitch tail I bought on Adafruit. Here is the link to the product: https://www.adafruit.com/products/268

I connected the powerswitch tail, one end to the lamp, the other end to Arduino, three separate wires run into Arduino’s power, ground and pin 19.

I soldered a speaker to my wave shield. Then I wired up the small circuit with the distance sensor (HC-SR04 range finder) occupying pin 14 (trig) and pin 16 (echo)

I also bought a battery for Arduino so I don’t have to have it connected to my laptop all the time.

 

Code

I also got the code to work the way I wanted. The following sketch tells Arduino to run the subroutine “LW” when something is within 10cm of the distance sensor. In the subroutine, the lamp turns on, a random sound file is played, then the light turns off.

#include 
#include 
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"


SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

 
uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads


/*
 * Define macro to put error messages in flash memory
 */
#define error(msg) error_P(PSTR(msg))

#define trigPin 14
#define echoPin 16
#define CLOSECIRCUIT 19

long randNumber;

void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println(F("Wave test!"));

  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(CLOSECIRCUIT, OUTPUT);


  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());

  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
   
    while (1);                           // then 'halt' - do nothing!
  }



  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {   // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                           // we found one, lets bail
  }
  if (part == 5) {                     // if we ended up not finding one  :(
    Serial.println(F("No valid FAT partition!"));  // Something went wrong, lets print out why
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  Serial.print(F(", type is FAT"));
  Serial.println(vol.fatType(), DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    Serial.println(F("Can't open root dir!"));      // Something went wrong,
  }
  
  // Whew! We got past the tough parts.
  Serial.println(F("Files found (* = fragmented):"));

  // Print out all of the files in all the directories.
  root.ls(LS_R | LS_FLAG_FRAGMENTED);

    randomSeed(analogRead(1));
}


void loop ()
{
  int duration, distance;
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds (500);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  Serial.print (distance);
  Serial.println("cm");
  delay(500); 
    
  if (distance < 10)
  {
    LW ();
  }

}
void LW()
{ 
  
  digitalWrite(CLOSECIRCUIT, LOW);

  randNumber = random(1,5);
  Serial.println(randNumber);

  delay (2000);
  
  if (randNumber == 1)
  {
  playcomplete("LW1.WAV");
  }
  else if (randNumber == 2)
  {
    playcomplete ("LW2.WAV");
  }
  else if (randNumber == 3)
  {
    playcomplete ("LW3.WAV");
  }
  else if (randNumber == 4)
  {
    playcomplete ("LW4.WAV");
  }
  else if (randNumber == 5)
  {
    playcomplete ("LW5.WAV");
  }

   delay (1000);
  
  digitalWrite(CLOSECIRCUIT, HIGH);
  delay (10000);
}

// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
  
  // ok time to play! start playback
  wave.play();
}
}