Go Down

### Topic: Multiple || in for loop? (Read 570 times)previous topic - next topic

#### RageRiot

##### Feb 28, 2012, 10:43 pm

Hello arduinians

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

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.
Code: [Select]
`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: [Select]
`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

#### AWOL

#1
##### Feb 28, 2012, 10:46 pmLast Edit: Feb 28, 2012, 10:50 pm by AWOL Reason: 1
Code: [Select]
`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: [Select]
`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: [Select]
`const int greenPin [] = {0, 2, 3, 5};`
and then a simple for loop to step through.
"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.
I speak for myself, not Arduino.

#### RageRiot

#2
##### Feb 28, 2012, 10:51 pm
Code: [Select]
`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

#3
ahh I see

#### AWOL

#4
##### Feb 28, 2012, 10:53 pm
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.
"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.
I speak for myself, not Arduino.

#### RageRiot

#5
##### Feb 28, 2012, 10:57 pm
I understand that now. thank you

Go Up