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
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
#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;
}