Photoresistive Light Switch/Volume Control

Excuse me Please:

I apologize for my ignorance in Arduino, I am still learning.
It is my desire for my project to have a plushy stuffed animal have sound effects {e.g. the growling puppy}
I have the code working alone for blinking LED "eyes" randomly; I have the code working alone for "play trackoo1.mp3" using an Adafruit mp3 shield{Play Music | Adafruit Music Maker Shield | Adafruit Learning System}.
I yet need to establish an "on/off" switch using a photoresister to silence the pup when the lights go out.
I have found the following code for use of a photoresister:

Switch statement

Demonstrates the use of a switch statement. The switch
statement allows you to choose from among a set of discrete values
of a variable. It's like a series of if statements.

To see this sketch in action, but the board and sensor in a well-lit
room, open the serial monitor, and and move your hand gradually
down over the sensor.

The circuit:

  • photoresistor from analog in 0 to +5V
  • 10K resistor from analog in 0 to ground

created 1 Jul 2009
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment

void setup() {
// initialize serial communication:

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// do something different depending on the
// range value:
switch (range) {
case 0: // your hand is on the sensor
case 1: // your hand is close to the sensor
case 2: // your hand is a few inches from the sensor
case 3: // your hand is nowhere near the sensor
delay(1); // delay in between reads for stability

which seems to suggest four discrete states.

But it does not detail what is/can be accomplished on those four states.

I would think it can suggest when the light goes from bright to medium/dim, the volume of the mp3 shield be decreased, and then to shut the unit down/silence the volume when DARK. {or at least put it to "sleep" low power mode, until the lights come back on}

But I am not understanding how to translate my desires for the light environment and the above code.

the volume change on dim/medium I would think an entirely separate issue,...
heh, even to combine the codes for blinking LED eyes, mp3 shield, and the photoresister.

I thought it best to just do one function at a time to establish it works as expected, then to move to the more complication,....

Thank you kindly for the assistance in my continued learning,...


But it does not detail what is/can be accomplished on those four states.

What IS accomplished is simply printing a message to the serial port. What CAN BE accomplished is up to you.

I would think it can suggest when the light goes from bright to medium/dim, the volume of the mp3 shield be decreased

Do you have programmatic access to adjust the volume? Usually that is controlled by a potentiometer on the shield. That is, it is done in hardware, not software.

To more clearly articulate my concerns, I want to know HOW to code the affects I speak of.
I am vaguely aware of the power Arduino has, it is the program grammar that I lack.
I do not understand what the photoresistive coding means, or how it is to work, or what it is to affect.
But I suggest, the primary need of on/off, that since it has two other discrete states, that those could control the volume, beyond FULL with on, and MUTE with off.

And below coding shows the volume is adjustable, note the red bolded text.

This is an example for the Adafruit VS1053 Codec Breakout

Designed specifically to work with the Adafruit VS1053 Codec Breakout

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// define the pins used
//#define CLK 13 // SPI Clock, shared with SD card
//#define MISO 12 // Input data, from VS1053/SD card
//#define MOSI 11 // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins.
// See “Connections”

// These are the pins used for the breakout example
#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
// create breakout-example object!
// create shield-example object!

void setup() {
Serial.println(“Adafruit VS1053 Simple Test”);

if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F(“Couldn’t find VS1053, do you have the right pins defined?”));
while (1);
Serial.println(F(“VS1053 found”));

SD.begin(CARDCS); // initialise the SD card

// Set volume for left, right channels. lower numbers == louder volume!
** musicPlayer.setVolume(20,20);**

// Timer interrupts are not suggested, better to use DREQ interrupt!
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
// audio playing
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int

// Play one file, don’t return until complete
Serial.println(F(“Playing track 001”));
// Play another file in the background, REQUIRES interrupts!
Serial.println(F(“Playing track 002”));

void loop() {
// File is playing in the background
if (musicPlayer.stopped()) {
Serial.println(“Done playing music”);
while (1);
if (Serial.available()) {
char c =;

// if we get an ‘s’ on the serial console, stop!
if (c == ‘s’) {

// if we get an ‘p’ on the serial console, pause/unpause!
if (c == ‘p’) {
if (! musicPlayer.paused()) {
} else {


and this is the coding for the random blinking:


  • Blink_Randomly
  • Modified from the basic Arduino example. Turns an LED on for a random time,
  • then off for a (most likely) different random time. We use pin 13 because,
  • depending on your Arduino board, it has either a built-in LED
  • or a built-in resistor so that you need only an LED.
  • Original at -

int ledPin = 9; // LED connected to digital pin 9
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup() // run once, when the sketch starts
randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output

void loop() // run over and over again
randOn = random (1000, 12000); // generate ON time between 1 and 12 seconds
randOff = random (100, 900); // generate OFF time between 0.1 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

Thank you kindly for the actual assistance.

// Set volume for left, right channels. lower numbers == louder volume!

So, you should experiment with different values, to see what value makes the device inaudible, and what value makes the volume unreasonably loud.

Then, determine the range of values that you get from the light sensor, in full sunlight and in darkness.

Then, map() the actual reading, using the from range (the minimum and maximum values from the light sensor) and the to range (the volume settings you determine to be appropriate).

Then, call setVolume() with the output of the map() call.