Leds not turning off

Ok i will try to give as much information as possible.
I am working on a kind of music box for my girlfriend and i got it almost all working except for 1 thing (So far...)
I want it so that the leds will be turned off always, except when the light detector detects that there is enough light.
But the problem is even when there is not enough light, the leds are on.
What am i doing wrong cause i can't figure out what i did wrong.
Sorry for the kind of messy code but i am not a 100% perfect programmer :confused:
Thanks in advance and i hope someone can help me as upcomming friday is her birthday and i would love to give it to her by then :slight_smile:

#define DEBUG //uncomment this to enable debugging in monitor

#include <SPI.h> // Library for shield
#include <SdFat.h> // Library for sd card on shield
#include <SdFatUtil.h> // Library for sd card on shield
#include <SFEMP3Shield.h> // Library to control shield
#include <VarSpeedServo.h> // Special Library for controlling the servo

//create and name the library object
SFEMP3Shield MP3player;
SdFat sd;
SdFile file;
VarSpeedServo servo;

// Define pins and such
byte result;
int sensorPin = A0; // Light Sensor connected to Analog0
int sensorValue; // variable to store the value coming from the sensor
int servoPin = 5; // Pin controlling the servo
int randomnr; // Random number for choosing a song from the song array we use later
boolean firstSong = 1; // This is so the first track on the sd card will always play first when booting up the arduino

void setup() // run once, when the sketch starts
{
  Serial.begin(115200); // initialize the serial port
  pinMode(A0, INPUT); // sets analog pin A0 to be an input
  pinMode(A1, OUTPUT); // sets analog pin 3 to be an output
  pinMode(A2, OUTPUT); // sets analog pin 3 to be an output
  if(!sd.begin(SD_SEL, SPI_FULL_SPEED)) sd.initErrorHalt();;
  
  //boot up the MP3 Player Shield
  MP3player.begin();
}

void loop() // run over and over again
{ 
  sensorValue = analogRead(sensorPin); // read the value from the light sensor
  delay(250); // Wait for a short moment
  
  #ifdef DEBUG
  Serial.print("Light Sensor Value: "); // send that value to the computer
  Serial.println(sensorValue);

  Serial.print("Boolean Value first song: "); // send that value to the computer
  Serial.println(firstSong); // send that value to the computer

  Serial.print("Light detected: ");
  Serial.println(isLightDetected(sensorValue));
  #endif  
 
  if(isLightDetected(sensorValue)) //Light Detected function is true
  {
    if(!servo.attached()){ // We got to check if the servo is attached, so it will not attach it everytime it loops. This is made so we can stop the servo spinning as well when there is not enough light
      servo.attach(servoPin);
    }
    digitalWrite(A1, HIGH); //turns on led connected to pin 3
    digitalWrite(A2, HIGH); //turns on led connected to pin 4
    delay(500); // Wait half a second for turning on the servo...
    servo.writeMicroseconds(1535);
    
    if(firstSong == 1) // Check if song hasnt played before, if so start it up!
    {
      char firstsong[] = "track001.mp3";
      result = MP3player.playMP3(firstsong);
      delay(500);
      firstSong=0;
      
      #ifdef DEBUG
        Serial.print("Boolean Value after song: "); // send that value to the computer
        Serial.println(firstSong); // send that value to the computer
     #endif
    }

    randomnr = random(0,4); //random number generated for choosing a song from the array
    //Song array for the music to be choosen random
    char* trackArray[] = {
    "track001.mp3",
    "track002.mp3",
    "track003.mp3",
    "track004.mp3",
    "track005.mp3"  
    };
    
    result = MP3player.playMP3(trackArray[randomnr]); // Play the song!
    sensorValue = analogRead(sensorPin); //Read value from sensor to see if it still has enough light
    #ifdef DEBUG
      Serial.print("Light Sensor Value After: "); // send that value to the computer
      Serial.println(sensorValue);
    #endif
  }
  else //No Light Detected
  {
    #ifdef DEBUG
      Serial.println("Not enough light detected shutting everything off"); // 
    #endif
    //Stop everything!
    MP3player.stopTrack();
    digitalWrite(A1, LOW); // Turn of leds
    digitalWrite(A2, LOW); // Turn of leds
    servo.detach();
  }
}

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue >= 1;
}
  pinMode(A0, INPUT); // sets analog pin A0 to be an input
  pinMode(A1, OUTPUT); // sets analog pin 3 to be an output
  pinMode(A2, OUTPUT); // sets analog pin 3 to be an output

That is NOT what that function does. pinMode() does NOTHING to analog pins. Fix the comments!

    randomnr = random(0,4); //random number generated for choosing a song from the array

Generate a random number between 0 and 3 (inclusive) to select one of 5 songs. Don't like the last one, eh?

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue >= 1;
}

So, 0 means no light. Any value above 0 means light. Hmmm...

That is NOT what that function does. pinMode() does NOTHING to analog pins. Fix the comments!

Even when i tried it at pin 3 or 4 (as they where, it still doesnt do anything)

Generate a random number between 0 and 3 (inclusive) to select one of 5 songs. Don't like the last one, eh?

Was just for testing, this is not the final code yet. As i said i am almost no 100% perfect programmer :confused:

So, 0 means no light. Any value above 0 means light. Hmmm...

The light sensor returns 0 if there is indeed no light. anything above that it means it can sense light. It is a kind of treasure chest i have the stuff in. As soon as it opens it plays the music and all such work fine. Except for the led part :confused:

Hey Paul,

Take a look at https://www.arduino.cc/en/Tutorial/AnalogInputPins

You said

pinMode() does NOTHING to analog pins.

NOT my experience, or what Arduino.cc says...

---------------------( COPY )---------------------
The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:

pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
Pullup resistors

The analog pins also have pullup resistors, which work identically to pullup resistors on the digital pins. They are enabled by issuing a command such as

digitalWrite(A0, HIGH); // set pullup on analog pin 0
while the pin is an input.
-----------------( END COPY )----------------------

Details!

Thanks got it fixed now :slight_smile:
Changed some parts in the code, and now it works :smiley:
Thanks for the help !