Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 04:14:48 pm
ok thanks guys.  I finally understand how this works haha.  You're all awesome!
2  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 02:58:48 am
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.

3  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 01:12:10 am
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?
4  Using Arduino / Programming Questions / Re: syntax question on: February 09, 2013, 12:05:44 am
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
5  Using Arduino / Programming Questions / syntax question on: February 08, 2013, 11:37:01 pm
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!
Pages: [1]