Hey, So I am working on a project right now that is using 3 sensors together with the arduino uno. The three sensors are OSEPP microphone, IR thermometer MLX90614 and PIR motion sensor. The setup we are aiming for is such that when the pir motion and microphone are activated, we want it to play music through the speaker. We are also using a sparkfun mp3 player shield that connects the sd card and to the speaker using the aux cord. In the final code that we have, the ir thermometer and motion sensor gives us the right values however, the microphone does not give the right value.
#include <pcmRF.h>
#include <pcmConfig.h>
#include <TMRpcm.h>
#include <SdFatConfig.h>
#include <sdios.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <BlockDriver.h>
#include <SysCall.h>
#include <SFEMP3Shield.h>
#include <SFEMP3ShieldConfig.h>
#include <SFEMP3Shieldmainpage.h>
#include <SPI.h>
#include <Wire.h> // I2C library, required for MLX90614
#include <SparkFunMLX90614.h> // SparkFunMLX90614 Arduino library
//include SD module library
//include speaker control library
#include <SPI.h>
SdFat sd;
SFEMP3Shield MP3player;
//define CS pin
//crete an object for speaker library
IRTherm therm;
const int ledpin=13;
const int soundpin=A3;
const int threshold=140;
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 2000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
MP3player.setVolume(0, 0);
therm.begin(); // Initialize thermal IR sensor
therm.setUnit(TEMP_C);
pinMode(ledpin, OUTPUT);
pinMode(soundpin, INPUT);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(500);
}
////////////////////////////
//LOOP
void loop(){
if (therm.read()) // On success, read() will return 1, on fail 0.
{
// Use the object() and ambient() functions to grab the object and ambient
// temperatures.
// They'll be floats, calculated out to the unit you set with setUnit().
Serial.print("Object: " + String(therm.object(), 2));
//Serial.write('°'); // Degree Symbol
Serial.println("C");
Serial.print("Ambient: " + String(therm.ambient(), 2));
//Serial.write('°'); // Degree Symbol
Serial.println("C");
Serial.println();
delay(1000);
}
int soundsens=analogRead(soundpin);
// if (soundsens>=threshold){
// digitalWrite(ledpin,HIGH);
// Serial.println(soundsens);
// delay(100);
// }
// else{
// digitalWrite(ledpin, LOW);
// }
// if(digitalRead(pirPin) == HIGH && soundsens>=threshold){
// PLAY MUSIC
// }
Serial.println(soundsens);
delay(500);
if(digitalRead(pirPin) == HIGH && soundsens>=threshold){
MP3player.playTrack(1);
}
if(digitalRead(pirPin) == HIGH){
//the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
if(digitalRead(pirPin) == LOW || soundsens < threshold){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
}
}