I working on a project where I'm trying to trigger an audio file with an ultrasonic sensor so that if someone walks by, an audio file plays. I have a code that I'm working with, but I am very new with Arduino and I barely understand what I'm doing. Can someone have a look and see why it might not be working?
#include "SD.h" //Lib to read SD card
#include "TMRpcm.h" //Lib to play auido
#include "SPI.h" //SPI lib for SD card
#define SD_ChipSelectPin 4 //Chip select is pin number 4
TMRpcm music; //Lib object is named "music"
int trigger_pin = 2;
int echo_pin = 3;
int speaker = 10;
int time;
int distance;
int song_number=0;
boolean debounce1=true;
boolean debounce2=true;
boolean play_pause;
void setup(){
music.speakerPin = 9; //Auido out on pin 9
Serial.begin(9600); //Serial Com for debugging
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
pinMode(2, OUTPUT); //
pinMode(3, INPUT); //
music.setVolume(5); // 0 to 7. Set volume level
music.quality(1); // Set 1 for 2x oversampling Set 0 for normal
//music.volume(1); // 1(up) or 0(down) to control volume
//music.play("filename");
}
void loop()
{
if (digitalRead(2)==HIGH && debounce1 == true) //Button 1 Pressed
{
song_number++;
if (song_number==5)
{song_number=1;}
debounce1=false;
Serial.println("KEY PRESSED");
Serial.print("song_number=");
Serial.println(song_number);
if (song_number ==1)
{music.play("1.wav",10);} //Play song 1 from 10th second
if (digitalRead(3)==HIGH && debounce2 == true) //
{
music.pause(); Serial.println("PLAY / PAUSE");
debounce2=false;
}
if (digitalRead(2)==HIGH) //Avoid debounce
debounce1=true;
if (digitalRead(3)==HIGH)//Avoid debounce
debounce2=true;
}
}
I'm trying to trigger a .wav file using an ultrasonic sensor. Im using an SD card, an amplifier and a speaker. Would someone mind telling me why this isn't working? I'm very new at Arduino.
#include "SD.h" //Lib to read SD card
#include "TMRpcm.h" //Lib to play auido
#include "SPI.h" //SPI lib for SD card
#define SD_ChipSelectPin 4 //Chip select is pin number 4
TMRpcm music; //Lib object is named "music"
int trigger_pin = 2;
int echo_pin = 3;
int speaker = 10;
int time;
int distance;
int song_number=0;
boolean debounce1=true;
boolean debounce2=true;
boolean play_pause;
void setup(){
music.speakerPin = 9; //Auido out on pin 9
Serial.begin(9600); //Serial Com for debugging
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
pinMode(2, OUTPUT); //
pinMode(3, INPUT); //
music.setVolume(5); // 0 to 7. Set volume level
music.quality(1); // Set 1 for 2x oversampling Set 0 for normal
//music.volume(1); // 1(up) or 0(down) to control volume
//music.play("filename");
}
void loop()
{
if (digitalRead(2)==HIGH && debounce1 == true) //Button 1 Pressed
{
song_number++;
if (song_number==5)
{song_number=1;}
debounce1=false;
Serial.println("KEY PRESSED");
Serial.print("song_number=");
Serial.println(song_number);
if (song_number ==1)
{music.play("1.wav",10);} //Play song 1 from 10th second
if (digitalRead(3)==HIGH && debounce2 == true) //
{
music.pause(); Serial.println("PLAY / PAUSE");
debounce2=false;
}
if (digitalRead(2)==HIGH) //Avoid debounce
debounce1=true;
if (digitalRead(3)==HIGH)//Avoid debounce
debounce2=true;
}
}
Delta_G:
Why are you calling digitalRead on pin 2 when it is set to an OUTPUT?
What do you mean when you say it doesn't work? What does it do or not do that is wrong.
nothing comes out when its uploaded to the board. And I'm calling digitalRead on pin 2 when it's set to an OUTPUT because I have no solid idea of how this works. Should it be a digitalWrite? Or should it be set to an INPUT
(sorry for cross-posting, this is my first time using this and I didn't realize that was a rule)
Delta_G:
What do you want that pin to do? Do you know the difference between what the words input and output mean? (I'm not picking here, those words might not mean anything to someone who doesn't speak English natively.) If you think about the meaning of those two words and what you want that pin to do, then it should be pretty obvious what you should do with it.
I think I have more of a question of the difference between digitalRead and digitalWrite. I've been copying and pasting codes I've found so far to play around with this and never thought about the meaning.
But just to clarify, input is the information going into Arduino and Output is what is coming out of the system, right?
So a digitalRead is reading the value from a pin, right? So should it not be reading the Output?
mdelasanta13:
So a digitalRead is reading the value from a pin, right? So should it not be reading the Output?
You can read an output but it doesn't make sense: it returns the value that you have written to it before, which you know already. You would normally read an input, write to an output.
I'm sure there must be some input/output turorial out there... (one quick Google search on that term later)... Of course, there is.
Really the best thing I can recommend you now is to read some tutorials. This very site has lots of them, just search for every single term you encounter and don't fully understand. Sure that'll take you a day to read through your first sketch but after that you should completely understand what it's doing. Every next sketch you can build on that knowledge and learn more.
Back to your project: I see you have defined a trigpin and an echopin (are you using the HC-SR04 or another sensor? There are many different sensors out there!) but I don't see any code that actually reads that sensor. I do see some code for buttons, which you don't mention in your project description.
Delta_G:
Just copying and pasting with no thinking isn't going to get you anywhere. Coding is something for a thinking person. If you don't want to engage and challenge your brain then you should find another hobby.
Really, that goes for all of life. If you're not thinking then you are doomed. People will take advantage of you. You won't be able to do the things you want to do. You have to think if you want to be in control of your life. If you don't think, then life is in control of you.
Right.
So think about information going in or out of your brain. Which one is reading and which one is writing? Which one is input and which one is output?
Thank you for trying to help me out! I actually ended up figuring it out on my own!
To clarify, I am a fifth year student at Cornell University enrolled in an elective that uses Arduino. My professor proved to be absolutely worthless throughout the class and I've been struggling the whole semester. I'm really glad this forum exists, though.
Here's the code that worked:
//TMRpcm library is needed
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
int trigger_pin = 2;
int echo_pin = 3;
int time;
int distance;
TMRpcm tmrpcm;
void setup()
{
tmrpcm.speakerPin=9;
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(6);
tmrpcm.play("1.wav");
tmrpcm.pause();
pinMode (trigger_pin, OUTPUT);
pinMode (echo_pin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (trigger_pin, HIGH);
delayMicroseconds (10);
digitalWrite (trigger_pin, LOW);
time = pulseIn (echo_pin, HIGH);
distance = (time * 0.034) / 2;
if (distance <= 10)
{
Serial.println (" Person Detected ");
Serial.print (" Distance= ");
Serial.println (distance);
digitalWrite;
tmrpcm.play("1.wav");
delay (500);
}
else {
Serial.println (" No One There ");
Serial.print (" Distance= ");
Serial.println (distance);
}
}
Delta_G:
If you're at Cornell then you shouldn't need the professor. The whole point of college is learning to learn without someone being there to spoon feed you.
The professor is also there to tell you what's out there in the first place. Good luck starting searching for information when you have no idea what is out there. Without someone telling you they exist, it's hard to know about Arduinos in the first place. That's the professor's task, to help getting started and guide students along the way. Which is pretty much what this forum tries to do really, albeit on a more limited scale.