Using binary numbers for bit shifting (SOLVED).

Hello. I am doing my first project on the Arduino. I have some decent experience in programming c++/Java, however i never did it for arduino. And one important note i never used bitwise operations in C. I created this short program as a test but its not working as it i intended it to. The idea is that i have 8 shift registers (74HC595) and i wanted to create some code that will let me test if everything is set up well. The issue is that just the first register out of the 8 displays data using my function. I assume that i made something wrong in the bitwise operations but even after looking in the internet i cannot see the problem, so here i am.

int clk = 2;
int latch= 3;
int data1 = 4;
int data2 = 5;
int data3 = 6;
int data4 = 7;
int data5 = 8;
int data6 = 9;
int data7 = 10;
int data8 = 11;

byte val1 = B00000001;
byte val2 = B00000010;
byte val3 = B00000100;
byte val4 = B00001000;
byte val5 = B00010000;
byte val6 = B00100000;
byte val7 = B01000000;
byte val8 = B10000000;

void setup(){
  for(int i = 2; i < 12; i++){
    pinMode(i, OUTPUT);
  }
  digitalWrite(clk, LOW);
  digitalWrite(latch, LOW);
  reset();
}

void loop() {
  //digitalWrite(5, HIGH);
  delay(500);
  shift(val1);
  delay(500);
  //reset();
  shift(val2);
  delay(500);
  shift(val3);
  delay(500);
  shift(val4);
  delay(500);
  shift(val5);
  delay(500);
  shift(val6);
  delay(500);
  shift(val7);
  delay(500);
  shift(val8);
  delay(500);
}

//------------------------------------------------

void shift(byte values){
  byte val = values;
  for(int i = 4; i < 12; i++){
    digitalWrite(4, val & 1);
    val = val >> 1;
  }
  digitalWrite(clk, HIGH);
  digitalWrite(clk, LOW);
  show();
}

void reset(){
  for(int i = 4; i < 12; i++){
    digitalWrite(i, LOW);
  }
  digitalWrite(clk, HIGH);
  digitalWrite(clk, LOW);
}

void show(){
  digitalWrite(latch, HIGH);
  digitalWrite(latch, LOW);
}

From what i checked so far the registers are hooked up correctly and they work (the commented out lines were for that purpose). When i run this program the first line which is meant to display B00000001 which means only the first register should light up works fine but the other 7 bytes do not work. I assume that my right shift in the shift() method does not properly right shift the values. I cant see whats the issue in my code :confused:

NOTE: Im using only the first output of the registers and each has a LED with resistor attached, so thats how i check the output data.

Nevermind i found the error!!!!! Oh God i feel so stupid. My shift() function had a 4 in the for loop instead of an i, i was basically writing to the same register. Sorry for the trouble lol.

CLOSE TOPIC please

CLOSE TOPIC please

You can do that by editing the title of the thread with the addition of (Solved).

oh good to know, thank you