So I am trying to play music when my plant needs water, I have gotten really close, but I cannot get my song to loop. It wont play in the loop function...only through the setup. Once I water the sensor the song stops, but doesn't start up again...If I put it in the loop, I only hear static. Any suggestions to my code?
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
int dry; //declare an integer type variable to store sensor value
int threshold = 700; // threshold will store the value after which Buzzer will turn on
TMRpcm tmrpcm;
void setup()
{
tmrpcm.speakerPin = 9;
Serial.begin(9600); // start serial comm. at 9600 baud rate
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
pinMode(tmrpcm.speakerPin, OUTPUT);//config buzzer pin as OUTPUT
}
void loop()
{
dry = analogRead(A0); //store sensor value in dry variable
if (dry > threshold ) {
Serial.println("Plant Needs Water");
Serial.println("play wav.");
tmrpcm.play("1.wav");
}
else {
tmrpcm.stopPlayback();
}
}
Once A0 is above your threshhold, the song starts playing but the very next time through loop() you will just try to start the song again. the play() function does not wait for the song to end before returning. You need to call the isPlaying() function to see if you are playing the song or not and act accordingly. You may need another variable if you only want the song to play one time or do you want it to repeat until it gets water?
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
const int SD_ChipSelectPin = 4;
const int threshold = 700; // threshold will store the value after which Buzzer will turn on
bool isPlaying = false;
TMRpcm tmrpcm;
const int speakerPin = 9;
void setup()
{
Serial.begin(9600); // start serial comm. at 9600 baud rate
tmrpcm.speakerPin = speakerPin;
pinMode(tmrpcm.speakerPin, OUTPUT);//config buzzer pin as OUTPUT
tmrpcm.setVolume(5);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
while (1);
}
}
void loop()
{
int dry = analogRead(A0); //store sensor value in dry variable
if (dry > threshold ) {
if ( isPlaying == false ) {
// first dry detection, start playing
Serial.println("Plant Needs Water");
Serial.println("play wav.");
tmrpcm.play("1.wav");
isPlaying = true;
}
else {
// have already started playing so check if we are finished and loop
if (tmrpcm.isPlaying() == false ) {
// song has completed so restart
tmrpcm.play("1.wav");
}
}
}
else {
// plant has been watered
if ( isPlaying ) {
tmrpcm.stopPlayback();
isPlaying = false;
}
}
}
Okay this is my first time posting and helping but i have a suggestion it might not work . I did use a bit form blh64 suggestion.
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
int dry; //declare an integer type variable to store sensor value
int threshold = 700; // threshold will store the value after which Buzzer will turn on
bool musicOn = false; // adds a varible to check it is playing
TMRpcm tmrpcm;
void setup()
{
tmrpcm.speakerPin = 9;
Serial.begin(9600); // start serial comm. at 9600 baud rate
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
pinMode(tmrpcm.speakerPin, OUTPUT);//config buzzer pin as OUTPUT
}
void loop()
{
dry = analogRead(A0); //store sensor value in dry variable
if (dry > threshold && musicOn == false ) {// duel condtion dont know if arduino works with this
Serial.println("Plant Needs Water");
Serial.println("play wav.");
tmrpcm.play("1.wav");$
musicOn = true; // change variable to true
}
else if(dry <= threshold) { // change this to an else if stament with the condtion if dry is above
tmrpcm.stopPlayback();
musicOn = false; // my edit
}
}
int dry; //declare an integer type variable to store sensor value
int threshold = 700; // threshold will store the value after which Buzzer will turn on
bool musicOn = false; // adds a varible to check it is playing
TMRpcm tmrpcm;
void setup()
{
tmrpcm.speakerPin = 9;
Serial.begin(9600); // start serial comm. at 9600 baud rate
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println(“SD fail”);
return;
}
tmrpcm.setVolume(5);
pinMode(tmrpcm.speakerPin, OUTPUT);//config buzzer pin as OUTPUT
}
void loop()
{
dry = analogRead(A0); //store sensor value in dry variable
if (dry > threshold && musicOn == false ) {// duel condtion dont know if arduino works with this
Serial.println(“Plant Needs Water”);
Serial.println(“play wav.”);
tmrpcm.play(“1.wav”);$
musicOn = true; // change variable to true
}
else if(dry <= threshold) { // change this to an else if stament with the condtion if dry is above
tmrpcm.stopPlayback();
musicOn = false; // my edit
}
}
Your version will not repeat the song if the plant is not watered. It will only play once. The OP stated they wanted the song to loop until watering.