Here's the code:
First it gave me an error code of 10 and said "No valid FAT partition", then I unplug the trigger and echo jumpers and it plays the sound in a loop, printing out "Distance: 0" until I plug them back in and then it says it can't open the file. Now it's giving me an error code of 3...
So confused why this is happening!
Wave Shield confusion
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
Current version of code (sorry, forgot to attach in the last post):
Code: Select all
#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
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
* by Dejan Nedelkovski,*
*/
// defines pins numbers
const int trigPin = 11;
const int echoPin = 12;
long duration;
int distance;
// 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);
}
void setup() {
// set up serial port
Serial.begin(9600);
putstring_nl("WaveHC with 2 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);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an 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 loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(50);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if(distance < 40){
playcomplete("FART1.WAV");
}
}
/* //putstring("."); // uncomment this to see if the loop isnt running
switch (check_sensor()) {
case 1:
playcomplete("FART1.WAV");
break;
case 2:
playcomplete("FART2.WAV");
break;
case 3:
playcomplete("FART3.WAV");
break;
}
}
byte check_switches() {
static byte previous[6];
static long time[6];
byte reading;
byte pressed;
byte index;
pressed = 0;
for (byte index = 0; index < 2; ++index) {
reading = digitalRead(14 + index);
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
// switch pressed
time[index] = millis();
pressed = index + 1;
break;
}
previous[index] = reading;
}
// return switch number (1 - 6)
return (pressed);
} */
// 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();
}
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
I should add that I have the sensor pointing towards a wall roughly 80cm away with nothing obstructing.
In the photo, you'll notice the lines change from
" Distance = 0; " to
" Couldn't open file 'name' "
While it prints " Distance = 0 ", the sound loops over and over - at this point I have the echo/trig jumpers unhooked (as this seems to be the only way to get sound currently)
Once I connect the echo/trig jumpers back in place, it starts to print the 2nd line [Couldn't open file "name"] over and over and playback stops.
My other thought was that maybe it's an issue of enough voltage for both sensor and wave shield? I keep seeing different errors pop up, not to mention the time earlier when the playback would stop just by connecting the sensor ground pin.
Mystery still abounds!
In the photo, you'll notice the lines change from
" Distance = 0; " to
" Couldn't open file 'name' "
While it prints " Distance = 0 ", the sound loops over and over - at this point I have the echo/trig jumpers unhooked (as this seems to be the only way to get sound currently)
Once I connect the echo/trig jumpers back in place, it starts to print the 2nd line [Couldn't open file "name"] over and over and playback stops.
My other thought was that maybe it's an issue of enough voltage for both sensor and wave shield? I keep seeing different errors pop up, not to mention the time earlier when the playback would stop just by connecting the sensor ground pin.
Mystery still abounds!
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
My apologies for coming off frustrated, it's been a long road to success for this one.
Really appreciate all the help!
Really appreciate all the help!
- adafruit_support_carter
- Posts: 29483
- Joined: Tue Nov 29, 2016 2:45 pm
Re: Wave Shield confusion
It helps if you have handy several very simple sketches that only do one thing. For example, a sketch that does nothing but read the value from the sensor. Another sketch that does nothing but list the files on the SD card. Another sketch that does nothing but play a single file. etc.
These come in handy when your more complex code gives you errors. You can fall back to running these simple examples to make sure it's not something fundamental that is broken. If it is something fundamental, use those same basic sketches to troubleshoot. Don't return to the more complex code until all the very simple ones work again.
These come in handy when your more complex code gives you errors. You can fall back to running these simple examples to make sure it's not something fundamental that is broken. If it is something fundamental, use those same basic sketches to troubleshoot. Don't return to the more complex code until all the very simple ones work again.
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
Hi,
I'm now trying to work from this example:
viewtopic.php?f=25&t=13108&p=715929#p715929
but I'm having trouble understanding why they're using " analogRead(5) " when that pin is reserved for the DAC (at least I thought it was).
I'm not seeing which pins they're using for echo/trigger either. It's a different brand of sensor, although it's the same type, so perhaps it doesn't use Arduino for the trigger and just reads the echo pin but no pins are declared as INPUT in the sketch so it's confusing.
Here are my simplified code versions ---
Functional ultrasonic sensor code that blinks an LED (reads and prints distance values as well)
Functional audio trigger using reed switches (still using the switch...case function as that's the only one I can get working):
I'm now trying to work from this example:
viewtopic.php?f=25&t=13108&p=715929#p715929
but I'm having trouble understanding why they're using " analogRead(5) " when that pin is reserved for the DAC (at least I thought it was).
I'm not seeing which pins they're using for echo/trigger either. It's a different brand of sensor, although it's the same type, so perhaps it doesn't use Arduino for the trigger and just reads the echo pin but no pins are declared as INPUT in the sketch so it's confusing.
Here are my simplified code versions ---
Functional ultrasonic sensor code that blinks an LED (reads and prints distance values as well)
Code: Select all
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
// defines pins numbers
const int trigPin = 11;
const int echoPin = 12;
// defines variables
long duration;
int distance;
int LED=13;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED, OUTPUT); //
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if(distance < 70){
digitalWrite(LED, HIGH);
}
else{digitalWrite(LED, LOW);}
}
Code: Select all
#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);
}
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!
// 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);
// enable pull-up resistors on switch pins (analog inputs)
digitalWrite(14, HIGH);
digitalWrite(15, 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!");
}
void loop() {
//putstring("."); // uncomment this to see if the loop isnt running
switch (check_switches()) {
case 1:
playcomplete("FART1.WAV");
break;
case 2:
playcomplete("FART2.WAV");
break;
}
}
byte check_switches()
{
static byte previous[6];
static long time[6];
byte reading;
byte pressed;
byte index;
pressed = 0;
for (byte index = 0; index < 2; ++index) {
reading = digitalRead(14 + index);
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
// switch pressed
time[index] = millis();
pressed = index + 1;
break;
}
previous[index] = reading;
}
// return switch number (1 - 6)
return (pressed);
}
// 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();
}
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
I tried redoing the code starting from functional Ultrasonic code and having it just turn the LED on and off when the measured distance is below a certain amount (ie: when I put my hand in front of it) and the Serial monitor runs way slower than it did in the original Ultrasonic code and reads a distance of zero for each line and the LED will not light at all.
Really confused as to what the problem is here... working from a totally functional code and when adding the totally functional audio playback code, it fails.
Here it is:
Really confused as to what the problem is here... working from a totally functional code and when adding the totally functional audio playback code, it fails.
Here it is:
Code: Select all
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
// defines pins numbers
const int trigPin = 11;
const int echoPin = 12;
// defines variables
long duration;
int distance;
int LED=13;
#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
// 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);
}
void setup() {
Serial.begin(9600); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED, OUTPUT); //
putstring_nl("WaveHC with sensor");
putstring("Free RAM: "); // helps with debugging, running out of RAM is bad
Serial.println(freeRam()); // under 150 bytes it may spell trouble!
// Set output pins for DAC control. Pins are defined in library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an 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!
}
//past the tough parts.
putstring_nl("Ready!");
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if(distance < 70){
digitalWrite(LED, HIGH);
}
else{digitalWrite(LED, LOW);}
}
// 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();
}
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
Also, here's a screenshot of the Serial Monitor (running at probably one hundredth the speed or less of the original ultrasonic code)
I haven't added any extra delays or waits either :-/
I haven't added any extra delays or waits either :-/
- Attachments
-
- Distance perpetually reads zero when it should read over 70 as it did with the other code from just a moment ago
- Screenshot (7).png (170.23 KiB) Viewed 119 times
- subhashvnr
- Posts: 1
- Joined: Fri Dec 28, 2018 5:12 am
Re: Wave Shield confusion
I was also getting the "0 cm" and "out of range" reading when I combine audio playing with sr04. But as said in this post https://forum.arduino.cc/index.php?topic=314802.0, the use of pins 11 and 12 for trigger and echo pins was what was causing the problem. Using pins 5-8 instead solved my problem.
- 0hm_Mic_0dd
- Posts: 43
- Joined: Thu Dec 06, 2018 2:51 pm
Re: Wave Shield confusion
you'll need to use a Maxbotix sensor instead of the HC-SR04. Refer to the 4th post on this thread to find out why:
viewtopic.php?f=25&t=130239
viewtopic.php?f=25&t=130239
Please be positive and constructive with your questions and comments.