#include <SoftwareSerial.h>
/*
Random Sound Player
By biocow
November 2010
*/int distanceVal;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the averageint inputPin = A5;
void setup() {
// initialize the digital pin as an output:
pinMode(6, OUTPUT); // On, Play and pause
pinMode(7, OUTPUT); // Forward track
pinMode(0, INPUT); // will listen for the end of the trackfor (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;Serial.begin(9600);
}void loop() {
// Get the readings from the IR sensor
// and smooth them out.// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
Serial.println(average, DEC);// is someone there? Let's see
if(average > 30) {
// turn on the MP3 player
Serial.println("turning on player");
digitalWrite(6, HIGH);
delay(1400);
digitalWrite(6, LOW);// wait for it to power up
delay (4500);// advance one track to make sure we're at the start of a song
digitalWrite(7, HIGH);
delay(200);
digitalWrite(7, LOW);
delay(900);
Serial.println("advance one track");// Play the current track
digitalWrite(6, HIGH);
delay(200);
digitalWrite(6, LOW);
Serial.println("Play");// listen to the current song for a bit
// later we'll wait and listen for the end of the track
delay(5000);
Serial.println("How do you like this song?");
delay(5000);// pause the song
digitalWrite(6, HIGH);
delay(200);
digitalWrite(6, LOW);
Serial.println("Song is over");// Advance the tracks a random number of times
for(int x = 1; x <= random(1,7); x++) {
digitalWrite(7, HIGH);
delay(200);
digitalWrite(7, LOW);
delay(1000);
Serial.print("Advance track ");
Serial.print(x);
Serial.println(" time(s)");
}// delay
delay(700);// turn off the MP3 player
Serial.println("shutting down");
digitalWrite(6, HIGH);
delay(1400);
digitalWrite(6, LOW);
delay(5000); // give it time to turn off// Reset all the sensor stuff
average = 0;
total = 0;
for(int l = 0; l < numReadings; l++) {
readings[l] = 0;
}// wait a good bit of time before we reset
int pause = random(10000,20000);
Serial.print("Pausing for ");
Serial.print(pause / 1000);
Serial.println(" seconds");
delay(pause);}
}