Go Down

Topic: LED and photoresistor (Read 4 times) previous topic - next topic

SheepRusH

Hello Fellow Arduino-Lovers

I have the following problem
i got a photoresistor and 2 LED's; one LED is supposed to be on when the photoresistor isn't blocked. i want the other LED to be on when the photoresistor is blocked. after some time of searching i didn't even find a hint how my code hast to look like to realize this special kind of setup


here is the code i'm using now and which is working for one LED being on while the photoresistor isn't blocked

int lightPin = 0;
int ledPin=8;
int ledPin=9;

void setup()
{
    Serial.begin(9600);
    pinMode( ledPin, OUTPUT );
}

void loop()
{
    Serial.println(analogRead(lightPin));
    analogWrite(ledPin, analogRead(lightPin)/2);
   delay(10);
}

hope u guuys can help me =)

best wishes
SheepRusH

P.S.: I'm using the Arduino UNO

igor86

I haven't really programmed the arduino yet, so some arduino function calls may be wrong, but I think you want something like:

Code: [Select]


int lightPin = 0;
int ledPin0=8;
int ledPin1=9;

void setup()
{
    Serial.begin(9600);
    pinMode( ledPin0, OUTPUT );
    pinMode( ledPin1, OUTPUT );
    pinMode( lightPin, INPUT );
}

void loop()
{
    Serial.println(analogRead(lightPin));
    if (analogRead(lightPin)) {
          digitalWrite(ledPin0, HIGH);
          digitalWrite(ledPin1, LOW);
    } else {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin0, LOW);
    }
   delay(10);
}
[\code]

HTH
-Igor

Msquare

The original code will be "true" unless the sensor is in the very very dark (or light, depends on your circuit)
Code: [Select]
if (analogRead(lightPin)) {should be
Code: [Select]
if (analogRead(lightPin) > 500) {
Pick any value between 1 and 1023 to be the trigger value.
(If you are not using Serial.<otherfunctions> elsewhere in the code then you do not need the Serial.begin in the setup)


SheepRusH

i wanna thank u  =) now it does work as i wished

best wishes
SheepRusH

masekoriyadi

I have trouble code LED and photodiode in Arduino, if anyone can help me about the LED and photodiode?

sample code in Arduino LED and photodioda ...
thanks

Go Up