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;
}
}
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);
}