First attempt at looping through (not full) matrix led strip...

using the 'bit walk' approach.. can you turn on leds consecutively?..leaving the previous one on?..

or can you just "SHIFT" the next led to be on.. and the previous one off?

(I cant get the bit walking/shifting approach to work.. either way using | or &..etc..)

however.. I have another question..

one of these codes works.. the other does not..

can you tell me why?

WORKS:

#include <SPI.h>  

//-----[this is SPI hardware wiring pinout only]-----//
/* 
 Arduino pin D13 is connected to the DataIn (MAX7221 pin:13)
 Arduino pin D11 is connected to the CLK (MAX7221 pin:1)
 Arduino pin D10 is connected to LOAD (MAX7221 pin:12)
 */

// define MAX72xx default register addresses
#define DECODE_MODE 0x09 // Code B Decode Digits 0-7: 0xFF / No-Decode Mode: 0x00
#define INTENSITY_ADDRESS 0x0A // 0x07 to start, half intensity. valid from 0x00 (min) to 0x0F (max)
#define SCANLIMIT_ADDRESS 0x0B // All 8 digits on = 0xFF?? FF? Datasheet says 0x07?
#define SHUTDOWN_ADDRESS 0x0C  // Normal operation = 0x01 / Shutdown = 0x00 (powers up in shutdown mode)
#define DISPLAYTEST_ADDRESS 0x0F // 0x01 = all lights on full, 0x00 = normal ops

//---[define register addresses ['register/digit x' represent 1 section/grouping of 8 leds]---//
#define register1 0x01 // digit 0
#define register2 0x02 // digit 1
#define register3 0x03 // digit 2
#define register4 0x04 // digit 3
#define register5 0x05 // digit 4
#define register6 0x06 // digit 5
#define register7 0x07 // digit 6
#define register8 0x08 // digit 7

//--[define the SS pin]--//
#define SS 10 //hardware slave select pin on Arduino Due.
//--[deifne custom val]--//
#define totalGroups 8
//--[deifne custom val]--//
#define totalLeds 60


//create var to hold delay tim
int pause = 35;
//create var to hold current led count
int ledCounter = 0;

// create array to hold the register addresses
//byte registerAddress[] = {0,1,2,3,4,5,6,7};
byte registerAddress[] = {
  register1, register2, register3, register4, register5, register6, register7, register8};

// initialize array with some data/pattern
//1 on at a time
//byte registerData[] = {B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001}; 
//incrmentally on, 1, then 2, then 3...all staying light after
byte registerData[] = {
  B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 

//stay on pattern/array
byte ledIn[] = {B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 
byte ledOut[] = {B00000000,B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110}; 
//byte ledOut[] = {B11111110, B11111100, B11111000, B11110000, B11100000, B11000000, B10000000, B00000000}; 
//byte ledOut[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; 


void setup() {
  Serial.begin(9600);
  Serial.println("--SET UP CHIP--");
  //define 'SS' pin as output (needed for SPI....even if other devices use other pins as their SS/CS pins...correct?
  pinMode (SS, OUTPUT);
  // start the SPI library
  SPI.begin(); 

  //set MAX7221 defaults
  digitalWrite (SS, LOW);
  SPI.transfer (DECODE_MODE);  
  SPI.transfer (0x00);  // 0x00 - no-decode mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (INTENSITY_ADDRESS);  
  SPI.transfer (0x0F);  // 0x0F - max on/brightness
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SCANLIMIT_ADDRESS);  
  SPI.transfer (0x07);  // 0xFF or 0x07? 0x07 - all display digits on
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SHUTDOWN_ADDRESS);  
  SPI.transfer (0x01);  // 0x01 - normal operation mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer(DISPLAYTEST_ADDRESS);
  SPI.transfer(0x00);  // 0x00 - normal operation mode
  digitalWrite (SS, HIGH);

  //clear display
  for(int group=0; group<totalGroups; group++){    
    digitalWrite (SS,LOW);
    SPI.transfer (registerAddress[group]);  // register to write to
    SPI.transfer (B00000000);  // and value       
    digitalWrite (SS,HIGH);      
  }

}

void loop() {   
  //clear display
  //for(int group=0; group<totalGroups; group++){    
  //digitalWrite (SS,LOW);
  //SPI.transfer (registerAddress[group]);  // register to write to
  //SPI.transfer (B00000000);  // and value       
  //digitalWrite (SS,HIGH);
  //}

  //going up:
  for (int group = 0; group <7; group = group+1){   // first 7 rows
    for (int led=0; led<8; led++){
      digitalWrite (SS,LOW);
      SPI.transfer (registerAddress[group]);  // register to write to
      SPI.transfer (ledIn[led]);  // and value       
      digitalWrite (SS,HIGH);

      delay(pause);
      //Serial.println(led);
    }
  }
  //last group of 4
  int group = 7;  // 8th row
  for (int led=0; led<3; led++){
    digitalWrite (SS,LOW);
    SPI.transfer (registerAddress[group]);  // register to write to
    SPI.transfer (ledIn[led]);  // and value       
    digitalWrite (SS,HIGH);

    delay(pause);
    //Serial.println(led);
  }



  //going down:
  //last group of 4
  group = 7;  // 8th row
  for (int led=3; led>=0; led--){
    digitalWrite (SS,LOW);
    SPI.transfer (registerAddress[group]);  // register to write to
    SPI.transfer (ledOut[led]);  // and value       
    digitalWrite (SS,HIGH);

    delay(pause);
    //Serial.println(led);
  }


  for (int group = 6; group>=0; group--){   // first 7 rows
    for (int led=7; led>=0; led--){
      digitalWrite (SS,LOW);
      SPI.transfer (registerAddress[group]);  // register to write to
      SPI.transfer (ledOut[led]);  // and value       
      digitalWrite (SS,HIGH);

      delay(pause);
      //Serial.println(led);
    }
  }



  /*
  // now backwards, starting with 8th row
   for (byte led = B11110000; led = 0; led=led>>1){ // walk the other way until clear
   digitalWrite (SS,LOW);
   SPI.transfer (registerAddress[group]);  // register to write to
   SPI.transfer (ledIn[led]);  // and value       
   digitalWrite (SS,HIGH);
   delay(pause);
   }
   for (int group = 6; group = 0; group = group -1){
   for (byte led = B11111111; led = 0; led=led>>1){ // walk the other way until clear
   digitalWrite (SS,LOW);
   SPI.transfer (registerAddress[group]);  // register to write to
   SPI.transfer (ledIn[led]);  // and value       
   digitalWrite (SS,HIGH);
   delay(pause);
   }
   }
   */
}

and

DOESNT WORK:

#include <SPI.h>  

//-----[this is SPI hardware wiring pinout only]-----//
/* 
 Arduino pin D13 is connected to the DataIn (MAX7221 pin:13)
 Arduino pin D11 is connected to the CLK (MAX7221 pin:1)
 Arduino pin D10 is connected to LOAD (MAX7221 pin:12)
 */

// define MAX72xx default register addresses
#define DECODE_MODE 0x09 // Code B Decode Digits 0-7: 0xFF / No-Decode Mode: 0x00
#define INTENSITY_ADDRESS 0x0A // 0x07 to start, half intensity. valid from 0x00 (min) to 0x0F (max)
#define SCANLIMIT_ADDRESS 0x0B // All 8 digits on = 0xFF?? FF? Datasheet says 0x07?
#define SHUTDOWN_ADDRESS 0x0C  // Normal operation = 0x01 / Shutdown = 0x00 (powers up in shutdown mode)
#define DISPLAYTEST_ADDRESS 0x0F // 0x01 = all lights on full, 0x00 = normal ops

//---[define register addresses ['register/digit x' represent 1 section/grouping of 8 leds]---//
#define register1 0x01 // digit 0
#define register2 0x02 // digit 1
#define register3 0x03 // digit 2
#define register4 0x04 // digit 3
#define register5 0x05 // digit 4
#define register6 0x06 // digit 5
#define register7 0x07 // digit 6
#define register8 0x08 // digit 7

//--[define the SS pin]--//
#define SS 10 //hardware slave select pin on Arduino Due.
//--[deifne custom val]--//
#define totalGroups 8
//--[deifne custom val]--//
#define totalLeds 60


//create var to hold delay tim
int pause = 35;
//create var to hold current led count
int ledCounter = 0;

// create array to hold the register addresses
//byte registerAddress[] = {0,1,2,3,4,5,6,7};
byte registerAddress[] = {
  register1, register2, register3, register4, register5, register6, register7, register8};

// initialize array with some data/pattern
//1 on at a time
//byte registerData[] = {B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001}; 
//incrmentally on, 1, then 2, then 3...all staying light after
byte registerData[] = {
  B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 

//stay on pattern/array
byte ledIn[] = {B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 
byte ledOut[] = {B00000000,B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110}; 
//byte ledOut[] = {B11111110, B11111100, B11111000, B11110000, B11100000, B11000000, B10000000, B00000000}; 
//byte ledOut[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; 


void setup() {
  Serial.begin(9600);
  Serial.println("--SET UP CHIP--");
  //define 'SS' pin as output (needed for SPI....even if other devices use other pins as their SS/CS pins...correct?
  pinMode (SS, OUTPUT);
  // start the SPI library
  SPI.begin(); 

  //set MAX7221 defaults
  digitalWrite (SS, LOW);
  SPI.transfer (DECODE_MODE);  
  SPI.transfer (0x00);  // 0x00 - no-decode mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (INTENSITY_ADDRESS);  
  SPI.transfer (0x0F);  // 0x0F - max on/brightness
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SCANLIMIT_ADDRESS);  
  SPI.transfer (0x07);  // 0xFF or 0x07? 0x07 - all display digits on
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SHUTDOWN_ADDRESS);  
  SPI.transfer (0x01);  // 0x01 - normal operation mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer(DISPLAYTEST_ADDRESS);
  SPI.transfer(0x00);  // 0x00 - normal operation mode
  digitalWrite (SS, HIGH);

  //clear display
  for(int group=0; group<totalGroups; group++){    
    digitalWrite (SS,LOW);
    SPI.transfer (registerAddress[group]);  // register to write to
    SPI.transfer (B00000000);  // and value       
    digitalWrite (SS,HIGH);      
  }

}

void loop() {   
  //clear display
  //for(int group=0; group<totalGroups; group++){    
  //digitalWrite (SS,LOW);
  //SPI.transfer (registerAddress[group]);  // register to write to
  //SPI.transfer (B00000000);  // and value       
  //digitalWrite (SS,HIGH);
  //}

  //count up
  for(int group=0; group<totalGroups; group++){
    for(int led=0; led<8; led++){
      digitalWrite (SS,LOW);
      SPI.transfer (registerAddress[group]);  // register to write to
      SPI.transfer (ledIn[led]);  // and value       
      digitalWrite (SS,HIGH);
      delay(pause);

      ledCounter++;
      
      //count down:
      if(ledCounter >= 60){
        //count backwards
        Serial.println("");
        Serial.println("--REVERSE!--");
        Serial.println("");

        for(int group2=7; group2>=0; group2--){
          if(group2 >= 7){
            for (int led=3; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause); 
              ledCounter--;         
            }
          }
          else{            
            for (int led=7; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause);
              ledCounter--;
            }
          }
          
        }
      }


    }
  }
}

In the 2nd one:
for (int led=3; led>=0; led--){
&
for (int led=7; led>=0; led--){

these will count down from B00000011, B00000010, B0000001, B00000000
& B00000111, B00000110, B00000101, B00000100, B00000011, B00000010, B0000001, B00000000

so you are not getting the bit shifting that you are expecting.
Check the reference page on ++, -- vs << and >>
You also led as int's (16 bit variable) vs byte's (8 bit variable), which could make a difference.

??

Im using 'led' as the number of the array index to grab my pre-determined pattern/layout?... no?

Im not passing 'led' in my SPI.transfer();... but instead ledOut[led] ??

or doesnt it work like that?

(and Im starting with the LAST index and reversing through it)

(although.. I could just have my array in the right order.. and loop through it normally.... but that was hind-sight..and not really a 'problem')

byte ledOut[] = {B00000000,B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110};

for (int led=7; led>=0; led--){
digitalWrite (SS,LOW);
SPI.transfer (registerAddress[group]); // register to write to
SPI.transfer (ledOut[led]); // and value
digitalWrite (SS,HIGH);
}

and I like the 'bit' shifting/walking.... because you can use the 'length' as an auto stop sorta..

'but'..

I dont want:

B10000000
B01000000
B00100000

I want:

B10000000
B11000000
B11100000

etc..etc.

B11111111

then reverse...

B11111110
B11111100
B11111000

etc..etc...

and I couldnt figure out that was done in your examples...

ok, that's just a different manipulation vs shifting a 1 back & forth.
for 00000001, 00000011, 00000111, 00001111,
I think you can start with 1 and shift<1 and add 1:
for (byte led = 1; led=255; led = led<1 +1){

for 11111111, 11111110, 11111100, 11111000
start with 11111111 and shift left 1
for (byte led = 255; led = 0; led+led<1){

for 10000000, 11000000, 11100000, 11110000
start with 10000000, shift right and add 100000000
for (byte led = B10000000; led = 255; led = led>1 + B10000000){

ok.. thanks.. I'll try that out tonight!.

but Im still curious as to other questions?

for now.. forget about the bit shifting...

I think ultimately...when I want to do random/odd patterns..etc.. building an array would be helpful to use in certain functions.

so what is wrong with passing an array index value?

or more to the point.. what is wrong with MY code that attempts to do that?

that has nothing to do with bit shifting.. I am hard coding bit values in the array..and feeding that to the 'loops'???

thanks

this line?
SPI.transfer (ledIn[led]);

I don't know, seems like it should work.
It compiles okay? What happens when it does not work?

it works like that.. but I thought you were seeing it or something?

as you were posting different code/approaches then those two code samples..

this has NOTHING to do with bit shifting.. (outside of hard coding the values in an array)

the working code.. uses array.. but also uses your approach of breaking up the 'groups' from 0-6 and then 7

where as the other approach uses a conditional check..

otherwise I cant see a difference?

#include <SPI.h>  

//-----[this is SPI hardware wiring pinout only]-----//
/* 
 Arduino pin D13 is connected to the DataIn (MAX7221 pin:13)
 Arduino pin D11 is connected to the CLK (MAX7221 pin:1)
 Arduino pin D10 is connected to LOAD (MAX7221 pin:12)
 */

// define MAX72xx default register addresses
#define DECODE_MODE 0x09 // Code B Decode Digits 0-7: 0xFF / No-Decode Mode: 0x00
#define INTENSITY_ADDRESS 0x0A // 0x07 to start, half intensity. valid from 0x00 (min) to 0x0F (max)
#define SCANLIMIT_ADDRESS 0x0B // All 8 digits on = 0xFF?? FF? Datasheet says 0x07?
#define SHUTDOWN_ADDRESS 0x0C  // Normal operation = 0x01 / Shutdown = 0x00 (powers up in shutdown mode)
#define DISPLAYTEST_ADDRESS 0x0F // 0x01 = all lights on full, 0x00 = normal ops

//---[define register addresses ['register/digit x' represent 1 section/grouping of 8 leds]---//
#define register1 0x01 // digit 0
#define register2 0x02 // digit 1
#define register3 0x03 // digit 2
#define register4 0x04 // digit 3
#define register5 0x05 // digit 4
#define register6 0x06 // digit 5
#define register7 0x07 // digit 6
#define register8 0x08 // digit 7

//--[define the SS pin]--//
#define SS 10 //hardware slave select pin on Arduino Due.
//--[deifne custom val]--//
#define totalGroups 8
//--[deifne custom val]--//
#define totalLeds 60


//create var to hold delay tim
int pause = 35;
//create var to hold current led count
int ledCounter = 0;

// create array to hold the register addresses
//byte registerAddress[] = {0,1,2,3,4,5,6,7};
byte registerAddress[] = {
  register1, register2, register3, register4, register5, register6, register7, register8};

// initialize array with some data/pattern
//1 on at a time
//byte registerData[] = {B10000000,B01000000,B00100000,B00010000,B00001000,B00000100,B00000010,B00000001}; 
//incrmentally on, 1, then 2, then 3...all staying light after
//byte registerData[] = {B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 

//stay on pattern/array
byte ledIn[] = {B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110,B11111111}; 
byte ledOut[] = {B00000000,B10000000,B11000000,B11100000,B11110000,B11111000,B11111100,B11111110}; 
//byte ledOut[] = {B11111110, B11111100, B11111000, B11110000, B11100000, B11000000, B10000000, B00000000}; 
//byte ledOut[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; 

void setup() {
  Serial.begin(9600);
  Serial.println("--SET UP CHIP--");
  //define 'SS' pin as output (needed for SPI....even if other devices use other pins as their SS/CS pins...correct?
  pinMode (SS, OUTPUT);
  // start the SPI library
  SPI.begin(); 

  //set MAX7221 defaults
  digitalWrite (SS, LOW);
  SPI.transfer (DECODE_MODE);  
  SPI.transfer (0x00);  // 0x00 - no-decode mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (INTENSITY_ADDRESS);  
  SPI.transfer (0x0F);  // 0x0F - max on/brightness
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SCANLIMIT_ADDRESS);  
  SPI.transfer (0x07);  // 0xFF or 0x07? 0x07 - all display digits on
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer (SHUTDOWN_ADDRESS);  
  SPI.transfer (0x01);  // 0x01 - normal operation mode
  digitalWrite (SS, HIGH);

  digitalWrite (SS, LOW);
  SPI.transfer(DISPLAYTEST_ADDRESS);
  SPI.transfer(0x00);  // 0x00 - normal operation mode
  digitalWrite (SS, HIGH);

  //clear display
  for(int group=0; group<totalGroups; group++){    
    digitalWrite (SS,LOW);
    SPI.transfer (registerAddress[group]);  // register to write to
    SPI.transfer (B00000000);  // and value       
    digitalWrite (SS,HIGH);      
  }

}

void loop() {   
  //clear display
  //for(int group=0; group<totalGroups; group++){    
  //digitalWrite (SS,LOW);
  //SPI.transfer (registerAddress[group]);  // register to write to
  //SPI.transfer (B00000000);  // and value       
  //digitalWrite (SS,HIGH);
  //}

  //count up
  for(int group=0; group<totalGroups; group++){
    for(int led=0; led<8; led++){
      digitalWrite (SS,LOW);
      SPI.transfer (registerAddress[group]);  // register to write to
      SPI.transfer (ledIn[led]);  // and value       
      digitalWrite (SS,HIGH);
      delay(pause);

      ledCounter++;
      
      //count down:
      if(ledCounter >= 60){
        //count backwards
        Serial.println("");
        Serial.println("--REVERSE!--");
        Serial.println("");

        for(int group2=7; group2>=0; group2--){
          Serial.print("GROUP 2: ");
          Serial.println(group2);
          if(group2 >= 7){
            Serial.println(" -LAST GROUP- ");
            for (int led=3; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group2]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause); 
              ledCounter--;         
            }
          }else{  
            Serial.print(" -NOT LAST GROUP- "); 
            Serial.println(group2);       
            for (int led=7; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group2]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause);
              ledCounter--;
            }
          }
        }
      }
    }
  }
}

it seems to light UP fine.....but the reverse it tweaks in the last group of 4 then...then goes on to do odd animation/pattern?

Ah, so you just have a logic problem them. That's different. Let me consider it some.

haha...

yeah..

I only had a 'logic' problem from the beginning... :wink:

however.. learning a bit of how the SPI protocol works didnt hurt! :slight_smile:

Still havent tried your suggestion for bit walking to make all 1's instead of just 'shifting' the 1..

Im a bit under the weather.. :frowning:

I think the }s in void loop were a litte off, so the count up code was not separated from the count down code. Try it like this.

void loop() {   
  //clear display
  //for(int group=0; group<totalGroups; group++){    
  //digitalWrite (SS,LOW);
  //SPI.transfer (registerAddress[group]);  // register to write to
  //SPI.transfer (B00000000);  // and value       
  //digitalWrite (SS,HIGH);
  //}

  //count up
  for(int group=0; group<totalGroups; group++){
    for(int led=0; led<8; led++){
      digitalWrite (SS,LOW);
      SPI.transfer (registerAddress[group]);  // register to write to
      SPI.transfer (ledIn[led]);  // and value       
      digitalWrite (SS,HIGH);
      delay(pause);
      if ( (group == 7) && (led == 3) ){ led = 8;}

    }
  }
        for(int group2=7; group2>=0; group2--){
          Serial.print("GROUP 2: ");
          Serial.println(group2);
          if(group2 >= 7){
            Serial.println(" -LAST GROUP- ");
            for (int led=3; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group2]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause); 
              ledCounter--;         
            }
          }
          else{  
            Serial.print(" -NOT LAST GROUP- "); 
            Serial.println(group2);       
            for (int led=7; led>=0; led--){
              digitalWrite (SS,LOW);
              SPI.transfer (registerAddress[group2]);  // register to write to
              SPI.transfer (ledOut[led]);  // and value       
              digitalWrite (SS,HIGH);

              delay(pause);
              ledCounter--;
            }
          }
        }
      }