Pages: [1]   Go Down
Author Topic: Multiple || in for loop?  (Read 296 times)
0 Members and 1 Guest are viewing this topic.
UK - Manchester
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hello arduinians smiley

I got my self an UNO on saturday and I've had some fun learning a shed loads about electronics and this is comming from an IT technician as it is my day job smiley

I'm still in the beginner stages so I'm working with simple circuits still, in my current project I have 2 RGB leds and I wish to light up 2 colors on each so figured I'd modify my first bliking white code just blink ( or at least at this stage keep them on for testing.
my code was this
 
Code:
for(int i = 0; i < 6; i++)

and I changed it to this so I could basiclly ignore the green pins in my project.
 

Code:
void loop() {
  //I want 0 2 3 & 5
  for(int i = 0; i < 6 && i == 0 || 2 || 3 || 5; i++)
 {
   digitalWrite(ledpins[i], 0);
 }
}

I'm using 6 pins on PWM ( which I will later play with for fading) which goes to each color so my var ledpins is an array of 6 refering to each pins and 0 2 3 5 are red and blue.

I uploaded this code but it just light all of them up as if it ignores my OR operators but I'm guessing I'm using || wrong. I've tried a serach but havnt found any similar for loop

Thanks

RageRiot
Logged

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

Code:
i == 0 || 2 || 3 || 5
i == 0 is a comparison but
2
3
5
all evaluate to true, because they are all non-zero.
So, you expression evaluates to
Code:
i == 0 || true || true || true
and true OR x is always true.

A much simpler approach is to have an array for each of the pins controlling each colour:
Code:
const int greenPin [] = {0, 2, 3, 5};
and then a simple for loop to step through.
« Last Edit: February 28, 2012, 04:50:20 pm by AWOL » 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.

UK - Manchester
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i = 0; i < 6 && i == 0 || i == 2 || i == 3 || i == 5; i++)

I tried this also assuming that was my mistake but it still the same result
Logged

UK - Manchester
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ahh I see
Logged

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

I think you've misunderstood what the condition in a for loop is there for.

It defines the condition for sustaining (or terminating) the loop - it's a binary proposition, and as soon as the condition is false, the loop terminates.
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.

UK - Manchester
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand that now. thank you smiley-grin
Logged

Pages: [1]   Go Up
Jump to: