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

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);
}

Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the blink without delay example.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1186
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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()  smiley) 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!
« Last Edit: May 09, 2012, 03:11:51 am by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

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

Code:
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:
      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:
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.
« Last Edit: May 09, 2012, 05:56:42 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and use underscores for extra points.
Better still, use [code] [/code] tags when posting code
Logged

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and use underscores for extra points.
Why? CamelCase, or camelBack, is perfectly acceptable.
Logged

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

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
Logged

Pages: [1]   Go Up
Jump to: