Musik auf Knopfdruck abspielen

Hallo
Für ein Schulprojekt baue ich eine Useless Box. Wenn der Schalter umgelegt wird, erkennt das der Arduino am Pin 2 und reagiert entsprechend mit einer Servobewegung. Nun wollte ich zusätzlich noch Sound abspielen lassen, habe dazu ein SD-Shield und einen kleinen Lautsprecher mit dem Arduino verbunden. Mit dem Beispielprogramm "basic" aus der TMRpcm-Library hat alles geklappt, nun jedoch mit dem Schalter nicht mehr.

Hier der betroffene Ausschnitt aus dem Code, "now playing" wird nie auf der Konsole ausgegeben...

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch

boolean Switch = false;

void setup(){
  tmrpcm.speakerPin = 9;

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  tmrpcm.play("1.wav"); //the sound file "1.wav" will play each time the arduino powers up, or is reset
  pinMode(2,INPUT_PULLUP);
}



void loop(){  

  if(digitalRead(2) == 0){
    Switch = true;
  }

  if(Switch){    
      Serial.println("now playing...");
      tmrpcm.play("1.wav");
      Switch = false;
  }
}

Kann mir jemand helfen?

Vielen Dank

Put in another serial print to tell you when switch gets set to true.

Habe ich nun nach "Switch = true;" gemacht, wird aber ebenfalls nie ausgegeben.

It sounds like a wiring issue then.

Sollte nicht der Fall sein, der selbe Kabelbaum funktioniert problemlos in der Grundanwendung mit folgendem Code:

#include <Servo.h>  // the servo library

Servo arm;  // create servo object to control a servo
Servo door;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  
  door.attach(3);
  arm.attach(5);
  pinMode(2,INPUT_PULLUP);

}

void loop() {

if(digitalRead(2) == 0){

  Serial.println("Interrupt!");

  for(int x = 0; x<=96; x+=4){
   door.write(x);                  // sets the servo position according to the scaled value
   delay(15);                        // waits for the servo to get there
 }

for(int x = 0; x<=98; x+=2){
   arm.write(x);                  // sets the servo position according to the scaled value
   delay(15);                        // waits for the servo to get there
 }

 delay(100);

 for(int x = 100; x>=2; x-=2){
   arm.write(x);                  // sets the servo position according to the scaled value
   delay(15);                        // waits for the servo to get there
 }

 for(int x = 100; x>=4; x-=4){
   door.write(x);                  // sets the servo position according to the scaled value
   delay(15);                        // waits for the servo to get there
 }

 delay(100);

}

Serial.print("n");
delay(500);

}

Ich schlage vor, dass Sie alles in Ihrem Originalcode (einschließlich der Bibliothek), das sich auf TMRpcm bezieht, kommentieren und erneut testen.

Ich vermute, dass der TMRpcm Pin 2 verwendet und deshalb Ihr digitalRead ausfällt.

(Google translate)

Falls TMRpcm Pin 2 verwendet, gibt es eine andere Library die ich verwenden kann, um Sound von einer SD-Karte abzuspielen?

Warum nicht den Schalter auf einen anderen Pin legen?

Haben Sie sich selbst bewiesen, dass Pin 2 das Problem ist?

Die erste Zeile im Setup sagt, dass der Speaker an Pin 9 hängt.

tmrpcm.speakerPin = 9;

Gruß Tommy

Pin 6 als Input hat funktioniert, danke für den Tipp

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.