Go Down

Topic: LDR code and breadboard set up (Read 1 time) previous topic - next topic


Hi, im trying to get my project to work with multiple LDR's, i can get it working with one from the information from http://www.ladyada.net/learn/sensors/cds.html but m having trouble getting it to work with multiple ones (i need it with 5). I have modified the code so it is now

/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

For more information see www.ladyada.net/learn/sensors/cds.html */

int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellPin1 = 1; // the cell and 10K pulldown are connected to a1
int photocellPin2 = 2; // the cell and 10K pulldown are connected to a2
int photocellPin3 = 3; // the cell and 10K pulldown are connected to a3
int photocellPin4 = 4; // the cell and 10K pulldown are connected to a4
int photocellPin5 = 5; // the cell and 10K pulldown are connected to a5
int photocellReading;     // the analog reading from the analog resistor divider

void setup(void) {
  // We'll send debugging information via the Serial monitor

void loop(void) {
  photocellReading = analogRead(photocellPin0);
  photocellReading = analogRead(photocellPin1);
  photocellReading = analogRead(photocellPin2); 
  photocellReading = analogRead(photocellPin3);
  photocellReading = analogRead(photocellPin4);
  photocellReading = analogRead(photocellPin5);

  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading

  // We'll have a few threshholds, qualitatively determined
  if (photocellReading < 10) {
    Serial.println(" - Dark");
  } else if (photocellReading < 200) {
    Serial.println(" - Dim");
  } else if (photocellReading < 500) {
    Serial.println(" - Light");
  } else if (photocellReading < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
This compiles fine but im not sure how to set up the components on the breadboard...any suggestions? Thanks!


You are overwriting photocellReading 5 times!  Only the sixth call to analogRead() is being used later in the loop() function.

A suggestion:  look up about arrays and indexing.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


What is it that you want to achieve with 6 sensors? If you can tell us this, we can help you with code that will do it.  You showed us what you did. We just don't know what you want to do.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Go Up