Motion sensor, Adafruit Wave Shield and pinMode-problem

Hello! Bought my first Arduino UNO just a few days ago and I love it! It’s great fun but I have a problem that could be programming related but it could also be hardware related. I hope you can help me because it’s making me crazy. Well, here we go:

What I’m trying to do:

I want to use a motion sensor ( pir sensor) together with the Adafruit wave sheild (Overview | Wave Shield | Adafruit Learning System) and a ws2801-LED strip.
When you move in front of (activate) the sensor I want the program to randomly choose one of three sequences/scenarios .

In each sequence the Arduino will make one LED blink and play a .wav file. The LED blinks with different lengths and the .wav-file changes with each of the three.

My problem: depending on where I put the line “pinMode(sensor,INPUT);” (sensor = 11) the program either plays the sound and blinks as it should BUT detects motion when there is no motion. It seems to activate the sensor although I am not. And the thing never stops and goes on and on.

OR

the sensor works as it should, only detecting motion when there is motion and it blinks BUT there is no sound and it says: “Couldn’t open file”.

I’m using FastLed (http://fastled.io/) library for the LED, the Adafruit library (https://code.google.com/p/wavehc/) for the wave shield and I used a lot of the code in Adafruits “play6_hc.pde”-example (The Play6_HC Example | Wave Shield | Adafruit Learning System)

Here is my code:

#include "FastLED.h"
#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
#define NUM_LEDS 20
#define DATA_PIN 7 
#define CLOCK_PIN 6 

// Define the array of leds
CRGB leds[NUM_LEDS];

int sensor = 11;
int randomEvent = 1;

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


void setup() { 
   // set up serial port
  Serial.begin(9600);

  putstring_nl("WaveHC");
 
  FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  
[b]pinMode(sensor,INPUT); // HERE IT REPEATS!![/b]
   
  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!
  
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(12,OUTPUT); 

 
   // 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!");

[b] // pinMode(sensor,INPUT); //HERE IT DOESN'T PLAY SOUND!![/b]

}

void loop() { 
    
 delay(3000);
 
   if (digitalRead(sensor) == HIGH){
   Serial.println("\n\nMotion detected");

  int randomEvent = random (1,3);

//Event 1 
  if (randomEvent == 1){
    Serial.println("Event 1");
    playcomplete("1.wav");
   
    leds[0] = CRGB::White;
   FastLED.show();
  delay(600);

  leds[0] =  CRGB::Black;
  FastLED.show();
  delay(600);
  
     while (wave.isplaying) {
  }
  
    }
  
  //Event 2
  else if (randomEvent == 2) {
     Serial.println("Event 2");
      playcomplete("2.wav");

     leds[0] = CRGB::White;
   FastLED.show();
   delay(400);
   
    leds[0] = CRGB::Black;
  FastLED.show();
   delay(400);
  
  while (wave.isplaying) {
  }
  }
  
   //Event 3
  else if (randomEvent == 3) {
     Serial.println("Event 3");
     playcomplete("3.wav");
       leds[0] = CRGB::White;
   FastLED.show();
   delay(300);

  leds[0]  = CRGB::Black;
  FastLED.show();
     delay(300);
  
    while (wave.isplaying) {
 
  }

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

}

I highlighted the two pinMode(sensor,INPUT). Depending on which one I use the program has different problems.

So, I am very confused and I’m really stuck. I’ve tried to use pin 13 instead. As you can see the only digital pins available are 0, 1, 8, 9, 11 and 13.

I hope you can help me out and that I explained everything clearly.

(I’m very much a beginner at c++ and programming so if you have a solution please explain like I’m a 5-year old!)

Best regards,
Niklas

Try this instead
pinMode(sensor,INPUT_PULLUP);
so the pin is at a solid level and not floating.
Check the PIR output - is it a low level when active?
Then check for a low input as well.

It is hard to find which pins are in use, but I assume that the SDcard used SPI interface.
The SPI uses 11,12,13 and also 10. I don't know which pin is used for ChipSelect, perhaps pin 10.

Peter_n:
It is hard to find which pins are in use, but I assume that the SDcard used SPI interface.
The SPI uses 11,12,13 and also 10. I don't know which pin is used for ChipSelect, perhaps pin 10.

http://arduino.cc/en/Hacking/PinMapping168

I had to solder on a female-thing on pin 8 och 9 and it worked! Pin 8 was the solution.

Thank you!