Go Down

Topic: syntax question (Read 698 times) previous topic - next topic

Hello I've been looking into making an LED cube and I ran across I line of code and I'm curious if you can explain a part of it.

digitalWrite( Pins[index] , 7 & (1 << 0) );

What is the meaning of adding the "7 & (1<<ledcol)"?  I don't understand what this comparison does.
ledcol goes from 0 to 8.

Thank you!

johncc

It's not a comparision, << is a bit shift operator.  And & is the bit "and" operator. http://playground.arduino.cc/Code/BitMath

I can't quite get the meaning of the one line of code you posted though.  Perhaps if you posted more of the surrounding code?

Cheers,
John

Code: [Select]
// display pattern in table until DisplayTime is zero (then repeat)
void loop()
{
  // declare variables
  byte PatternBuf[ PLANESIZE ];      // saves current pattern from PatternTable
  int PatternIdx;
  byte DisplayTime;        // time*100ms to display pattern
  unsigned long EndTime;
  int plane;      // loop counter for cube refresh
  int patbufidx;   // indexes which byte from pattern buffer
  int ledrow;    // counts LEDs in refresh loop
  int ledcol;    // counts LEDs in refresh loop
  int ledpin;    // counts LEDs in refresh loop

  // Initialize PatternIdx to beginning of pattern table
  PatternIdx = 0;
  // loop over entries in pattern table - while DisplayTime>0
  do {
    // read pattern from PROGMEM and save in array
    memcpy_P( PatternBuf, PatternTable+PatternIdx, PLANESIZE );
    PatternIdx += PLANESIZE;
    // read DisplayTime from PROGMEM and increment index
    DisplayTime = pgm_read_byte_near( PatternTable + PatternIdx++ );   
    // compute EndTime from current time (ms) and DisplayTime
    EndTime = millis() + ((unsigned long) DisplayTime) * TIMECONST;

    // loop while DisplayTime>0 and current time < EndTime
    while ( millis() < EndTime ) {
      patbufidx = 0;    // reset index counter to beginning of buffer
      // loop over planes
      for (plane=0; plane<CUBESIZE; plane++) {
        // turn previous plane off
        if (plane==0) {
          digitalWrite( PlanePin[CUBESIZE-1], HIGH );
        } else {
          digitalWrite( PlanePin[plane-1], HIGH );
        }

        // load current plane pattern data into ports
        ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
          patbufidx++;
        }

        // turn current plane on
        digitalWrite( PlanePin[plane], LOW );
        // delay PLANETIME us
        delayMicroseconds( PLANETIME );
      }    // for plane
    }    // while <EndTime
  } while (DisplayTime > 0);        // read patterns until time=0 which signals end

johncc


Code: [Select]
    // load current plane pattern data into ports
        ledpin = 0;
        for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
          for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
            digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
          }
          patbufidx++;
        }


The digitalWrite here is going through each bit of the current location in the PatternBuff to decide whether to turn the corresponding LED on  (1) or off (0).

The (1 << ledcol) is the "mask" which would specify which bit in PatternBuf[patbufidx] to look at.

HTH?
John

so I'm getting numbers like B111 and B100 and my cube is 3x3x3 so wouldnt it need more bits to represent all 9 led's?

johncc

#5
Feb 09, 2013, 07:15 am Last Edit: Feb 09, 2013, 07:19 am by johncc Reason: 1

so I'm getting numbers like B111 and B100 and my cube is 3x3x3 so wouldnt it need more bits to represent all 9 led's?


Which 9 leds?

Sorry, let me put it differently.  ledcol only varies from 0 to 2.  There are only 3 "columns"...

#6
Feb 09, 2013, 08:58 am Last Edit: Feb 09, 2013, 09:13 am by mchammer9111 Reason: 1
ok I'm confused about the bit shifting.  So I traced 9 iterations of the loop and I don't see whats happening.

Code: [Select]

       // load current plane pattern data into ports
       ledpin = 0;
       for (ledrow=0; ledrow<CUBESIZE; ledrow++) {
         for (ledcol=0; ledcol<CUBESIZE; ledcol++) {
           digitalWrite( LEDPin[ledpin++], PatternBuf[patbufidx] & (1 << ledcol) );
         }
        }


Here's where I'm confused.
B0011 & (1<<0) => B011 & 1
B0011 & (1<<1) => B011 & 10
B0011 & (1<<2) => B011 &100

How do I know when the pin is in the high/low state?  I'm assuming B000 & 1 would be in the low state.  I guess I don't understand the AND operator.


Nick Gammon

If you digitalWrite a non-zero value, the pin will be on.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

UKHeliBob

Are you sure about that last result ?
Code: [Select]
B0011 & (1<<2)=B100
The result should be B000

You know when a particular bit is high because the result of the bitwise AND is not zero
Run this to see how
Code: [Select]
byte test = B1010;

void setup()
{
  Serial.begin(9600);
Serial.println(test,BIN);
  for (int a = 0;a <= 3;a++)
  {
    int x = test & (1 << a);
    Serial.print(a);
    Serial.print("\t");
    Serial.print(x ,BIN);
    Serial.print("\tbit ");
    Serial.print(a);

    if (x == 0)
    {
      Serial.println("\tis not set");       
    }
    else
    {
      Serial.println("\tis set");
    }
  }
}

void loop() {}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ok thanks guys.  I finally understand how this works haha.  You're all awesome!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy