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?
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).
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;
}