SPI and TPIC6c595

Trying to test/learn SPI and shift registers via a simple “knight rider” LED sweep, but instead of sweeping, the LEDs light either one or two at a time from 0 to 7 in a loop.
8 LEDs driven by a TPIC6c595 / Uno.
Is my byte output correct?

#include <SPI.h>


const byte scanArray[] = {
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001
};

const int dataPin = 11;
const int latchPin = 13;
const int slaveSelectPin = 10; 

int index = 0;

void setup() {
  pinMode(dataPin, OUTPUT);  
  pinMode(latchPin, OUTPUT);
  pinMode (slaveSelectPin, OUTPUT);
  Serial.begin(9600);
  SPI.begin();
  digitalWrite(slaveSelectPin,LOW);
  
}

void loop() {
if (index<10){
    delay(200);
    digitalWrite (slaveSelectPin, LOW);
    SPI.transfer (scanArray[index]); 
    digitalWrite (slaveSelectPin, HIGH);
    index++;
  }
index=0;
}

You have a logic error. Index will always be 0 when tested by your if statement. You need a for or while statement instead of an if statement to cycle through all the values of that index should take on (0 to 14).

Agreed with Mrs. Cross Roads. Also, you could do this, which is closer I think to your intent:

oracledude:

void loop() {

if (index<15){
    delay(200);
    digitalWrite (slaveSelectPin, LOW);
    SPI.transfer (scanArray[index]);
    digitalWrite (slaveSelectPin, HIGH);
    index++;
  }
  else
    index=0;
}

I changed the top count to 14 here. You have 15 indices - 0 to 15 in your array.

Take these out - they interfere with SPI library.

const int dataPin = 11;
const int latchPin = 13;
pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);

Well, that's a pain in the butt! Darn forum stuck in a bunch of codes on me, and won't let me edit them?

And wiped out my simple for loop! Arrgh.
Which JoeN has, just not written as a for loop.

No dice. Updated the code and the LEDs still go one way only. Each iteration is different from the last though. one time through it lights 0,1/2,3/4,5/6,7. Next cycle its 0,1/2,2/3,3/4,5/6,6/7.

#include <SPI.h>


const byte scanArray[] = {
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001
};

//const int dataPin = 11;
//const int latchPin = 13;
const int slaveSelectPin = 10; 

int index = 0;

void setup() {
  // set the Pins as output:
//  pinMode(dataPin, OUTPUT);  
//  pinMode(latchPin, OUTPUT);
  pinMode (slaveSelectPin, OUTPUT);
  Serial.begin(9600);
  // initialize SPI:
  SPI.begin();
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  
}

void loop() {
//for (int i=0; i <= 10; i++){
  if (index<15){
    delay(200);
    digitalWrite (slaveSelectPin, LOW);
    SPI.transfer (scanArray[index]); 
    digitalWrite (slaveSelectPin, HIGH);
    index++;
  }
  else
    index=0;
}

Switched Data and Latch pins and it works. Thanks for the help!

Now onto ShiftPWM. :slight_smile:

oracledude:
Switched Data and Latch pins and it works. Thanks for the help!

Now onto ShiftPWM. :slight_smile:

Good, ya scared me there for a second. :slight_smile: