Timed LDR sensor readings


hello folks,

i have problem wich feedback on more experienced arduino users me. hope not inappropriate such large post 1st post. ( want explain issue clear possible)

i trying make small book (only 4 pages now) wich has ldr's in  for triggering led (and later triggering audio sample in other ic).

the objective make book works under different light conditions , want try few different methods of reading , comparing ldr's

but since pretty noob arduino , programming in particular im having little trouble realizing idea have in code.


i have written simple program book 4 pages works it's bit sensitive, when u turn pages activate led not supposed light up.

the program following:

it reads ldr's , maps value range of 0-4.
then looks if there ldr's have higher value 2.
if ldr1 > 2 turn on led1, if ldr1 , 2 > 2 turn on led2 , on.


this seems work ok, when turn page 1 ldr1 get's lid , led1 turn on. turn page 2 , ldr1 , 2 lid , led2 turn on. mentioned before it's bit noisy, when turn pages led lights isn't supposed light up.  :-[

i have idea fix issue don't know how implement in programming.
my idea following:

i want have have led switched on after ldr value higher 2 specified amount of time.
for example: if ldr1 > 2 longer 300ms turn on led1
if ldr1 , 2 > 2 longer 300ms turn on led2 

but have no idea how implement time code  :-[
i looked alot of examples , tutorials can't seem figure out, hoping here me out.

here's code have far (remember im programming noob not @ algebra, prolly not optimized).

code: [select]
/*
ldr book
   
*/

const int led1 = 8;             // pin led connected to
const int led2 = 7;
const int led3 = 6;
const int led4 = 5;
const int ldr1 = a0;            // pin ldr1
const int ldr2 = a1;            // pin ldr2
const int ldr3 = a2;            // pin ldr3
const int ldr4 = a3;            // pin ldr4
const int sensormin = 50;       // sensor minimum, discovered through experimentation
const int sensormax = 600;      // sensor maximum, discovered through experimentation
int state = 0;                  // state of ldr's


void setup() {
 pinmode(led1, output);        // set led pins output
 pinmode(led2, output);
 pinmode(led3, output);
 pinmode(led4, output);
 
 serial.begin(9600);           // initialize serial communication:
}


void loop (){
 //read ldr1 through 4
 int ldr1val = analogread(ldr1);
 int ldr2val = analogread(ldr2);
 int ldr3val = analogread(ldr3);
 int ldr4val = analogread(ldr4);
 
 // map ldr sensors ranges range of 4 options
 int ldr1range = map(ldr1val, sensormin, sensormax, 0, 3);
 int ldr2range = map(ldr2val, sensormin, sensormax, 0, 3);
 int ldr3range = map(ldr3val, sensormin, sensormax, 0, 3);
 int ldr4range = map(ldr4val, sensormin, sensormax, 0, 3);
 
 
 // set state of leds depending on how many ldr's getting light
if(ldr1range >= 2)
   state = 1;

if(ldr1range && ldr2range >= 2)
  state = 2;

if(ldr1range && ldr2range && ldr3range >= 2)
 state = 3;

if(ldr1range && ldr2range && ldr3range && ldr4range >= 2)
 state = 4;

if(ldr1range < 2)
 state = 0;

// turn on led depending on state
if (state ==0){
 digitalwrite(led1, low);
 digitalwrite(led2, low);
 digitalwrite(led3, low);
 digitalwrite(led4, low);
}
if (state ==1){
 digitalwrite(led1, high);
 digitalwrite(led2, low);
 digitalwrite(led3, low);
 digitalwrite(led4, low);
}
if (state ==2){
 digitalwrite(led1, low);
 digitalwrite(led2, high);
 digitalwrite(led3, low);
 digitalwrite(led4, low);
}
if (state ==3){
 digitalwrite(led1, low);
 digitalwrite(led2, low);
 digitalwrite(led3, high);
 digitalwrite(led4, low);
}
if (state ==4){
 digitalwrite(led1, low);
 digitalwrite(led2, low);
 digitalwrite(led3, low);
 digitalwrite(led4, high);
}
}
   


i'm looking blink without delay , smoothing examples see if can implement programming ,
see if helps noise issue.
but hear input of more experienced users me.


also other ideas on triggering pages ldr's welcomed.

i afraid method described above won't work in every lighting condition (light rooms, dark rooms, natural light , artifical light). since i'm new programming want start simple , learning alot along way.

my next idea have 5th ldr in book (already in book prototype use now) wich used determin lowest , highest brightness of room book in.
then compares other ldr's tot value  of 5th ldr , if example  ldr1  higher or has value close of 5th ldr, led1 s turned on.
i made small program tries wasn't working @ all. i'm still looking how code  that work ldr determines room brightness.


hope story isn't long , boring  ::).


any appreciated.


best regards,

sim

code: [select]
if(ldr1range && ldr2range >= 2)
this not testing ldr1range , ldr2 range greater or equal 2. testing ldr1range true, , if is, whether ldr2range greater or equal true.

there no shortcuts.
code: [select]
if(ldr1range [glow]>= 2[/glow] && ldr2range >= 2)


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Timed LDR sensor readings


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums