Go Down

Topic: Controlling multiple LEDs using LDRs to turn them on and off (Read 727 times) previous topic - next topic

T_Wild

Afternoon,

I'm hoping somebody could help with with a project to turn LEDs on and off when you move something (e.g. hand) over them using LDRs. I started with one LED and one LDR and used this code which seems to work fine.


Code: [Select]
int sensorReading;
const byte LEDPin = 6;

void setup()
{
 Serial.begin(9600);
 pinMode(6,OUTPUT); //LED
}

void loop()
{
 sensorReading=analogRead(0); //LDR
 if (sensorReading<850)
 {
   digitalWrite(6,HIGH); //LED,ON
 }
 else digitalWrite(6,LOW); //LED,OFF
 Serial.println(sensorReading);
 delay(100);
}



I understand the script in the "void loop" is what turns the LED on and off when you move your hand over the LDR. But if I want to have an array of LEDs all connected to individual LDRs that turn on and off independently when you move your hand over them one by one, is there a simpler way of writing the code to achieve this other than copying and pasting the above void loop and changing the corresponding analogRead and digitalWrite (as below)?


Code: [Select]
int sensorReading;
const byte LEDPin(1) = 6;
const byte LEDPin(2) = 7;

void setup()
{
 Serial.begin(9600);
 pinMode(6,OUTPUT); //LED1
 pinMode(7,OUTPUT); //LED2
}

void loop()
{
 sensorReading=analogRead(0); //LDR1
 if (sensorReading<800)
 {
   digitalWrite(6,HIGH); //LED1,ON
 }
 else digitalWrite(6,LOW); //LED1,OFF
 Serial.println(sensorReading);
 delay(100);

 sensorReading=analogRead(1); //LDR2
 if (sensorReading<800)
 {
   digitalWrite(7,HIGH); //LED2,ON
 }
 else digitalWrite(7,LOW); //LED2,OFF
 Serial.println(sensorReading);
 delay(100);
}



Sorry if this is very simple and i'm missing a trick.

Eventually I would like to create a simple 3x4 grid of LEDs that turn on and off when you move your hand across them (to create a shadow of your hand in LEDs). I would have a bigger grid but I'm guessing I will need to use & connect two arduino boards as there is only 6 analog pins on my UNO and I don't want to over complicate it at this stage.

Also any advice on whether I would be better off using photodiodes instead of LDRs would be much appreciated.

Thank you in advance!

Tom

aarg

Welcome to the Forum. Please read the two posts

How to use this forum - please read.
and
Read this before posting a programming question ...

at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look
Code: [Select]
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. So before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like
Code: [Select]
digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose.
Code: [Select]
digitalWrite(hornRelayPin, LOW) does. You can do that by declaring
Code: [Select]
const byte hornRelayPin = 3; before setup() in your program.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

T_Wild

Thank you aarg.

I hope the changes help. Sorry I missed using the code tags.

I'm unsure how to use
Code: [Select]
const byte hornRelayPin = 3; obviously i'm guessing the wording changes slightly (i.e. hornReplayPin to LEDPin?) but I hope that it will at least make sense and somebody may be able to help.

Thanks,

Tom

sterretje

You can make use of functions. The below code makes the assumption that an analog input is associated with a LED by a constant offset of 2. So analog0 is associated with a LED on pin2, analog1 is associated with a LED on pin3 and so on

Code: [Select]

void loop()
{
  for(int ledcnt=0;ledcnt<6;ledcnt++)
  {
    // switch LED on or of based on light level
    doLed(ledcnt);
    // wait a while
    delay(100);
  }
}

// read value of given LDR and switch associated LED on or off
void doLed(int iLedNumber)
{
  // read voltage
  int sensorReading = analogRead(iLedNumber)

  // logic to switch on or off
  if (sensorReading<800)
  {
    digitalWrite(iLedNumber + 2, HIGH);
  }
  else
  {
    digitalWrite(iLedNumber + 2, LOW);
  }
}


You can also make use of a lookup table; that way you will get rid of the hard relation (offset 2)


Code: [Select]

// lookup table to associated analog inputs 0 to 5 with pins connected to LEDs
int lookup[6];
void setup()
{
  // associated analog inputs 0 to 5 with LEDs on specified pin numbers
  lookup[0] = 13;
  lookup[1] = 11;
  lookup[2] = 10;
  lookup[3] = 9;
  lookup[4] = 12;
  lookup[5] = 3;
}

void loop()
{
  for(int ledcnt=0;ledcnt<6;ledcnt++)
  {
    // switch LED on or of based on light level
    doLed(ledcnt);
    // wait a while
    delay(100);
  }
}

// read value of given LDR and switch associated LED on or off
void doLed(int iLedNumber)
{
  // read voltage
  int sensorReading = analogRead(iLedNumber);

  // logic to switch on or off
  if (sensorReading<800)
  {
    digitalWrite(lookup[iLedNumber], HIGH);
  }
  else
  {
    digitalWrite(lookup[iLedNumber], LOW);
  }
}


In the last step, you can replace the pin numbers by sensible names.

Code: [Select]

#define LDR1 0  // LDR on analog 0
#define LDR2 1  // LDR on analog 1
#define LDR3 2  // LDR on analog 2
#define LDR4 3  // LDR on analog 3
#define LDR5 4  // LDR on analog 4
#define LDR6 4  // LDR on analog 5

#define LED1 13 // LED on digital 13
#define LED2 11 // LED on digital 11
#define LED3 10 // LED on digital 10
#define LED4 9  // LED on digital 9
#define LED5 12 // LED on digital 12
#define LED6 3  // LED on digital 3

int lookup[6];
void setup()
{
  // associated analog inputs 0 to 5 with LEDs on specified pin numbers
  lookup[LDR1] = LED1;
  lookup[LDR2] = LED2;
  lookup[LDR3] = LED3;
  lookup[LDR4] = LED4;
  lookup[LDR5] = LED5;
  lookup[LDR6] = LED6;
}

The loop stays the same.

Note: you need to complete the setup with the usual stuff like setting pinmode and so on.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

aarg

Code: [Select]

void setup()
{
  // associated analog inputs 0 to 5 with LEDs on specified pin numbers
  lookup[0] = 13;
  lookup[1] = 11;
  lookup[2] = 10;
  lookup[3] = 9;
  lookup[4] = 12;
  lookup[5] = 3;
}


That is not a good way to do initialization. Do it like this:

Code: [Select]

  // associated analog inputs 0 to 5 with LEDs on specified pin numbers
const uint8_t  lookup[] = {13, 11, 10, 9, 12, 3};

void setup()
{
}
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

sterretje

That is not a good way to do initialization. Do it like this:
Thanks for that, I have been looking into the 'const' but wasn't quite there yet.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

T_Wild

Hi,

Thank you for the replies.

I' struggling to define how your code works (which is does - so thank you) and hope that somebody could help with a my question below.

Code: [Select]
const int nLEDs=2;
int ledPins[nLEDs]={2,3};

const int nSensors=2;
int LDRs[nSensors]={(0),(1)};

int sensorReading; //analog pin reading
int ledPinX;

void setup()
{
  Serial.begin(9600);
  //Declares all LEDs as OUTPUT
  for (int i=0; i<nLEDs; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
}

void loop()
{
  for (int i=0; i<nSensors; i++)
  {
    sensorReading=analogRead(LDRs[i]);
    Serial.println(sensorReading);
 
    //ledPinX=
   
    if (sensorReading<850);
    {
      digitalWrite(ledPinX,HIGH);
    }
    else digitalWrite(ledPinX,LOW);
    delay(50);
  }
}


(To simplify it down, the arrays each have two LEDs/LDR which later I will increase to 12.)

I started by declaring all LEDs as OUTPUT, but struggling to work out how I can associate each LED with a specific LDR. I have a loop which reads each sensor to determine whether the associated LED should be on of off (below):

Code: [Select]
void loop()
{
  for (int i=0; i<nSensors; i++)
  {
    sensorReading=analogRead(LDRs[i]);
    Serial.println(sensorReading);


and in this loop I want to tell the arduino to turn on a specific LED associated with the LDR. I feel at this point I need to declare the LED number (i've used ledPinX to represent this) but i'm not sure how to do this.

Sorry, i'm very new to arduino, but I hope somebody may be able to help.

Thanks.

D6equj5

I must say I found this topic very interesting in that it (the members) showed differing ways to resolve the problem. I am, as it happens, in the middle of a project which itself is partly using ldrs (covered by hands etc) to choose different menu options. I often read back through old topics when looking for solutions to difficulties with getting an Arduino to do exactly as I want and as with this topic, it doesn't always need the defining post showing the full solution to help me along the way.
Thank you forum.
Thank you
D6

aarg

You're welcome. But be aware that responses to a question may address issues that some other person wouldn't need help with. I think it's because there are more ways to be wrong than there are to be right. :)
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

D6equj5

You're welcome. But be aware that responses to a question may address issues that some other person wouldn't need help with. I think it's because there are more ways to be wrong than there are to be right. :)
Erm yes, he said rather hesitantly, so if a topic does not cover an area a reader needs help with, they won't necessarily find it useful.  How very astute of you. Accordingly, if a post does indeed cover an area a reader finds interesting, then it may be of interest to them. I see what you mean. Time for a cuppa and bacon sarnie I feel.
Thank you
D6

aarg

Indeed, if there was more awareness of the search capability at the upper left of the forum page, it would be great. Otherwise it is a hit and miss effort to get an answer just by reading today's posts. It might even make a lot of posts unnecessary. But I think a magnifying glass is just too subtle for many people to grasp. ;)
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

D6equj5

Indeed, if there was more awareness of the search capability at the upper left of the forum page, it would be great. Otherwise it is a hit and miss effort to get an answer just by reading today's posts. It might even make a lot of posts unnecessary. But I think a magnifying glass is just too subtle for many people to grasp. ;)

Yes exactly - search first for any resemblance to your query and then, if nothing suitable is found, post your question.
Thank you
D6

D6equj5

Anyway - back to the topic:
I have been looking at interrupts and need my void loop() to be interrupted if an analog pin has a certain data value (through an ldr).
Could anyone advise as to the best way to do this - the documentation seems to tell me interrupts only work on digital pins - or have I got that wrong?
Thank you
D6

sterretje

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

D6equj5

Why do you think you need an interrupt?
Ah good question sterrtje - simple answer, my project needs two ldr's to be able to interrupt the void loop() independently and individually whilst allowing everything else within the loop() to continue up until the moment of interrupt then instantly branch off to do some specific function and then return to the loop().
All the best and all that
D6
Thank you
D6

Go Up