Improved Blink with multi-set not working using bitwise & and |, v1.0.6

I am a very experienced programmer, and am at my wits end!

I have improved the basic blink program so that it can easily set multiple LEDs at the same time. I only have leds 9-13 built, and they all work with the digitalWrite function.

However, leds(leds_none) works, all other values result in all LEDs on. It’s like the bitwise ‘&’ function is performing a boolean and, but the documentation is quite clear that & is bitwise and && is boolean.

Any help appreciated!

/*
Blink improved - for leds 9-13
Simon Carter
*/

int d = 1000; /* this is the delay */

unsigned int led1 = 1; //may change to use B000001 format later
unsigned int led2 = 2;
unsigned int led3 = 4;
unsigned int led4 = 8;
unsigned int led5 = 16;
unsigned int led6 = 32;
unsigned int led7 = 64;
unsigned int led8 = 128;
unsigned int led9 = 256;
unsigned int led10= 512;
unsigned int led11= 1024;
unsigned int led12= 2048;
unsigned int led13= 4096;

int leds_all = led1 | led2 | led3 | led9 | led10 | led11 | led12 | led13;
int leds_none = 0;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}

void leds( unsigned int which )
{
unsigned int test = 1;
for ( int i = 1; i <= 13; i++ )
{
if (which & test != 0)
digitalWrite(i, HIGH);
else
digitalWrite(i, LOW);

test = test << 1; //multiply by 2 to test the next bit
}

delay(d);
}

// the loop function runs over and over again forever
void loop() {
leds( leds_all );
leds( leds_none );

leds( led9 );
leds( led10 );
leds( led11 );
leds( led12 );
leds( led13 );
leds( led12 );
leds( led11 );
leds( led10 );
leds( led9 | led10 );
leds( led9 | led10 | led11 );
leds( led9 | led10 | led11 | led12 );
leds( led9 | led10 | led11 | led12 | led13 );
}

void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
}

And the other pins?

Any reason for not using arrays?

if (which & test != 0)

Operator precedence?

if (which & test != 0)
      digitalWrite(i, HIGH);
    else
      digitalWrite(i, LOW);

If you simplified this to just digitalWrite(i, which & test); you'd be OK.

datamystic:
I am a very experienced programmer, and am at my wits end!

    test = test << 1;  //multiply by 2 to test the next bit

}
 
  delay(d);
}

Never a wise thing to start a post with.

Experienced programmers don’t use delay() they use the technique in the Blink Without Delay example sketch which is also illustrated in the demo several things at a time.

And just to forestall any smart replies, I am NOT a very experienced programmer.

…R

Thanks guys - operator precedence got me!

if ((which & test) != 0)

I am coding with my 11 year-old, this is project #2 and she is learning really well. Delays can go out the window once we add the keypad and door sensor, but she has to learn the reason why first.

You could have showed your 11 year-old how to add debug prints to help locate the problem.

Please remember to use code tags when posting code.