Motion-sensing sound box, I really need to understand, could use help!

This is my first Arduino project and I’m pretty terrible at this. I’ve created a box with 8 photocells in it, and I want each one to play a different sound when your hand is in front of it (the C major scale, specifically).

I built the wave shield and the sounds are playing fine when playing them in a basic sequence. But I am trying to write the code to make it responsive now and I cannot get it to work! I could be entirely wrong in how I wrote this but being that I can’t find any tutorials doing exactly what I am, I parsed bits of different tutorials I found together. And I’m not getting any results lol. I need this done hopefully by tonight so ANY AND ALL HELP IS MUCH APPRECIATED!!!

#include <FatReader.cpp>
#include <FatReader.h>
#include <SdReader.cpp>
#include <SdReader.h>
#include "WaveHC.h"
#include <WaveUtil.cpp>
#include "WaveUtil.h"



int sensorPinC4 = 0; 
int sensorPinD4 = 1;
int sensorPinE4 = 6;
int sensorPinF4 = 7;
int sensorPinG4 = 8;
int sensorPinA4 = 9;
int sensorPinB4 = 11;
int sensorPinC5 = 12;
				// select the input pin for the photocell
int sensorValueC4 = 0;		// variable to store the value coming from the photocell
int sensorValueD4 = 0;
int sensorValueE4 = 0;
int sensorValueF4 = 0;
int sensorValueG4 = 0;
int sensorValueA4 = 0;
int sensorValueB4 = 0;
int sensorValueC5 = 0;

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;


void setup() {
  Serial.begin(9600); 				//Set baud rate to 9600 on the Arduino
}

void playfile(char *name) {
  wave.play();
}

void loop() {
                                          // read the value from the sensor
  sensorValueC4 = digitalRead(sensorPinC4);  //get the voltage value from input pin
  Serial.println(sensorValueC4); 		 //print the value to Serial monitor
  delay(2000);                            //delay for 2 seconds

  if (sensorValueC4 < 300) {
    playfile("C4NOTE.WAV");   
  }    //play sound
  
  sensorValueD4 = digitalRead(sensorPinD4);  
  Serial.println(sensorValueD4); 
  delay(2000);  

  if (sensorValueD4 < 300) {
    playfile("D4NOTE.WAV");
  }
  
   sensorValueE4 = digitalRead(sensorPinE4);  
  Serial.println(sensorValueE4); 
  delay(2000);  

  if (sensorValueE4 < 300) {
    playfile("E4NOTE.WAV");
  }
  
   sensorValueF4 = digitalRead(sensorPinF4);  
  Serial.println(sensorValueF4); 
  delay(2000);  

  if (sensorValueF4 < 300) {
    playfile("F4NOTE.WAV");
  }
  
   sensorValueG4 = digitalRead(sensorPinG4);  
  Serial.println(sensorValueG4); 
  delay(2000);  

  if (sensorValueG4 < 300) {
    playfile("G4NOTE.WAV");
  }
  
   sensorValueA4 = digitalRead(sensorPinA4);  
  Serial.println(sensorValueA4); 
  delay(2000);  

  if (sensorValueA4 < 300) {
    playfile("A4NOTE.WAV");
  }
  
   sensorValueB4 = digitalRead(sensorPinB4);  
  Serial.println(sensorValueB4); 
  delay(2000);  

  if (sensorValueB4 < 300) {
    playfile("B4NOTE.wav");
  }
  
   sensorValueC5 = digitalRead(sensorPinC5);  
  Serial.println(sensorValueC5); 
  delay(2000);  

  if (sensorValueC5 < 300) {
    playfile("C5NOTE.WAV");
  }
}

carribou: I cannot get it to work!

What's it supposed to do, and what does it actually do?

Have you tested the capability to detect motion and play sounds independently?

PeterH:

carribou:
I cannot get it to work!

What’s it supposed to do, and what does it actually do?

Have you tested the capability to detect motion and play sounds independently?

It is supposed to take the input from the 8 photocells (each attached to separate digital I/O spots) and when, for example, the cell in spot 0 goes dark, it plays C4NOTE.WAV; when the cell in spot 1 goes dark, it plays D4NOTE.WAV; etc.

I was able to get it playing all the sounds on a loop just fine, without any input from the photocells. However, I did not test the photocells individually, since this is a class project and I ran out of time I just soldered them right on and went into programming.

However, I think if I can get all the correct code, then if it still doesn’t work I’ll know it’s a hardware issue, which I am fairly confident I can fix. I have worked with electronics before, just not Arduino.

I have tried several variations of parsed code from tutorials I’ve found, and tips across these forums. However, I can not get it to respond to any input from the photocells. And the code I pasted here in the original post wouldn’t even compile. I have since made minor changes, which allow the code to compile successfully, but I am still not getting a response from the photocells.

This is what I have now. Which compiles, but has no effect.

#include <FatReader.h>
#include <SdReader.h>
#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

#define DEBOUNCE 100  // button debouncer

// this handy function will return the number of bytes currently free in RAM, great for debugging!   
int freeRam(void)
{
  extern int  __bss_end; 
  extern int  *__brkval; 
  int free_memory; 
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end); 
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval); 
  }
  return free_memory; 
} 

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}

int inpin = 0;
int val = 0;
 
void setup() {
  // set up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with 6 buttons");
  
   putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
  
  pinMode(inpin, INPUT);
 
  //  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
    sdErrorCheck();
    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  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Ready!");
}

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();
}

int checkSensor(){  
  val = digitalRead(0);  
  if (val < 300) {
      playfile("C4NOTE.WAV");
  }
} 

void loop() {
  checkSensor();
}
int checkSensor(){  
  val = digitalRead(0);  
  if (val < 300) {
      playfile("C4NOTE.WAV");
  }
} 

void loop() {
  checkSensor();
}

One major problem here is that you are going to call this thousands of times a second. And the first thing playfile does is:

 if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }

So you need to do something like: don’t play a file if one is playing, or maybe don’t play the same file again if it is already playing.

So you need to do something like: don't play a file if one is playing, or maybe don't play the same file again if it is already playing.

Do you have any suggestions on how I could add that funcitonality? :cold_sweat:

Also, I'm not using a breadboard with this. I have the photocells installed in the wall of the box (with the wiring and board inside) and they are just directly connected (each one) one end to a resistor then to its corresponding I/O port and one end to a wire attached to the ground port on the digital I/O side. Don't know if this is leading to any of my problems or not...

See whether your sensors are actually giving you the input you expect. Put that sketch to one side and write a new sketch which just reads from a sensor and prints out the result.

It is almost always easier to divide the problem up and debug the components separately than throw together unknown components and try to debug the result.

See whether your sensors are actually giving you the input you expect. Put that sketch to one side and write a new sketch which just reads from a sensor and prints out the result.

It is almost always easier to divide the problem up and debug the components separately than throw together unknown components and try to debug the result.

Great, thanks! I'll try that. ^__^