All right, so I noticed that the shift register attached to my Arduino isn’t quite working properly.
I tried running this test code:
void loop(){
for(int i = 0; i < 256; i++) {
Serial.println(i,DEC);
writeToRegister(i);
delay(1000);
}
}
// Uses SPI to clock in data, then latches to set LED on/off
void writeToRegister(byte data) {
// digitalWrite(LATCHPIN, LOW);
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, data);
digitalWrite(LATCHPIN, LOW);
digitalWrite(LATCHPIN, HIGH);
}
The code works fine for i==1 and i==2.
Starting from i==3, it begins to look like B00000101 and starts shifting to the right (eg. i==4 looks like B00001010, etc. bitshift << 1)
Then, it only seems to alternate between B10100101 and B01011010 starting from i=7 all the way up to 255.
SRCLR/serial clear (active low) is connected to 5V, output enable/G (active low) is grounded, SRCK/shift register clock is CLOCKPIN, RCK/register clock/latch is LATCHPIN, and SERIN/serial in is DATAPIN.
The other code doesn't touch the register in any way except calling the function writeToRegister(byte data), but here it is anyway (commented some stuff out to try and figure out what was wrong).
http://pastebin.com/mChUWqfb
I defined constants for the serial in, clock and latch pins, they're in the code.
And I did follow those tutorials, so I don't know what I'm doing wrong...
I hope it is not the tutorial that suggests putting a capacitor on the clock pin, that will over stress your arduino.
Have you got a supply decoupling capacitor on your shift register?
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
Yep remove that 1uF capacitor. Not only is it an arduino killer it will make the thing not work.
Instead connect that capacitor across ground and the supply of the chip.
No dice, same problem.
The 255-i bit doesn’t matter at all, actually, as it won’t fix this weird flipflopping during the debugging part of setup() - the part where it flips from displaying byte 165 to byte 90 and vice versa.
It’s at the end of setup(), and I posted about it in the original post.
The problem there is that although the program is sending bytes 0-255, after byte 2, it begins to start acting up, and byte 7 onwards has the flipflopping effect.
If you look at the bytes it is flipping between:-
165 = 0xA5 = b1010 0101
90 = 0x5A = b0101 1010
You will see that it is just doing one shift instead of clocking in 8 bits.
So if you start off by trying to shift in 255 (0xff) and that is it, do you see it appearing as 255 or as 0?
Yes, that appears to have fixed the problem! Thanks!
It also looks like I managed to wire four of the LEDs incorrectly, but that can be fixed with a software fix.