Pages: [1]   Go Down
Author Topic: Working with 2 photoresistors  (Read 519 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



* arduino_sketch.jpg (767.14 KB, 1755x1335 - viewed 24 times.)
« Last Edit: November 17, 2012, 04:00:40 pm by sb_88 » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34532
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Modify the code to say:-
Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34532
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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.

Pages: [1]   Go Up
Jump to: