Go Down

Topic: light sensor (Read 717 times) previous topic - next topic


i need some help on a school project and was wondering if some one can help. i need to make 2 light sensors to have them control 2 leds so that  if i were to cover one of the leds the led should flash slower  and if i put a light on the led the light should flash for for a longer time

//light sensor proget 

int ledPin = 6;

int ldrPin = 0;

int lightVal = 0;

int ledPin1= 7;

int ldrPin1 = 1;

int lightVal1 = 1;

void setup()
       pinMode(ledPin, OUTPUT);
        pinMode(ledPin1, OUTPUT);

void loop()
       lightVal = analogRead(ldrPin);
       digitalWrite(ledPin, HIGH);
       digitalWrite(ledPin, LOW);
       lightVal1 = analogRead(ldrPin);
       digitalWrite(ledPin, HIGH);
       digitalWrite(ledPin1, LOW);


Look at the blink without delay example.


May 09, 2012, 09:22 am Last Edit: May 09, 2012, 10:11 am by marco_c Reason: 1
The blink without delay example is what you should look at and make 2 of them running together.

Your problem is/will be that the the delay() function stops the program at that point (should be called stop()  :)) and so will not allow you to smoothly control 2 lights at the same time. You need to implement a scheme that does not just stop the program - the example shows you how to do this with the millis() function and a few variables.

Good luck!
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


May 09, 2012, 12:40 pm Last Edit: May 09, 2012, 12:56 pm by AWOL Reason: 1
Code: [Select]
unsigned long time = o;
delay = 0; // the delay time
boolean led1 = FALSE; //status of led 1
int ledPin = 6;
int ldrPin = 0;

delay= analogRead(ldrPin);
if (time+delay < millis()){
 if(led1 == FALSE) digitalWrite(ledPin, HIGH); // this toggels the led, there probably is a neater way to do it but i couldnt think of it
 else digitalWrite(ledPin, LOW);
led1 != led1; // toggel the status of the led
time = millis(); // reset your timer starting point so that the next calculation

the above isn't very neat but i think it is about what you need if you correct it,
also in your original code
Code: [Select]
      lightVal1 = analogRead(ldrPin);    
      digitalWrite(ledPin, HIGH);
dont make sense. i think you wanted the variables "ldrpin1" and "ledpin1" there.

anyway as suggested look at the timer blink example (called blink without delay i think) and you wil be good to go

one last tip: try to neatly organize your variables
the same things below eachother and in the same style
dont start counting at nothing an then jump to 1 like you did but rather start at zero then and use underscores for extra points.
Code: [Select]
int led_Pin_0 = 6;
int led_Pin_1= 7;

int ldr_Pin_0 = 0;
int ldr_Pin_1 = 1;

int lightVal_0 = 0;
int lightVal_1 = 1;

Moderator edit: Code tags added, italics tags removed.


and use underscores for extra points.

Better still, use [font=Verdana][[/font]code] [font=Verdana][[/font]/code] tags when posting code
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


and use underscores for extra points.

Why? CamelCase, or camelBack, is perfectly acceptable.


excuse me for not using [code ] [/code ]
but obviously the underscore thingy was just a joke, i am well aware that you don't get extra points for that . . .
however my advice still remains the same when using numbers in a variable title.
sepparate them with a underscore form the main title.
this chitchat however does not help pavel so i will not further polute this topic with none helpfull information besides this message.

Pavel, could you give us an update if you fixed youre program or still have questions/issues

Go Up