I ordered the maxbotix but I have also gotten the Ping to work. Anyone know how I can determine the target field and write a sketch to work with the ping/wave shield. I played with modifying the earlier post changing the PIR to a Ping but no luck. Obviously I'm new at this.
Thanks
Wave Shield and PIR sensor
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
The standard methodology for distance sensors is to map out a grid on the floor, fix your sensor at one end, move the target around on the grid points and graph the response. The Maxbotix data sheets show typical sensitivity patterns graphed for various shaped targets. But for critical applications it is always best to test with an actual target object and in the actual operating envionment.
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
I have the maxbotix sonar sensor and was able to get it to work with the piezo straight off the arduino, this code came from the Maxbotix tutorials.
int sonarPin = 0; //pin connected to analog out on maxsonar sensor
int piezoPin = 9; // specifies the pin connected to piezo from Arduino
int inchesAway; // inches away from the maxsonar sensor
void setup() {
pinMode(piezoPin, OUTPUT);
//Serial.begin(9600); // starts serial communication, used for debugging or seeing the values
}
void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
//Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
//Serial.println(" inches from sensor");
if (inchesAway < 24) { // if something is 24 inches away then make a 1khz sound
digitalWrite(piezoPin, HIGH);
delayMicroseconds(500);
digitalWrite(piezoPin, LOW);
delayMicroseconds(500);
}
}
I then took Goatboy's sketch and tried to replace the pir with sonar for the pin and shifted to pin 0 for analog and renamed wave files... no luck.
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int pirPin=6; // Reading from digital pin 6 PIR sensor!
AF_Wave card;
File f;
Wavefile wave;
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(pirPin, INPUT);
// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6,INPUT);
// open memory card...........This part is necessary to set up and read SDcard!
if (!card.init_card()) {
return;
}
if (!card.open_partition()) {
return;
}
if (!card.open_filesys()) {
return;
}
if (!card.open_rootdir()) {
return;
}
}
void loop(){
while (LOW==digitalRead(pirPin)) {;}
playcomplete("0.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("1.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("2.WAV");
delay(1500);
}
void playcomplete(char *name){
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}
As I said I'm new to this but I can get both the wave files to play and the sensor to work independently but can't seem to get them to work together. I also tried to fuse the two but that was really throwing dart in the dark. I'm back to the foundations page and hacking page from arduino.cc. Does anyone have sample code or assistance.
Thanks
int sonarPin = 0; //pin connected to analog out on maxsonar sensor
int piezoPin = 9; // specifies the pin connected to piezo from Arduino
int inchesAway; // inches away from the maxsonar sensor
void setup() {
pinMode(piezoPin, OUTPUT);
//Serial.begin(9600); // starts serial communication, used for debugging or seeing the values
}
void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
//Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
//Serial.println(" inches from sensor");
if (inchesAway < 24) { // if something is 24 inches away then make a 1khz sound
digitalWrite(piezoPin, HIGH);
delayMicroseconds(500);
digitalWrite(piezoPin, LOW);
delayMicroseconds(500);
}
}
I then took Goatboy's sketch and tried to replace the pir with sonar for the pin and shifted to pin 0 for analog and renamed wave files... no luck.
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int pirPin=6; // Reading from digital pin 6 PIR sensor!
AF_Wave card;
File f;
Wavefile wave;
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(pirPin, INPUT);
// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6,INPUT);
// open memory card...........This part is necessary to set up and read SDcard!
if (!card.init_card()) {
return;
}
if (!card.open_partition()) {
return;
}
if (!card.open_filesys()) {
return;
}
if (!card.open_rootdir()) {
return;
}
}
void loop(){
while (LOW==digitalRead(pirPin)) {;}
playcomplete("0.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("1.WAV");
delay(1500);
while (LOW==digitalRead(pirPin)) {;}
playcomplete("2.WAV");
delay(1500);
}
void playcomplete(char *name){
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}
As I said I'm new to this but I can get both the wave files to play and the sensor to work independently but can't seem to get them to work together. I also tried to fuse the two but that was really throwing dart in the dark. I'm back to the foundations page and hacking page from arduino.cc. Does anyone have sample code or assistance.
Thanks
-
- Posts: 10
- Joined: Tue Mar 03, 2009 10:12 am
Re: Wave Shield and PIR sensor
It was a long time ago when I patched all this together and don't have time to revisit it yet but I wonder if this other piece of code that I have will help at all! Although it was for the SRF02 I wonder if the maxbotix can work in the same way? This piece was for operating a servo but should trigger the wav's with a bit of jigging. Excuse me if I've missed the point here.
Code: Select all
/*
SRF02 sensor reader
Language: Wiring/Arduino
Reads data from a Devantech SRF02 ultrasonic sensor.
Should also work for the SRF08 and SRF10 sensors as well.
Sensor connections:
SDA - Analog pin 4
SCL - Analog pin 5
*/
// include Wire library to read and write I2C commands:
#include <Wire.h>
#include <MegaServo.h>
#include <Servo.h>
// the commands needed for the SRF sensors:
#define sensorAddress 0x70
//#define readInches 0x50
// use these as alternatives if you want centimeters or microseconds:
#define readCentimeters 0x51
#define readMicroseconds 0x52
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02
#define LED 8
int pos = 0; // variable to store the servo position
MegaServo firstServo;
Servo myservo1; // create servo object to control a servo
void setup()
{
pinMode (LED, OUTPUT );
firstServo.attach( 2); // this servo on pin 2
// start the I2C bus
Wire.begin();
// open the serial port:
Serial.begin(9600);
}
void loop()
{
// send the command to read the result in inches:
sendCommand(sensorAddress, readCentimeters);
// wait at least 70 milliseconds for a result:
delay(70);
// set the register that you want to read the result from:
setRegister(sensorAddress, resultRegister);
// read the result:
int sensorReading = readData(sensorAddress, 2);
if (( sensorReading >= 15 ) && ( sensorReading <= 50 )) {
for(pos = 80; pos < 120; pos += 1) // goes from 0 degrees to 180 degrees
firstServo.write(pos); // tell servo to go to position in variable 'pos'
//digitalWrite ( LED, HIGH);
delay(80);
// } else {
//digitalWrite ( LED, LOW);
}
if (( sensorReading >= 15 ) && ( sensorReading <= 50 )) {
for(pos = 120; pos >= 80; pos -= 1) // goes from 180 degrees to 0 degrees
firstServo.write(pos); // tell servo to go to position in variable 'pos'
delay(80);
}
// print it:
Serial.print("distance: ");
Serial.print(sensorReading);
Serial.println(" centimeters ");
// wait before next reading:
delay(70);
}
/*
SendCommand() sends commands in the format that the SRF sensors expect
*/
void sendCommand (int address, int command) {
// start I2C transmission:
Wire.beginTransmission(address);
// send command:
Wire.send(0x00);
Wire.send(command);
// end I2C transmission:
Wire.endTransmission();
}
/*
setRegister() tells the SRF sensor to change the address
pointer position
*/
void setRegister(int address, int thisRegister) {
// start I2C transmission:
Wire.beginTransmission(address);
// send address to read from:
Wire.send(thisRegister);
// end I2C transmission:
Wire.endTransmission();
}
/*
readData() returns a result from the SRF sensor
*/
int readData(int address, int numBytes) {
int result = 0; // the result is two bytes long
// send I2C request for data:
Wire.requestFrom(address, numBytes);
// wait for two bytes to return:
while (Wire.available() < 2 ) {
// wait for result
}
// read the two bytes, and combine them into one int:
result = Wire.receive() * 256;
result = result + Wire.receive();
// return the result:
return result;
}
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
I think I'm very close with what I have below but I only get the sound to play not to respond to the sensor.
Here is the sketch:
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int sonarPin = 0; //pin connected to analog out on maxsonar sensor
int inchesAway; // inches away from the maxsonar sensor
AF_Wave card;
File f;
Wavefile wave;
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(sonarPin, INPUT);
analogWrite(sonarPin, HIGH);
// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(0,INPUT);
// open memory card...........This part is necessary to set up and read SDcard!
if (!card.init_card()) {
return;
}
if (!card.open_partition()) {
return;
}
if (!card.open_filesys()) {
return;
}
if (!card.open_rootdir()) {
return;
}
}
void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
//Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
//Serial.println(" inches from sensor");
{
if (inchesAway < 36) // if something is 36 inches away then play .wav file
while (HIGH==analogRead(sonarPin)) {;}
playcomplete("NOAAMI.WAV");
delay(1500);
while (HIGH==analogRead(sonarPin)) {;}
playcomplete("NOAADFW.WAV");
delay(1500);}
}
void playcomplete(char *name){
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}
Any thoughts? Thanks.
Here is the sketch:
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int sonarPin = 0; //pin connected to analog out on maxsonar sensor
int inchesAway; // inches away from the maxsonar sensor
AF_Wave card;
File f;
Wavefile wave;
void setup() {
// set up serial port
Serial.begin(9600);
pinMode(sonarPin, INPUT);
analogWrite(sonarPin, HIGH);
// set up waveshield pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(0,INPUT);
// open memory card...........This part is necessary to set up and read SDcard!
if (!card.init_card()) {
return;
}
if (!card.open_partition()) {
return;
}
if (!card.open_filesys()) {
return;
}
if (!card.open_rootdir()) {
return;
}
}
void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
//Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
//Serial.println(" inches from sensor");
{
if (inchesAway < 36) // if something is 36 inches away then play .wav file
while (HIGH==analogRead(sonarPin)) {;}
playcomplete("NOAAMI.WAV");
delay(1500);
while (HIGH==analogRead(sonarPin)) {;}
playcomplete("NOAADFW.WAV");
delay(1500);}
}
void playcomplete(char *name){
playfile(name);
while (wave.isplaying);
card.close_file(f);
}
void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}
Any thoughts? Thanks.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
What is the purpose of this statement?
It is comparing an analog reading to a digital HIGH.
You code has lots of nice diagnostic statements, but they are all commented out. Uncomment all those Serial.println statements and the serial monitor will tell you exactly what the code is doing.
Also, please use the "code" button when submitting code. It preserves the formatting and makes it much easier to read.
Code: Select all
while (HIGH==analogRead(sonarPin)) {;}
You code has lots of nice diagnostic statements, but they are all commented out. Uncomment all those Serial.println statements and the serial monitor will tell you exactly what the code is doing.
Also, please use the "code" button when submitting code. It preserves the formatting and makes it much easier to read.
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
Got it. So I eliminated the and it is working.
I'm still very vague on how the code works... obviously.
Thanks for all the help!
Code: Select all
while (HIGH==digitalRead(sonarPin)) {;}
I'm still very vague on how the code works... obviously.
How do I do this? Is there a section in the tutorials that will give me some info on this.You code has lots of nice diagnostic statements, but they are all commented out. Uncomment all those Serial.println statements and the serial monitor will tell you exactly what the code is doing.
Thanks for all the help!
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
To uncomment, just remove the "//" before the statement.
To use the serial monitor: after downloading your code to the arduino, just click on the rightmost button in the toolbar. This will open a serial monitor window to display all the text sent by Serial.println().
Lesson 4 covers this in more detail: http://www.ladyada.net/learn/arduino/lesson4.html
If you have not done so already, a few hours going through the Adafruit tutorials is the best way to get up to speed.
To use the serial monitor: after downloading your code to the arduino, just click on the rightmost button in the toolbar. This will open a serial monitor window to display all the text sent by Serial.println().
Lesson 4 covers this in more detail: http://www.ladyada.net/learn/arduino/lesson4.html
If you have not done so already, a few hours going through the Adafruit tutorials is the best way to get up to speed.
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
I went through the refresher class and started from scratch, sort of. I began with the PlayAllLoop and then modified a sketch sent to me but I get no sound. The serial monitor is telling me it is reading the distance from the sensor and when my hand comes into the target area it says its playing the .wav file but no sound and it does not run the duration of the file.
Here is the sketch:
Thanks for your help and patience as I learn in fits and starts!
Here is the sketch:
Code: Select all
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
int sensorValue; //Maxsonar connected to analog 0,
char* mySounds [3]={"stream.wav", "oil.wav", "train.wav"};
AF_Wave card;
File f;
Wavefile wave; // only one!
uint16_t samplerate;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Wave test!");
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(0, INPUT);
if (!card.init_card()) {
putstring_nl("Card init. failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
putstring_nl("Files found:");
ls();
}
char* getRandomSoundName(){
long i;
char* myString;
i=random(0, 3);
myString=mySounds[i];
return myString;
}
void ls() {
char name[13];
int ret;
card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}
uint8_t tracknum = 0;
void loop() {
uint8_t i, r;
char c;
char* mySoundName;
card.reset_dir();
// scroll through the files in the directory
{
sensorValue=analogRead(0);
Serial.println(sensorValue, DEC);
delay(10);
}
if(sensorValue<36){
//reset directory so we can find the file
card.reset_dir();
mySoundName=getRandomSoundName();
putstring("\n\rPlaying "); Serial.print(mySoundName);
}
else
{Serial.println("resting");}
delay (5000);
}
void playcomplete(char *name) {
playfile(name);
samplerate = wave.dwSamplesPerSec;
while (wave.isplaying) {
// you can do stuff here!
delay(5000);
}
card.close_file(f);
}
void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl(" Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl(" Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
Code: Select all
if(sensorValue<36){
//reset directory so we can find the file
card.reset_dir();
mySoundName=getRandomSoundName();
putstring("\n\rPlaying "); Serial.print(mySoundName);
}
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
Now it is telling me :
Playing train.wav Couldn't open file
11
Thoughts? They all played in the PlayAllLoop so I'm sure they are good files.
Here is the code correction:
Playing train.wav Couldn't open file
11
Thoughts? They all played in the PlayAllLoop so I'm sure they are good files.
Here is the code correction:
Code: Select all
if(sensorValue<36){
//reset directory so we can find the file
card.reset_dir();
mySoundName=getRandomSoundName();
putstring("\n\rPlaying "); Serial.print(mySoundName);
playcomplete(mySoundName);
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
Not sure what is going on there. You have another sketch that can play the same file?
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
Yea, the PlayAllLoop for the AF_Wave examples. Here is that code:
Code: Select all
#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"
AF_Wave card;
File f;
Wavefile wave; // only one!
#define redled 9
uint16_t samplerate;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Wave test!");
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);
if (!card.init_card()) {
putstring_nl("Card init. failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
putstring_nl("Files found:");
ls();
}
void ls() {
char name[13];
int ret;
card.reset_dir();
putstring_nl("Files found:");
while (1) {
ret = card.get_next_name_in_dir(name);
if (!ret) {
card.reset_dir();
return;
}
Serial.println(name);
}
}
uint8_t tracknum = 0;
void loop() {
uint8_t i, r;
char c, name[15];
card.reset_dir();
// scroll through the files in the directory
for (i=0; i<tracknum+1; i++) {
r = card.get_next_name_in_dir(name);
if (!r) {
// ran out of tracks! start over
tracknum = 0;
return;
}
}
putstring("\n\rPlaying "); Serial.print(name);
// reset the directory so we can find the file
card.reset_dir();
playcomplete(name);
tracknum++;
}
void playcomplete(char *name) {
uint16_t potval;
uint32_t newsamplerate;
playfile(name);
samplerate = wave.dwSamplesPerSec;
while (wave.isplaying) {
// you can do stuff here!
delay(500);
}
card.close_file(f);
}
void playfile(char *name) {
f = card.open_file(name);
if (!f) {
putstring_nl(" Couldn't open file"); return;
}
if (!wave.create(f)) {
putstring_nl(" Not a valid WAV"); return;
}
// ok time to play!
wave.play();
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Wave Shield and PIR sensor
The difference here is that the Play All code is reading the file names from the directory instead of hard-coding them. That assures that the files requested are actually files recognized by the file system.
Are your file names spelled correctly? Are they case-sensitive?
Are your file names spelled correctly? Are they case-sensitive?
-
- Posts: 37
- Joined: Sat Sep 12, 2009 12:37 pm
Re: Wave Shield and PIR sensor
I double checked the files and they are spelled correctly and in the correct case. I have reloaded the sketch but still no sound. I don't know if it will help but here is a copy of the serial monitor:
39
resting
11
Playing stream.wav Couldn't open file
125
Wave test!
Files found:
Files found:
_STREA~2.WAV
TRAIN.WAV
TRASHE~1
_~1.TRA
DS_STO~1
_TRAIN~2.WAV
OIL.WAV
FSEVEN~1
SPOTLI~1
_OIL~1.WAV
STREAM.WAV
125
resting
11
Playing oil.wav Couldn't open file
11
Playing oil.wav Couldn't open file
11
Playing train.wav Couldn't open file
11
Playing train.wav Couldn't open file
11
Playing oil.wav Couldn't open file
125
resting
39
resting
11
Playing stream.wav Couldn't open file
125
Wave test!
Files found:
Files found:
_STREA~2.WAV
TRAIN.WAV
TRASHE~1
_~1.TRA
DS_STO~1
_TRAIN~2.WAV
OIL.WAV
FSEVEN~1
SPOTLI~1
_OIL~1.WAV
STREAM.WAV
125
resting
11
Playing oil.wav Couldn't open file
11
Playing oil.wav Couldn't open file
11
Playing train.wav Couldn't open file
11
Playing train.wav Couldn't open file
11
Playing oil.wav Couldn't open file
125
resting
Please be positive and constructive with your questions and comments.