oldArray[] = new Array[] question

On request :wink:

Well, this is the start of something bigger, first steps and so...

I do a shiftIn of a bunch of encoders.
Than (still in the void loop, the "for" statement, haven't figured out how to make a function out of this) I'm deciding if the pot was turned up or down.
Than I calculate the value the pots represent.
Mind you that this code isn't working. I only read one of the 4 shift in loops I intend to use, eventually.

The output on the screen is xxx -1 -1 yyy with x and y a random value. only the -1's change, though not corresponding to the actual turning of the pot.

I need to get the read out on LCD working so I can check if the my calculations are of any good.

Any help apreciated!

So here the code (messy as this is actually my first real program)

//define where your pins are for shiftIn
int latchPin = 8;
int dataPin = 11;
int clockPin = 12;

//Define variables to hold the data 
//for each shift register.
byte switchVar1; 
byte OLDswitchVar1;

//Define varaibles to hold data
//for direction of rotary encoder (+1 or -1)
int encoders1[3];
int OLDencoders1[3];

//Define varaibles to hold data
//for values of rotary encoders
int values1[3];
int OLDvalues1[3];

// include the library code
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

////------------------------------------------------start setup
void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello,");
  //define pin modes
  delay(1000);
  lcd.clear();
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
}
//------------------------------------------------end setup

////------------------------------------------------start main loop
void loop() {
  //but inputs of 74HC156 in buffer
  digitalWrite(latchPin, LOW);
  delayMicroseconds(5);
  digitalWrite(latchPin, HIGH);
  //read inputs from 74HC156
  switchVar1 = shiftIn(dataPin, clockPin);
  //calculate CW or CCW movement of rotary encoders
  //should be placed in a seperate function as I need to read 4 74HC156's  
    for (int k = 0; k <= 3; k++) 
    {
      int j = (2*k)+1;
      int i = j-1;
      int a = bitRead(switchVar1, i);
      int b = bitRead(switchVar1, j);
      int OLDa = bitRead(OLDswitchVar1, i);
      int OLDb = bitRead(OLDswitchVar1, j);
      
      if ((a == 1) && (b == 1))
      {
      }   
      if ((a == 0) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 0)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
      }
      if ((a == 0) && (b == 1) && (OLDa == 1) && (OLDb == 0)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
      }
      if ((a == 0) && (b == 0) && (OLDa == 1) && (OLDb == 0)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
      }
      if ((a == 0) && (b == 1) && (OLDa == 0) && (OLDb == 0)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
      }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
      }
     else 
      {
        //if value changed and is not 11, than do as before
        encoders1[k]=OLDencoders1[k];
      } 
      OLDencoders1[k]=encoders1[k]; 
     }
  //calculate rotary encoder values. If previous value was 80 and direction is +1 than new value is 81    
  for (int n = 0; n <= 3; n++)
  {
    int c = OLDvalues1[n];
    int d = encoders1[n];
    values1[n] = c + d;
    OLDvalues1[n] = values1[n];
  } 
  //display the values of the rotary encoders on the LCD
  lcd.setCursor(0, 0);
  lcd.print(values1[0]);
  lcd.setCursor(4, 0);
  lcd.print(values1[1]);
  lcd.setCursor(8, 0);
  lcd.print(values1[2]);
  lcd.setCursor(12, 0);
  lcd.print(values1[3]);
}
//------------------------------------------------end main loop

////// ----------------------------------------start shiftIn function 74LS156

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  //read the byte
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    //set clk pin low and read the input
    delayMicroseconds(2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      myDataIn = myDataIn | (1 << i);
      //if pin is high write a 1 else a 0
    }
    //set clk pin low to start reading the next bit
    digitalWrite(myClockPin, 1);
  }
  return myDataIn;
}
//------------------------------------------------end shiftIn function

Cheers ToAd

edit: code with some comments