Hey guys, I am completely new to all of this and I am having a particular hard time with the coding..
I want to use 2 PIR sensors with my WaveShield,
1 will will make my WaveShield play a file on a loop
the other will simply turn it off, stop it from playing.
If the easiest way is to adapt the existing code for using sensors to trigger sounds: http://www.ladyada.net/make/pumpkin/pumpkin.html
What do I need to add to this code?
Thanks
2 PIR sensors to turn WaveShield ON and OFF
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
The easiest thing is probably to start with one of the 6-button examples from the tutorial page. The last two in the list seem most like the functionality you describe:
You'll only have two 'buttons', so you can eliminate the code related to buttons 3, 4, 5 and 6 (or just leave it there - it won't get in the way).
Your PIRs will look just like the buttons, with one exception: the buttons read LOW when pressed. The PIRs will read HIGH when activated (i.e., equivalent to 'pressed'). The button state is checked in the function check_switches(), so that's where you'll have to reverse the sense of what HIGH and LOW indicate.Play the wave file all the way through and loop it, but allow other buttons to interrupt
Play the wave file all the way through once, but allow other buttons to interrupt
You'll only have two 'buttons', so you can eliminate the code related to buttons 3, 4, 5 and 6 (or just leave it there - it won't get in the way).
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
Thanks driverblock!
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
I'm a little confused to how I will physically connect 2 sensors to my Waveshield if both sensors require the 5V pin.
How would I do this?
How would I do this?
- adafruit_support_mike
- Posts: 67454
- Joined: Thu Feb 11, 2010 2:51 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
The 5v pin is just the supply voltage. You can connect any number of things to that. The sensor outputs are the ones that need to be connected to separate Arduino pins.
If you mean, "how do I get power connections to more than one sensor when there's only one 5v pin?", that's where breadboads and things like the Perma-Proto boards (http://www.adafruit.com/products/571) come in handy.
If you mean, "how do I get power connections to more than one sensor when there's only one 5v pin?", that's where breadboads and things like the Perma-Proto boards (http://www.adafruit.com/products/571) come in handy.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
Splice them any way that's convenient. As I recall, the wires on the PIR are kind of lightweight. You could use some length of regular 22 AWG hookup wire as a pigtail - solder the PIR leads to one end, and connect the other end to 5V.
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
I've decided to use two LDRs instead, would this work in the same way?
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
Which LDRs?
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
I'm not sure which ones as I just took what was available from school, attached is a photo of one of them:
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
These are not at all like PIRs or pushbuttons. You will need to connect them to an analog pin and monitor the voltage.
This tutorial shows how to connect and use these: http://learn.adafruit.com/photocells
This tutorial shows how to connect and use these: http://learn.adafruit.com/photocells
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
I've looked through the tutorial. Could I combine the analog light measurement code with the WaveHC library?
To look something like this:
int ldrPin = 0; // the cell and 10K pulldown are connected to a0
int ldrReading; // the analog reading from the analog resistor divider
int ldr2Pin = 1; // the cell and 10K pulldown are connected to a0
int ldr2Reading; // the analog reading from the analog resistor divider
void loop() {
if (ldrReading < 100) {
Serial.println(" - Dark");
playfile ("TRAINAPP.WAV");
}
else if (ldr2Reading < 100) {
Serial.println(" - Dark");
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
}
I need to make a simulation for my exhibition stand so when the user pushes the train over the first sensor the audio file starts playing and it stops after passing the 2nd sensor as shown:
I have the 2 LDRs with me but I have no idea how to generate the code for this simply on/off play of a looped file. It looks like it will be much easier if is use 2 PIR module but I don't really want to purchase them unless I have to.
To look something like this:
int ldrPin = 0; // the cell and 10K pulldown are connected to a0
int ldrReading; // the analog reading from the analog resistor divider
int ldr2Pin = 1; // the cell and 10K pulldown are connected to a0
int ldr2Reading; // the analog reading from the analog resistor divider
void loop() {
if (ldrReading < 100) {
Serial.println(" - Dark");
playfile ("TRAINAPP.WAV");
}
else if (ldr2Reading < 100) {
Serial.println(" - Dark");
if (wave.isplaying) {// already playing something, so stop it!
wave.stop(); // stop it
}
}
I need to make a simulation for my exhibition stand so when the user pushes the train over the first sensor the audio file starts playing and it stops after passing the 2nd sensor as shown:
I have the 2 LDRs with me but I have no idea how to generate the code for this simply on/off play of a looped file. It looks like it will be much easier if is use 2 PIR module but I don't really want to purchase them unless I have to.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
The pseudocode you posted looks like pretty much all you need to do. A few of the WaveShield example sketches use buttons to do exactly the sort of start/stop thing you're trying to do. Substituting a light reading for a button-press will be trivial.
Use the analogRead function for reading your photocells.
The Arduino Playground has lots of ideas and information about interfacing with hardware
Use the analogRead function for reading your photocells.
The Arduino Playground has lots of ideas and information about interfacing with hardware
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
Okay I've decided to order some PIR modules so I can substitute them for buttons in the example code.
I've adapted the code for 'Play the wave file all the way through and loop it, but allow other buttons to interrupt'
and verified it, if you guys could double check it for me I'd appreciate it.
I've deleted the 4 unused buttons, turned the HIGHs to LOWs and visa versa, and instead of having the 2nd button play another file, I've told it to simply stop whatever is playing. I've kept everything else incase I delete any important stuff.
I have also deleted the press and hold/ press release and all that so the buttons or the PIRs are simply 'pressed' and carry out the required functions.
I've adapted the code for 'Play the wave file all the way through and loop it, but allow other buttons to interrupt'
and verified it, if you guys could double check it for me I'd appreciate it.
I've deleted the 4 unused buttons, turned the HIGHs to LOWs and visa versa, and instead of having the 2nd button play another file, I've told it to simply stop whatever is playing. I've kept everything else incase I delete any important stuff.
I have also deleted the press and hold/ press release and all that so the buttons or the PIRs are simply 'pressed' and carry out the required functions.
Code: Select all
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card;
FatVolume vol;
FatReader root;
FatReader f;
WaveHC wave;
#define DEBOUNCE 2
byte buttons[] = {14, 15};
#define NUMBUTTONS sizeof(buttons)
volatile byte pressed[NUMBUTTONS];
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() {
byte i;
// set up serial port
Serial.begin(9600);
putstring_nl("WaveHC with ");
Serial.print(NUMBUTTONS, DEC);
putstring_nl("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!
// 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);
// pin13 LED
pinMode(13, OUTPUT);
// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
}
// 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!");
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
}
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
byte index;
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(buttons[index]); // read the button
/*
Serial.print(index, DEC);
Serial.print(": cstate=");
Serial.print(currentstate[index], DEC);
Serial.print(", pstate=");
Serial.print(previousstate[index], DEC);
Serial.print(", press=");
*/
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just pressed
}
else if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
//Serial.println(pressed[index], DEC);
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
void loop() {
byte i;
static byte playing = -1;
if (pressed[0]) {
if (playing != 0) {
playing = 0;
playfile("TRAINAPP.WAV");
}
}
else if (pressed[1]) {
if (wave.isplaying) {
wave.stop();
}
}
if (! wave.isplaying) {
playing = -1;
}
}
// 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();
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: 2 PIR sensors to turn WaveShield ON and OFF
But the PIR sensors will not detect your train. PIR sensors detect changes in IR radiation such as from movement of a human body. They also have a very wide field of view. Your PIR sensors will likely be triggering whenever anyone walks by.
-
- Posts: 13
- Joined: Sat Mar 09, 2013 12:31 pm
Re: 2 PIR sensors to turn WaveShield ON and OFF
The PIRs will be housed in the box with a small circular opening so that it can only detect things directly above it. The user will have to push the train with their hand so this should trigger it.
I just need to know if that code is correct, if it doesn't work then I will have to use LDRs but as I said I struggle with the code
I just need to know if that code is correct, if it doesn't work then I will have to use LDRs but as I said I struggle with the code
Please be positive and constructive with your questions and comments.