I am trying to make a project on Home Security.
When laser beam is off arduino will play an audio file from sd card with tmrpcm library. And when a button is pressed the audio will be stopped. But the audio file is not playing. I don't know why. Please help me.Here is the code:
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <SPI.h>
TMRpcm tmrpcm;
const int sensivity = 400;
#define ldr A0
#define buttonPin A2
int buttonState;
int state = 0;
void setup() {
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.speakerPin = 9;
}
void alarmOFF() {
tmrpcm.disable();
}
void alarmON() {
tmrpcm.stopPlayback();
tmrpcm.setVolume(2);
tmrpcm.play("1.wav");
}
void loop() {
int light = analogRead(ldr);
int button = analogRead(buttonPin);
if (button >= 1020) {
state = 0;
}
if (light > sensivity) {
state = 1;
}
if ((light <= sensivity) && (state == 0)) {
alarmOFF();
}
if (state == 1) {
alarmON();
}
}