Hello! I am trying to alter someone's existing code to fit what I want to do. What I want to do is have it so that when light hits the sensor, the sound of crickets turns on (originally it was the opposite--the sound would go on when it got dark). I have done this successfully, but I'm having trouble getting it to turn OFF when there is no longer any light. Trying to make an if/else statement to do this, but I'm having some issues understanding the code being used.
This is the bit I've added:
if (sensorLevel() < thresholdLevel)
{
digitalWrite(speakerPin, LOW)
; }
This is the entirety. My apologies for its messiness and general ick, I'm brand new to building circuits and would appreciate any help
const int lightPin = 0; //the analog pin for the photoresistor
const int speakerPin = 9; //the digital pin for the piezo speaker
//Constants for chirp speed
const int fast = 20;
const int med = 60;
const int slow = 100;
//Set initial values of other variables
int thresholdLevel = 500; // an arbitrary light threshold level when chirping should start
int initialChirp = true; // a true state means that it is the first 'chirp' since the
// light has darkened and crossed the threshold level
//Check & return current light level function
int sensorLevel(){
int lightLevel = analogRead(lightPin); //read the lightLevel (between 0 and 1024)
Serial.print("Photocell reading = "); //print out the light level on the serial
Serial.println(lightLevel); // monitor (if attached) for debugging
return lightLevel; //return the value to the callng function
}
//Chirp function
//adapted from Michael Still's 'Cricket Noise Door Bell' arduino project
//http://www.stillhq.com/svn/trunk/arduino/DoorBellWithMatt/DoorBellWithMatt.pde
//We need to pass in a desired chirp length using an integer or our predefined
//constants
void chirp(int length){
for(int i = 0; i < length; i += 2)
{
delay(i);
digitalWrite(speakerPin, LOW);
delay(length - i);
digitalWrite(speakerPin, HIGH);
}
}
void setup()
{
pinMode(speakerPin, OUTPUT);
//We'll send debugging information via the serial monitor
//if attached
Serial.begin(9600);
//Shuffle the randomizer with analog noise on an
//unconnected analog pin
randomSeed(analogRead(5));
}
//Here is our main logic function - as the name suggests,
//this function loops whilever power is applied to the Arduino
void loop()
{
//if we cross the threshold, start the chirp cycle
while (sensorLevel() > thresholdLevel) {
//if initial crossing of threshold - start with a 'slow' chirp
if (initialChirp == true) {
chirp(slow);
initialChirp = 0; //let us know that we have done our initial chirp in the chirp cycle
}
if (sensorLevel() < thresholdLevel)
{
digitalWrite(speakerPin, LOW)
; }
//generate a random number between 0 and 14
int rndMediumChirp = random (14);
//send value to serial port for debugging
Serial.print("rndMediumChirp = ");
Serial.println(rndMediumChirp);
//if rndMediumChrip is 1, do a 'medium' chirp - ie, a 1 in 15 chance of any given chirp
//being 'medium' as opposed to 'fast' for added realism
if (rndMediumChirp == 1) {
chirp(med);
}
//the standard 'fast' chirps
chirp(fast);
} //end of chirp cycle
//if we are here we are outside of the chirp cycle
//let's make sure we reinitialise the initalChirp to
//true (boolean), so that next time it becomes dark, we
//have a slow chirp
initialChirp = true;
//delay between readings when not in chirp cycle
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.