Hello,
This is my first arduino code. Some parts I wrote, others I copied parts of it from what was posted online. :-[
I am trying to add a command inside void loop() and have a suspicion that the sequence of commands is not right and can't figure out how it should be done.
There will be three pir sensors installed in a room. When either one of them detects presence of a person, a playback of two soundtracks will start. One soundtrack is on cd player, another on rMP3 shield.
The soundtracks are 1 hr long so I thought to add a physical button to send 'stop' rmp3.stop();
command to the shield to have a playback reset option and I don't understand if the code for the resetButton is wrong or whether I put it in the wrong order.
Could you please`look at the code an tell me what's wrong. I will greatly appreciate it.
I hope my explanations are clear, the code is below.
Thank you very much.
M.
#include <NewSoftSerial.h>
#include <RogueSD.h>
#include <RogueMP3.h>
//sketch to start payback of soundtracks on cd player and mp3 shield
//when one of three PIR's reads HIGH
//May 1, 2010
//Mikhail Iliatov
//parts are copied from the code by mgehring and TJ on arduino.cc
#define CD 10
#define PIR1 2
#define PIR2 3
#define PIR3 4
#define resetButton 8
NewSoftSerial rmp3_serial(6, 7);
RogueMP3 rmp3(rmp3_serial);
int duration=20;
//temporary number, will be changed to actual length of the soundtrack
//(prob. 3923586 + time to start up cd (10 sec.) => 3933586 milisec or 3933.586 s ~= 3934 s)
void setup(){
Serial.begin(9600);
rmp3_serial.begin(9600);
pinMode(CD, OUTPUT);
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(PIR3, INPUT);
pinMode(resetButton, INPUT);
//this is the resetbutton I am trying to incoprporate
//so I can stop mp3 in the middle of playback if necessary
digitalWrite(PIR1, LOW);
digitalWrite(PIR2, LOW);
digitalWrite(PIR3, LOW);
digitalWrite(resetButton, LOW); //check this
rmp3.sync();
//don't know what this function does but it was in the rmp3 library sample code
int calibrationTime = 30; //do I need to use calibration, check with parallax
// int song="/NHD050708.mp3"; how to define songName?
//calibrate sensor
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
//insert stop command for cd here?
}
void loop(){
//this code doesn't work and I have no idea why or where to paste it!
if (digitalRead(resetButton) == HIGH){
digitalWrite(13, HIGH); //to turn LED on so I know that reset button works
rmp3.stop(); //to stop rmp3
}
if ((digitalRead (PIR1) == LOW) || (digitalRead (PIR2) == LOW) || (digitalRead (PIR3) == LOW)){
digitalWrite (CD, LOW);
//insert stop command for cd here to make sure nothing was triggered while calibrating sensors
}
else {
digitalWrite (CD, HIGH);
delay(500);
digitalWrite (CD, LOW);
delay(5000); //time for cd player to start
rmp3.playfile("/tinfou2_final.mp3");
Serial.println("NOW PLAYING"); //figure how to print status from MP3?
delay_secs(duration);
// pause program while the soundtrack is playing duration == length of sountrack
Serial.println("STOPPED");
}
}
void delay_secs( int secs ) // delays 'secs' seconds
{
int i;
for(i=0; i<secs; i++)
delay(1000);
}
/*think about adding button to stop playback on both cd and mp3 at any time
*/