Here are the 4 code samples I've been trying. They are not my own creation, I've found them here and there for 74HC595 Arduino applications.philba wrote:Sigh. You went to some effort to post the video but didn't post the code or some sort of schematic diagram. The video doesn't tell us much at all. If the SRs are wired up correctly, then it's your code. You should post that. (use the code tag)
1st one.
Code: Select all
int SER_Pin = 8; //pin 14 on the 75HC595
int RCLK_Pin = 9; //pin 12 on the 75HC595
int SRCLK_Pin = 10; //pin 11 on the 75HC595
//How many of the shift registers - change this
#define number_of_74hc595s 3
//do not touch
#define numOfRegisterPins number_of_74hc595s * 8
boolean registers[numOfRegisterPins];
void setup(){
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
//reset all register pins
clearRegisters();
writeRegisters();
}
//set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i >= 0; i--){
registers[i] = LOW;
}
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins - 1; i >= 0; i--){
digitalWrite(SRCLK_Pin, LOW);
int val = registers[i];
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
registers[index] = value;
}
void loop(){
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, HIGH);
setRegisterPin(7, LOW);
writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
//Only call once after the values are set how you need.
}
Code: Select all
int latchPin = 9; //595 pin 12
int clockPin = 10; //595 pin 11
int dataPin = 8; //595 pin 14
//595 pin 16 connected to 5VDC
//595 pin 8 connected to GND
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
digitalWrite(latchPin, LOW); //When the latchPin goes from low to high, the data gets moved from the shift registers to the output pins
shiftOut(dataPin, clockPin, MSBFIRST, B10101010); //Using the shiftOut function to send binary 11111111 to light all LEDs. You can change from 1 to 0 to turn off.
digitalWrite(latchPin, HIGH); // Move the data from shift registers to output pins
/* delay(5000); // wait 5 seconds, then go back to the top of loop */
}
Code: Select all
// This pin gets sets low when I want the 595 to listen
const int pinCommLatch = 9;
// This pin is used by ShiftOut to toggle to say there's another bit to shift
const int pinClock = 10;
// This pin is used to pass the next bit
const int pinData = 8;
void setup()
{
pinMode (pinCommLatch, OUTPUT);
pinMode (pinClock, OUTPUT);
pinMode (pinData, OUTPUT);
//Serial.begin (56600);
} // setup
void sendSerialData2 (
byte value)
{
// Signal to the 595 to listen for data
digitalWrite (pinCommLatch, LOW);
digitalWrite (pinClock, LOW);
digitalWrite (pinData, LOW);
digitalWrite (pinData, HIGH);
digitalWrite (pinClock, HIGH);
// Signal to the 595 that I'm done sending
digitalWrite (pinCommLatch, HIGH);
} // sendSerialData2
void loop()
{
byte counter;
sendSerialData2 (counter);
} // loop
Code: Select all
// This pin gets sets low when I want the 595s to listen
const int g_pinCommLatch = 9;
// This pin is used by ShiftOut to toggle to say there's another bit to shift
const int g_pinClock = 10;
// This pin is used to pass the next bit
const int g_pinData = 8;
// Number of shift registers in use
const int g_registers = 3;
// Array of numbers to pass to shift registers
byte g_registerArray [g_registers];
void setup()
{
pinMode (g_pinCommLatch, OUTPUT);
pinMode (g_pinClock, OUTPUT);
pinMode (g_pinData, OUTPUT);
Serial.begin (56600);
} // setup
// Simple function to send serial data to one or more shift registers by iterating backwards through an array.
// Although g_registers exists, they may not all be being used, hence the input parameter.
void sendSerialData (
byte registerCount, // How many shift registers?
byte *pValueArray) // Array of bytes with LSByte in array [0]
{
// Signal to the 595s to listen for data
digitalWrite (g_pinCommLatch, LOW);
for (byte reg = registerCount; reg > 0; reg--)
{
byte value = pValueArray [reg - 1];
for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
{
digitalWrite (g_pinClock, LOW);
digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
digitalWrite (g_pinClock, HIGH);
}
}
// Signal to the 595s that I'm done sending
digitalWrite (g_pinCommLatch, HIGH);
} // sendSerialData
void loop()
{
g_registerArray[0]=255;
g_registerArray[1]=128;
g_registerArray[2]=4;
sendSerialData (g_registers, g_registerArray);
delay(1000);
} // loop