Pages: [1]   Go Down
Author Topic: Changing the if() statement to use five LEDs  (Read 1209 times)
0 Members and 1 Guest are viewing this topic.
Vancouver, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the code that I'm using looks like this:
Code:
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:
Code:
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!
Logged

-skeeter_mcbee-

Offline Offline
Edison Member
*
Karma: 65
Posts: 2498
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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;
 ? ?
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


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

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26178
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 65
Posts: 2498
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26178
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 65
Posts: 2498
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK
The 'Arduino Reference' example is:
Code:
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
}
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26178
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 65
Posts: 2498
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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().

The 'Arduino Reference' example is:
Code:
int analogPin = 0;  //   They actually used [b]3[/b], same concept
                         //    They didn't use  int analogPin = A3; or  int analogPin = A0;

Look at the function analogRead():

Code:
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.


Just didn't think I've seen analogRead with an alias declared as int = A0 (or Ax) before.
You might be interested in the section on "Pin Mapping":
http://arduino.cc/en/Tutorial/AnalogInputPins
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26178
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 65
Posts: 2498
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:

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

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: