Hello folks,
I have an problem wich i would like some feedback on from more experienced arduino users then me. I hope its not inappropriate to do such a large post as a 1st post. ( i want to explain my issue as clear as possible)
I am trying to make a small book (only 4 pages for now) wich has ldr's in it for triggering a LED (and later triggering a audio sample in an other IC).
The objective is to make a book that works good under different light conditions and i want to try a few different methods of reading and comparing LDR's
But since pretty much a noob to arduino and programming in particular im having a little trouble realizing an idea i have in code.
I have written a simple program for a book with 4 pages that works but it's a bit sensitive, when u turn the pages sometimes it will activate a led that is not supposed to light up.
The program does the following:
It reads the LDR's and maps the value to a range of 0-4.
Then it looks if there are any LDR's that have a higher value then 2.
If only LDR1 is > 2 turn on LED1, if LDR1 and 2 are > 2 turn on LED2 and so on.
This seems to work ok, when i turn to page 1 LDR1 get's lid and LED1 turn on. I turn to page 2 and LDR1 and 2 get lid and LED2 turn on. But as i mentioned before it's a bit noisy, sometimes when i turn the pages a LED lights up that isn't supposed to light up. :-[
I have an idea to fix this issue but don't know how to implement it in my programming.
My idea is the following:
I want to have have the LED switched on after the LDR value is higher then 2 for a specified amount of time.
For example: if LDR1 > 2 for longer then 300ms turn on LED1
if LDR1 and 2 > 2 for longer then 300ms turn on LED2
But i have no idea how to implement the time into the code :-[
I looked into alot of examples and tutorials but can't seem to figure it out, so i was hoping someone here could help me out.
Here's the code i have so far (remember im a programming noob who is also not very good at algebra, so it is prolly not very optimized).
/*
LDR Book
*/
const int LED1 = 8; // pin that the led is connected to
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 5;
const int LDR1 = A0; // pin for LDR1
const int LDR2 = A1; // pin for LDR2
const int LDR3 = A2; // pin for LDR3
const int LDR4 = A3; // pin for LDR4
const int sensorMin = 50; // sensor minimum, discovered through experimentation
const int sensorMax = 600; // sensor maximum, discovered through experimentation
int state = 0; // State of the LDR's
void setup() {
pinMode(LED1, OUTPUT); // Set the LED pins to 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 the LDR sensors ranges to a 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 the state of the leds depending on how many LDR's are 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 the 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 currently looking into the blink without delay and smoothing examples to see if i can implement it into my programming and
see if it helps with the noise issue.
But i would very much like to hear some input of more experienced users then me.
Also any other ideas on Triggering the pages with the LDR's are welcomed.
I am afraid that the method described above won't work in every lighting condition (light rooms, dark rooms, natural light , artifical light). But since i'm new to programming i want to start simple and am learning alot along the way.
My next idea was to have an extra 5th LDR in the book (already in the book prototype i use now) wich is used to determin the lowest and highest brightness of the room the book is in.
Then it compares the other LDR's tot the Value of the 5th LDR and if for example LDR1 is higher or has a value that is close to that of the 5th LDR, LED1 s turned on.
I made a small program that tries to do this but it wasn't working at all. I'm still looking into how to code this so that will work with a LDR that determines the room brightness.
I hope my story isn't to long and boring ::).
Any help would be much appreciated.
Best regards,
Sim