Hi,
Im trying to make a thingy where a photosensor controls 3 LEDs and a piezo buzzer. It should be so that when it is light there is no music or light, when its dark the lights and music come on.
Im trying this so that I can use some code from the tutorials I have done individually - like combining the three. I have reached the point where I could use a little help please. Im blindly guessing at stuff now. Im sure Id need to set a condition (Boolean?) so that when there is no light the lights and buzzer activate, is that right?
I left the code in for 1 more LED than there actually is, as the last LED wasn't flashing at full brightness with the correct number of LEDs in the c ode. This way it does.
Regards the loop Im having problems putting it together.
Im not electrically minded so have posted a pic of my circuit too. I have about 10hours experience with Arduino as you can see.
Any help appreciated as I keep getting errors when I Verify. Im expecting the worst.
Thanks.
//Pin Variables
int lightPin = 0;
int ledPins[] = {2,3,4,5};
int speakerPin = 9;
void setup()
{
int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255
pinMode(lightPin, OUTPUT)
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 3; i++) //this is a loop and will repeat eight times
pinMode(ledPins[i], OUTPUT); //we use this to set each LED pin to output
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
{
pinMode(speakerPin, OUTPUT);
}
/*
* loop() - this function will start after setup finishes and then repeat
* we call a function called oneAfterAnother(). if you would like a different behaviour
* uncomment (delete the two slashes) one of the other lines
*/
void loop() // run over and over again
{
lightLevel = constrain(lightLevel, 0, 1); 1;//make sure the
//value is between
//0 and 255
analogWrite(ledPins[2,3,4,5], lightLevel); 1; //write the value
oneOnAtATime(); //this will turn one LED on then turn the next one
//on turning the
//former off (one LED will look like it is scrolling
//along the line
}
/*
* oneOnAtATime() - Will light one LED then the next turning off all the others
*/
void oneOnAtATime(){
int delayTime = 70; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
for(int i = 0; i <= 3; i++){
int offLED = i - 1; //Calculate which LED was turned on last time through
if(i == 0) { //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
offLED = 3; //turn on LED 2 and off LED 1)
} //however if i = 0 we don't want to turn of led -1 (doesn't exist)
//instead we turn off LED 7, (looping around)
digitalWrite(ledPins[i], HIGH); //turn on LED #i
digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
delay(delayTime);
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
[code]
[/code]