Pages: [1]   Go Down
Author Topic: syntax question  (Read 547 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Code:
// 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
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    // 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
Logged

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

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"...
« Last Edit: February 09, 2013, 01:19:49 am by johncc » Logged

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

ok I'm confused about the bit shifting.  So I traced 9 iterations of the loop and I don't see whats happening.

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

« Last Edit: February 09, 2013, 03:13:18 am by mchammer9111 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18807
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you digitalWrite a non-zero value, the pin will be on.
Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you sure about that last result ?
Code:
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:
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() {}
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

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

Pages: [1]   Go Up
Jump to: