1

I have the speaker working with the tmrpcm.play working, but this only plays when the arduino is powered up or reset. Can i make the .wav play in anther other ways? I wan't the file to play when i open a closet and my LDR resistor is affected by light.

#include <SD.h>                           //include SD module library
#include <TMRpcm.h>                       //include speaker control library

#define SD_ChipSelectPin 4                //define CS pin

TMRpcm tmrpcm;                            //crete an object for speaker library

const int ledPin = 8;
const int buzzerPin = 10;
const int ldrPin = A0;
int ledState = LOW;  
int buzstate = 600;
unsigned long previousMillis = 0;        

int ldrStatus = analogRead(ldrPin);  //read the state of the LDR value

const long interval = 400; 

void setup () {

  tmrpcm.speakerPin = 9;    
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ldrPin, INPUT);
   
 }
void LedAndBuzzer(){
   unsigned long currentMillis = millis();
  int ldrStatus = analogRead(ldrPin);  //read the state of the LDR value

 if (ldrStatus >= 400) {

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    digitalWrite(buzzerPin, ledState);
}}
}
void soundon(){
    if (!SD.begin(SD_ChipSelectPin)) {      //see if the card is present and can be initialized
    
    return;                               //don't do anything more if not
  }
  else {
  tmrpcm.setVolume(6);                    //0 to 7. Set volume level
  tmrpcm.play("4.wav"); }
}
 
void loop() {
    

    
  }
7
  • Yes, that is totally possible. You need to write the code for it. What have you tried so far? Do you know how to read an LDR? Have you searched for tutorials?
    – chrisl
    Commented May 4, 2021 at 19:27
  • @chrisl thanks for the respons, yes i have googled a lot, but im quite new to programming and working with the arduino. This is the code (updated the post) that in working on right know. There is one more element than my original question, i want it to start the led/buzzer loop at the same time as the .wav file is played. Is this posible?
    – Elisabot
    Commented May 4, 2021 at 19:41
  • Is tmrpcm.play() a blocking function?
    – Fahad
    Commented May 4, 2021 at 23:13
  • @Fahad i don't know what that means
    – Elisabot
    Commented May 5, 2021 at 5:50
  • 1
    @Fahad tmrpcm.play() is non blocking. It starts playing then tmrpcm.stopPlayback() stops the music and tmrpcm.isPlaying() return the state of the player.
    – Dorian
    Commented May 5, 2021 at 9:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.