I am new to Arduino and coding.
My project is to make a simple MP3 player, that plays .wav music files from a SD card reader. I have so far been able to play the music using two 8ohm speakers. I have mostly set it up using the info from this website https://diyhacking.com/arduino-audio-player/.
I wanted to add a photo-resistor to the circuit, so that it will only play when there is sufficient light.
The problem I am having is that most forums explain how to use a photo-resistor for a LED and therefore are using pinMode() and digitalWrite(), these don't seem to work with the code that I have used for the 'MP3' player.
I have a copied the code I am trying to use at the moment, below.
int sensorPin = A0; // select the input pin for ldr
int sensorValue=0 ; //variable to store the value coming from the sensor
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
void setup() {
tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
if (!SD.begin(SD_ChipSelectPin)){ // see if the card is present and can be initialized:
return; // don't do anything more if not
}
tmrpcm.volume(1);
tmrpcm.play("11.wav");
}
void loop() {
sensorValue = analogRead(sensorPin);
if(sensorValue >300){ //setting a threshold value
digitalWrite(9,HIGH);
}
else digitalWrite(9,LOW); //turn relay OFF
}