Go Down

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

pavel1907

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
Quote

//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);
       
       delay(lightVal);
       
       digitalWrite(ledPin, LOW);
       
       delay(lightVal);
       
       lightVal1 = analogRead(ldrPin);
      
       digitalWrite(ledPin, HIGH);
      
       delay(lightVal1);
      
       digitalWrite(ledPin1, LOW);
       
       delay(lightVal1);
}


Arrch

Look at the blink without delay example.

marco_c

#2
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 http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

daatse

#3
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;

loop{
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.

AWOL

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

dxw00d

Quote
and use underscores for extra points.

Why? CamelCase, or camelBack, is perfectly acceptable.

daatse

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