Third Learning Exercice Help!

Hi!

I decided to learn Arduino. First, I did the LED exercise. Second, I wire up a photocell according to that tutorial How to use photocells, LDRs, CdS cells, photoresistors! and I used that code:

/* 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 photocellPin = 0;     // the cell and 10K pulldown are connected to a0
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(photocellPin);  
  
  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);
}

Now for my third exercise, I’d like to wire up 6 photocells but I really don’t understand how to write the code for 6. How does it works, what should I do to add 6 photocells to the code?

Thanks!
Micheline

  1. Create a function
    http://www.arduino.cc/en/Reference/FunctionDeclaration

Make it have the pin number as an argument.

  1. Call the function using values from an array
    http://arduino.cc/en/Reference/Array

[edit]This is what it could look like:

/* 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 How to use photocells, LDRs, CdS cells, photoresistors! */

const byte NUMBER_OF_PHOTOCELLS = 5;
byte photocellPins[NUMBER_OF_PHOTOCELLS] = {0,1,2,3,4}; // the cell and 10K pulldown are connected to a0
int photocellReadings[NUMBER_OF_PHOTOCELLS] = {0}; // 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){
for (byte currentIndex=0; currentIndex<NUMBER_OF_PHOTOCELLS; currentIndex++){
readPhotocell(currentIndex);
}
}

void readPhotocell(byte index) {
photocellReadings[index] = analogRead(photocellPins[index]);

Serial.print("Analog reading = ");
Serial.print(photocellReadings[index]); // the raw analog reading

// We’ll have a few threshholds, qualitatively determined
if (photocellReadings[index] < 10) {
Serial.println(" - Dark");
} else if (photocellReadings[index] < 200) {
Serial.println(" - Dim");
} else if (photocellReadings[index] < 500) {
Serial.println(" - Light");
} else if (photocellReadings[index] < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
delay(1000);
}

[/edit]

Oh! Thanks a lot AlphaBeta! I really appreciated it! ... but I'm a bit discouraged, it looks so hard ...

Using functions actually makes things much easier. You only have to write the code once, and you can use it in a variety of ways. For instance, I have a Buzz(int freq, int length) function - you pass it the frequency and the length, and it buzzes as you specify. So you can just call the function a bunch of times, instead of writing out those 10+ lines of code each time you want to buzz.

If you're worried about the programming aspects, look up some tutorials on coding in C/C++ - it's essentially the same for Arduino.