1 photoresistor 2 leds (sorry for my english)

Hi,

I am making a school project until the 14th, but i am new with programming.

A photoresistor controlls the leds. If i place my hand over the photoresistor, led2 goes on.

If I cover it, but then remove my hand, led1 goes on.

PLEASE HELP ME!

I need a programm that looks like this:

int photoIn = 1;
int led1 = 11;
int led2 = 12;
int setPoint = 350;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}
void loop() {
  Value = analogRead(photoIn);
  
  if (Value < setPoint;
       delay(500);
      Value > setPoint;)
  {
    digitalWrite(led1, HIGH);
     delay(1000)
    digitalWrite(led1, LOW);
  }

  if (Value < setPoint;
       delay(500);
      Value < setPoint;)
  {
    digitalWrite(led2, HIGH);
     delay(1000)
    digitalWrite(led2, LOW);
  }

Kevin,

I suggest you start by reviewing the [reference for the IF statement. It appears that your code does not match how that is defined.

Also, it is not clear to me whether you are trying to:

  1. turn on LED1 whenever the photoresistor is not covered, or

  2. turn on LED1 when the photoresistor has been covered and then uncovered. (For this behavior, do you want LED1 to turn on if the cover is removed from the photoresistor at any time after the photoresistor is covered, or only if the cover is removed during the time LED2 is on?

To do 1), you might find it easiest to use [the IF… ELSE statement.

To do 2), I can think of two ways to do this:

a) Set a flag when ever the photoresistor is covered, then turn on LED 1 if (the flag is set) and (the photoresistor is not covered)

b) Nest a loop inside the results of the IF statement that turns on LED2 if the photoresistor is no longer covered.

A couple of other questions occurred to me about the what you want the behavior to be:

  1. Do you want LED2 to be on for a fixed period after when you put your hand over the photoresistor or for a fixed period after you remove your hand? For example, if you put your hand over the photoresistor and leave it there for 2 seconds, do you want LED2 to turn off 1 second after you put your hand there, or 1 second after you remove your hand?

  2. Do you want to leave LED1 on or turn it off if the photoresistor is covered again?

More generally, you might find it helpful to draw a [flowchart of what you want the program to do. (I realize that flowcharts are not popular these days, but I learned to program >40 years ago and I still find them useful.)

Just because many beginners have difficulty wiring photoresistors properly, I suggest you use a voltmeter to make sure that the voltage being applied to the analog input pin is behaving the way you need it to when you cover and uncover the sensor.

Lastly, there is no reason to apologize for your English. I am an American who has spent much of his time in other countries and working in the States with people for whom English is a second or third language. My general impression is that people who apoligize for their English often use it more properly than most of Americans (none of whom apologizes for his use).

If you need more help, post again.

Have Fun,

Eric](Flowchart - Wikipedia)](http://arduino.cc/en/Reference/Else)](http://arduino.cc/en/Reference/If)