I'm experimenting with ShiftPWM and a circuit board with 4 7seg LEDs and 4 shift registers. It works great so far except after its been left on for a while the last shift register will freeze with all the pins high. Nothing about the program should cause this and even a reset doesnt fix it. It doesnt go back to normal until I cycle power to the shift registers. Here is my code:
#include <SPI.h>
#include "hsv2rgb.h"
//Data pin is MOSI (atmega168/328: pin 11. Mega: 51)
//Clock pin is SCK (atmega168/328: pin 13. Mega: 52)
const int ShiftPWM_latchPin=8;
const bool ShiftPWM_invertOutputs = 0; // if invertOutputs is 1, outputs will be active low. Usefull for common anode RGB led's.
#include <ShiftPWM.h> // include ShiftPWM.h after setting the pins!
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 75;
int numRegisters = 4;
int struck[32];
long lastMilli = 0;
long lastSec = 0;
int secWait = 0;
int tick = 0;
void setup() {
pinMode(ShiftPWM_latchPin, OUTPUT);
SPI.setBitOrder(LSBFIRST);
// SPI_CLOCK_DIV2 is only a tiny bit faster in sending out the last byte.
// SPI transfer and calculations overlap for the other bytes.
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.begin();
Serial.begin(9600);
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
ShiftPWM.SetAmountOfRegisters(numRegisters);
ShiftPWM.Start(pwmFrequency,maxBrightness);
ShiftPWM.SetAll(0);
randomSeed(analogRead(0));
secWait = random(5,10);
}
void loop(){
//fade pins
for(int i = 0;i<32;i++){
if (struck[i]!=0){
struck[i] = struck[i] - 1;
if(struck[i]<0) struck[i] = 0;
ShiftPWM.SetOne(i,struck[i]);
}
//cycle pins to 255 one by one
if(millis() >= lastMilli + map(analogRead(0),0,1023,5,1000)){
if(++tick==32) tick=0;
if(struck[tick]<64) struck[tick] = 64;
lastMilli = millis();
}
//randomly set all the pins to 255 between 5 and 10 seconds
if(millis() >= lastSec + secWait*1000){
for(int i = 0;i<32;i++){
struck[i] = 255;
}
lastSec = millis();
secWait = random(5,10);
}
}
delay(5);
}
What could be my issue?