Changing the if() statement to use five LEDs

the code that I'm using looks like this:

const int analogPin = A0;
const int ledPin = 13;
const int threshold = 400;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(analogPin);
  
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  Serial.println (analogValue);
}

I would like to change it so that it works with multiple LEDs in parallell. I've gotten this far:

const int analogPin = A0;
const int firstPin = 2;
const int secondPin = 3;
const int thirdPin = 4;
const int fourthPin = 5;
const int fifthPin = 6;
const int firstthreshold = 205;
const int secondthreshold = 410;
const int thirdthreshold = 615;
const int fourththreshold = 820;
const int fifththreshold = 1023;

void setup() {
  pinMode(firstPin, OUTPUT);
  pinMode(secondPin, OUTPUT);
  pinMode(thirdPin, OUTPUT);
  pinMode(fourthPin, OUTPUT);
  pinMode(fifthPin, OUTPUT);
  Serial.begin(9600);
}

int analogValue = analogRead(

And I can't figure out how to get passed that analogRead() event. Any and all help will be helpful. Thanks!

I can't figure out how to get passed that analogRead() event.

const int analogPin = A0;
? ?
Is that legal in that form?
A0 - A5 correspond to 14-20
Should it then be:
const int analogPin = 14;
? ?

You'll have to explain a little bit more about what you want to do. What are the conditions you want to define for each of those LEDs?

[quote author=Runaway Pancake link=topic=102535.msg769208#msg769208 date=1335121557]
const int analogPin = A0;
Is that legal in that form?
A0 - A5 correspond to 14-20
Should it then be:
const int analogPin = 14;[/quote]

Yes, that's legal syntax. There is no difference by using "A0" or "14". So I don't understand what you are questioning.

@runawaypancak,
A0 is 14.
What point are you trying to make?

That the analogPin value is then an integer "A0"?
In past I just use analogValue = analogRead(A0);
Does the IDE differentiate/understand that using "A0" or "14" is all the same?

The IDE has nothing to do with it; the preprocessor simply replaces A0 with the literal 14.

OK
The 'Arduino Reference' example is:

int analogPin = 0;  //   They actually used [b]3[/b], same concept
                         //    They didn't use  int analogPin = A3; or  int analogPin = A0;

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value
}

I still don't see what point you are trying to make; in this context (analogRead), zero and fourteen are completely interchangeable.

Well, I'm not trying to make a point or be a pain - I'm trying to get it (too).
An integer = 0 and A0? Likewise, an integer = 3 and A3?
Just didn't think I've seen analogRead with an alias declared as int = A0 (or Ax) before.
Thought that might be what's holding up the analogRead that our friend can't get past.

Well, if the pre-processor figures it all out in context then fine.

It's a combination of the pre-processor (Ax alias) AND the actual function analogRead().

Look at the function analogRead():

int analogRead(uint8_t pin)
{
//... removed for clarity
        if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif

The very first thing analogRead() does convert values like 14 (for the ATmega328) to 0. So if you pass A0 (which is replaced with 14 by the preprocessor), 14, or 0; analogRead() treats them all the same.

You might be interested in the section on "Pin Mapping":

RunawayPancake, No, you're still missing the point.
The preprocessor has no knowledge of any context of analogRead, it blindly replaces A0 with 14.
However, in the particular context of analogRead, zero and fourteen are equivalent.
Obviously, in any other context, the equivalence would not hold, you could not, for instance, write "A0 == 0" and expect it to be true.

James C4S,
So, it gets sorted - 0/A0/14 associated with analogRead yield the same result.
OK.

skeeter_mc,
Place a Serial.print or two in your 'sketch' to Debug your situation, to find out where it's getting stuck (if it's getting stuck at the analogRead, as you suggest.)

@AWOL,

If possible, It might be appropriate to split this topic. I think the discussion on what A0 is good to keep, but it might distract the original poster.

Are you trying to control five LEDs based on five different thresholds on the single analog input pin?

In that case, the cheap and cheerful approach is just to take this code from the original sketch and put five copies in your new sketch:

  if (analogValue > threshold)
  {
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }

In the first copy you replace threshold with firstthreshold and ledPin with firstPin .
In the second copy you replace threshold with secondthreshold and ledPin with secondPin.
You can probably figure the rest out ...

For a more elegent solution you would put your thresholds and LED pin numbers in arrays and put in a loop that applies each threshold.