Pages: [1]   Go Down
Author Topic: LDR code and breadboard set up  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
  Serial.begin(9600);   
}
 
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");
  }
  delay(1000);
}
 This compiles fine but im not sure how to set up the components on the breadboard...any suggestions? Thanks!
Logged

0
Offline Offline
Shannon Member
****
Karma: 160
Posts: 10418
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6889
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: