Help with a project

const int photo1=A0;
const int photo2=A5;
const int led_pin1=7; 
const int  led_pin2=4; 


// the setup routine runs once when you press reset: 
void setup () { 
pinMode (photo1, INPUT);
pinMode (photo2, INPUT);
pinMode (led_pin1, OUTPUT); // initialize the digital pin as an output.
pinMode (led_pin2, OUTPUT);
Serial.begin(9600);
} 


void loop () { 
int value1=analogRead(photo1);
int value2=analogRead(photo2);
delay (200);
Serial.println(value1);
if (value1-value2>100){
  digitalWrite(led_pin1,HIGH);
  digitalWrite(led_pin2,LOW);
}
else if (value2-value1>100){
  digitalWrite(led_pin2,HIGH);
  digitalWrite(led_pin1,LOW);
}
else {
  digitalWrite(led_pin1,LOW);
  digitalWrite(led_pin2,LOW); 
  delay(200);
}
}

Is there a problem in the code above?It is supposed to light up 2 LEDS according to the light the sensors get(If there is more light to the first sensor,and the difference with the other one is over 100,light up the first LED.Same goes for the second sensor.Else,none of the LEDS should light up).

Any suggestions?

Although I love that your post has code with colors. something this list should offer on the code boxes, all we can do is look.

please open your code, highlght the whole thing, copy it,
come back, open a new reply, past your code,
now comes the esscential part.
preview it, and on the reply box header, there is </>
with your code and only your code highlighters, hit that and it will put your code into a code box.

once you have done that and posted your reply, we can read your code and test it.

after all that, we will tell you to try to invert your values,
I would paste the line(s), but alas, since you did not paste your code, only a picture, I would have to make a picture and edit that. alas, I have unbuntu and there is virtually no software that even comes close to PAINT

but, take > and make it <

also, print value1, value2 and diff=value3
on your serial print so you can watch the values change.

value3 = value1-value2

You have told us what the program is supposed to do but what does it actually do ?

UKHeliBob:
You have told us what the program is supposed to do but what does it actually do ?

Instead of having 1 LED lit, in case the light difference is over 100, the LED that should be lit is flickering,doesn't have a stable behaviour.
Code incoming.

Could one or both photosensors be seeing the light from one or both LEDs? That could cause the system to oscillate.

const int photo1=A0;
const int photo2=A5;
const int led_pin1=7; 
const int  led_pin2=4; 


// the setup routine runs once when you press reset: 
void setup () { 
pinMode (photo1, INPUT);
pinMode (photo2, INPUT);
pinMode (led_pin1, OUTPUT); // initialize the digital pin as an output.
pinMode (led_pin2, OUTPUT);
Serial.begin(9600);
} 


void loop () { 
int value1=analogRead(photo1);
int value2=analogRead(photo2);
delay (200);
Serial.println(value1);
if (value1-value2>100){
  digitalWrite(led_pin1,HIGH);
  digitalWrite(led_pin2,LOW);
}
else if (value2-value1>100){
  digitalWrite(led_pin2,HIGH);
  digitalWrite(led_pin1,LOW);
}
else {
  digitalWrite(led_pin1,LOW);
  digitalWrite(led_pin2,LOW); 
  delay(200);
}
}

If that was my project I would calculate the difference and save it in a variable so I could print it

void loop () {
	int value1=analogRead(photo1);
	int value2=analogRead(photo2);
	int diff = value1 - value2;
	delay (200);
	
	Serial.print("V1 ")
	Serial.print(value1);
	Serial.print("  V2 ")
	Serial.print(value);
	Serial.print("  Diff ")
	Serial.println(diff);
	
	if (diff > 100){
	  digitalWrite(led_pin1,HIGH);
	  digitalWrite(led_pin2,LOW);
	}
	else if (diff < -100){
	  digitalWrite(led_pin2,HIGH);
	  digitalWrite(led_pin1,LOW);
	}
	else {
	  digitalWrite(led_pin1,LOW);
	  digitalWrite(led_pin2,LOW);
	  delay(200);
	}
}

...R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

So we can see how you have connected everything to your inputs and outputs.

Tom...... :slight_smile:

johnwasser:
Could one or both photosensors be seeing the light from one or both LEDs? That could cause the system to oscillate.

Hi,
Can you post a picture of your project, so we can see how you have the LDRs positioned please?
Also what are the resistor values?

Tom.... :slight_smile:

As for the resistors,they are 220 at the LEDS and 10k at the censors.

Hi.
Check your protoboard, the side strips that you use for +5 and gnd, make sure they are continuous the full length of the board.
Some have an open circuit halfway along.

Tom.... :slight_smile:

Protoboards.jpg

Protoboards1.jpg

TomGeorge:
Check your protoboard, the side strips that you use for +5 and gnd, make sure they are continuous the full length of the board.

Good pick!

The ones you illustrate - like mine - have the read and blue lines to indicate. The one he pictured is suspicious insofar as the spacing is different - the "bus" terminals all line up with the columns but there is a two hole gap in the middle instead of the one hole gap at other places.

The MB102 design has equal gaps between each set of five.