Go Down

Topic: Working with 2 photoresistors (Read 539 times) previous topic - next topic

sb_88

Nov 17, 2012, 08:01 pm Last Edit: Nov 17, 2012, 10:00 pm by sb_88 Reason: 1
Hello!

I am a newbie in arduino and I am trying to control a LED brightness with photoresistors. Basically, what I am trying to achieve is that each photoresistor will correspond to different brightness level.


For some reason I am not able to achieve that, and only one of the photoresistor is actually working.

Here is the code:



int lightPin = 0;  //define a pin for Photo resistor
int lightPin2 = 1;  //define a pin for Photo resistor

int ledPin=9;

int brightness = 0;  
int fadeAmount = 5;  

int  sensorValue1;
int  sensorValue2;



void setup()
{
   Serial.begin(9600);  //Begin serial communcation
   pinMode( ledPin, OUTPUT );
 
}

void loop()
{
 
  sensorValue1  = analogRead  (0);
  ensorValue2  = analogRead  (1);

 
   Serial.println(analogRead(lightPin));
   Serial.println(analogRead(lightPin2));
   analogWrite(ledPin,brightness);
   
    brightness = brightness + fadeAmount;


if (sensorValue1  < 800){

  brightness = 255;
  fadeAmount = -fadeAmount ;
 
}
 
 
if (sensorValue2 < 500){

  brightness = 5;
  fadeAmount = -fadeAmount ;
 

} else {
 

brightness = 0 ;
fadeAmount = -fadeAmount ;
}


  delay(10);
}


And I am also attaching the circuit schematic.

I am doing this with the help of different tutorials, and I am just stuck right now. Possibly I am making some really stupid mistakes, but just can't figure it out.

If someone could just help or give some piece of advice I would be very thankful.


Grumpy_Mike

Modify the code to say:-
Code: [Select]
FirstsensorValue  = analogRead  (0);
   SecondsensorValue  = analogRead  (1);


    Serial.println(FirstsensorValue);
    Serial.println(analogRead(SecondsensorValue));

And say what values you see as the sensors are covered up.

sb_88

Hi!

Thanks for the reply!

the sensor that is connected to the Analog pin 0 (sensorValue1) show values between:

When not covered is around 557, and when covered 240, with moreless variation (sometimes 300 and sometimes around 170)

557
292
561
178
552
309
551
235
557
296
559
227
545

The sensor that is connected to Analog pin 1 (sensorValue2), shows a little more difference:

Around 580 and sometimes more than 600, and when is covered 160, 150, 117, going sometimes to the 200s

688
486
160
266
687
534
159
288
107
183
138
164
113
142
102
128
129
135
238
207
144
151
135
144
122
130
123
130
122



BTW, there were some mistakes in the code that I corrected. I thought it could be misleading... The code is correct now and basically only the pin connected to the analog 1 is actually working. I wonder if the problem is in the circuit?

Grumpy_Mike

Quote
basically only the pin connected to the analog 1 is actually working. I wonder if the problem is in the circuit?

If you are getting readings from it then there is no problem with the circuit.
However, the fact it is not working like you expect shows that you are not getting the code right.
Post what you have now. However code will always do what it is written to do so there is no way from just looking at it that you can normally spot what is wrong. You need to also say what you expect it to do and what you see it doing.

dc42

From the Fritzing diagram, it appears that your LED is not connected correctly. The LED should be in series with it. If you leave it connected as shown in the diagram, you are likely to damage the Arduino and possibly the LED.

Maybe the light from the LED is affecting the resistance of the photoresistor connected to pin A0?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up