Hello Arduinites,
I am fairly new to writing sketches for an Arduino but as I was working on a sketch, I think things started to make since but I wanted others opinions before I patted myself on the back. I am running a Duemilanove to drive a four relay module which will operate a linear actuator, led light, fog machine and play audio via a YX5300 player as the linear actuator gets 50% extended. In my mind, the light will turn on and then a burst of fog will happen, at that point the actuator will extend and while it is in its process, an audio file will play. Once extended, it will pause shortly and then retract after which the light will turn off. This will be triggered by a PIR. The process will continue as people walk by the sensor. My biggest unknown is to only play the mp3 file once each time the sequence is run.. do I take it out of the loop statement?
Here is my sketch: [code]
type or paste PIR to trigger again:
Simple SerialMP3Player "zombie moan" example of YX5300 chip.
Copy the "zombie.mp3" file to an empty SD card
Connect the Serial MP3 Player to the Arduino board
GND â GND
VCC â 5V
TX â pin 11
RX â pin 10
After compile and upload the code you must hear âzombie moanâ.
by Steve Larsen
*******************************************************************************/
// digital pin 2 has a PIR attached to it.
int pir = 2;
// digital pin 5 has a fog trigger attached to it.
int fog = 5;
// digital pin 6 has led attached to it.
int led = 6;
// digital pin 7 will retract a linear actuator.
int retract = 7;
// digital pin 8 will extend a linear actuator.
int extend = 8;
#include "SerialMP3Player.h"
#define TX 11
#define RX 10
SerialMP3Player mp3(RX, TX);
void setup() {
// make the pir an input:
pinMode (pir, INPUT);
// make the fourth relay an output to fog machine:
pinMode (fog, OUTPUT);
// make the third relay an output to led light:
pinMode (led, OUTPUT);
// make the second relay an output to retract actuator:
pinMode (retract, OUTPUT);
// make the first relay an output to extent actuator:
pinMode (extend, OUTPUT);
//initialize pin 7 as high:
digitalWrite (retract, HIGH);
//initialize pin 8 as high:
digitalWrite (extend, HIGH);
Serial.begin(9600); // start serial interface
mp3.begin(9600); // start mp3-communication
delay(500); // wait for init
mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
delay(500); // wait for init
mp3.play(); // Play "zombie.mp3". You must hear "zombie moan"
}
// the loop function runs over and over again forever
void loop() {
// read the input pin;
int pirState = digitalRead(pir);
// print out the state of the pir;
Serial.println(pirState);
delay(1); //delay is between reads for stability
digitalWrite (led, HIGH);
delay (1000);
digitalWrite (fog, HIGH);
delay (2000);
digitalWrite (fog, LOW);
delay (500);
digitalWrite (extend, HIGH);
digitalWrite (retract, LOW);
delay (4000);
mp3.play(); // Play "zombie.mp3". You must hear "Moan"
delay (5000);
digitalWrite (extend, LOW);
digitalWrite (retract, HIGH);
if (digitalRead (2) == HIGH and digitalRead (3) == LOW) {
//extend linear actuator:
digitalWrite (extend, HIGH);
digitalWrite (retract, LOW);
}
else if (digitalRead (2) == LOW and digitalRead (3) == HIGH) {
//retract the linear actuator:
digitalWrite (extend, LOW);
digitalWrite (retract, HIGH);
}
else {
//stops Actuator:
digitalWrite (extend, HIGH);
digitalWrite (retract, HIGH);
}
digitalWrite (led, LOW);
// end program and wait for
} code here