Help! 74hc595 efficient pushbuttons inputs.

Hi,

i was using cd4021 for reading 8 pushbuttons and displaying the outputs through 8 leds/relays connected to 74hc595 through SPI. I am using cat8 of length ~20m between the arduino and ic circuit(wires are used for SCK, MISO, MOSI and 2 latches, 5V and ground). Due to the length of the wire, cd4021 is not responding to the buttons but i am able to drive the 74hc595 ic according to my will. I removed the cd4021 ics and i'm 1 74hc595 for inputs and 1 for outputs.

here is my circuit for the input..

|Q0------1n4007----button----|
|Q1------1n4007----button----|
595 |Q2------1n4007----button----|
|Q3------1n4007----button----|
|Q4------1n4007----button----|
|Q5------1n4007----button----|
|Q6------1n4007----button----|
|Q7------1n4007----button----|-----------pin 2 of arduino uno
ground----10k-----|

another ic is connected to this one..
I'm getting a flicker in the leds and the buttons are working with a delay... Kindly help me out... Here is my code

#include <SPI.h>
byte Input, Output=0, Check=1;
int j;
int s=0;
void setup(){
  pinMode(7, OUTPUT);
  pinMode(10, OUTPUT);//latch
  pinMode(2, INPUT);//Input from buttons


  

SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();
  digitalWrite(10, LOW);
  SPI.transfer(255);
  SPI.transfer(0);
  digitalWrite(10, HIGH);
  digitalWrite(7,LOW);
  Serial.begin(9600);
 attachInterrupt(0, pin_read, RISING); 
  
}//setup

void loop(){

}//loop


  void pin_read(){
    s=!s;
    digitalWrite(7,s);
    for(j=0; j<50; j++)
    delayMicroseconds(1000);

    Check=1;
    for(j=0; j<8; j++){
  
  SPI.transfer(Check);
  SPI.transfer(Output);
  digitalWrite(10, HIGH);
  
  delayMicroseconds(500);
  digitalWrite(10, LOW);
  if(digitalRead(2)==HIGH){
      if(bitRead(Output, j)==1)
      bitWrite(Output, j, 0);
      else
      bitWrite(Output, j, 1);
  }//dig check
  
  Check = Check<<1;
    }//j
    
  SPI.transfer(255);
  SPI.transfer(Output);
  digitalWrite(10, HIGH);
  digitalWrite(10, LOW);
 
 while(digitalRead(2)==HIGH){}

}

Swap these 2 lines:

SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();

Check your SPI.transfers,
SS LOW
transfer
SS HIGH

they do not look consistent. HC595 outputs are updated when SRCK goes from LOW to HIGH.

thank for reply.. it late night.. i'll try in the morning and reply

CrossRoads:
Swap these 2 lines:

SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.begin();

Check your SPI.transfers,
SS LOW
transfer
SS HIGH

they do not look consistent. HC595 outputs are updated when SRCK goes from LOW to HIGH.

hey thanks.. It works!!!

did as you asked and also lowered the SPI clock to div64 due to long wire... Still, works flawlessly!!!