oldArray[] = new Array[] question

Guys,

 for (int n = 0; n <= 3; n++)
  {
    int c = OLDvalues1[n];
    int d = encoders1[n];
    values1[n] = c + d;
    OLDvalues1[n] = values1[n];
  }

So Everything looks OK untill I do OLDvalues1[n] = values1[n].
Actually what I want to do at the end of the loop is making the array’s the same.
Should I do that inside the for loop or somewhere else? And how?

So how does one do that for arrays

FYI: In encoders1 are values +1 and -1 indicating that an encoder was turned CW or CCW.
values1 contains values between 0 and 127, same length as the encoders1 (4 pots)

Cheers,
ToAd

So Everything looks OK untill I do OLDvalues1[n] = values1[n].

And, what does it look like afterwards? You really need to post ALL of your code, to provide context for that snippet.

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

"I need to get the read out on LCD working so I can check if the my calculations are of any good." Or add Serial.begin(9600); and Serial.write(variable to be checked); and use the serial monitor.

Got anything against comments? Its a little tough to follow as is.

for (int n = 0; n <= 3; n++)
should rather read
for (int n = 0; n < 3; n++)

OLDvalues1[3] is a different variable and not part of your OLDvalues1 array

This was not visible in your first post :wink:

michael_x:
for (int n = 0; n <= 3; n++)
should rather read
for (int n = 0; n < 3; n++)

Yes that for condition should be changed, to correct it for the size of the array. Your arrays are size 3 but including the equals sign will cause the loop to run 4 times.

michael_x:
OLDvalues1[3] is a different variable and not part of your OLDvalues1 array

What do you mean? OLDvalues1[3] doesnt exist, it’s out of the range of the array.

Wazzled:
for (int n = 0; n <= 3; n++)
should rather read
for (int n = 0; n < 3; n++)

OK, I thought an array was zero indexed, so I was indeed reading it 4 times.
SO i should change

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

into

int encoders1[4];
int OLDencoders1[4];

So I should do the same for the lcd code:

lcd.setCursor(0, 0);
  lcd.print(values1[0]);

into

lcd.setCursor(0, 0);
  lcd.print(values1[1]);

PS the lcd is working, I get to see the values, that’s when I got rid of the serial print…

Thanks!!
ToAd

EDIT:
OK. I tried it and it’s looking much better. Still not working as expected but that is due to the calculations being wrong.
Still wondering about the lcd code though… the lcd.print(values1[0] shows something.
Cheers,
ToAd

Arrays are 0 indexed, but when you define their size you are saying how many elements you want.

Your array size should be the number of encoders your using.

if you have

int numbers[3];

that array will store 3 numbers, with the first being at index 0, second at index 1, third at index 2.

thebigear: OK, I thought an array was zero indexed, so I was indeed reading it 4 times.

It is zero indexed. When you declare an array like this:

int myArray[4];

You have access to the values:

myArray[0] myArray[1]; myArray[2]; myArray[3];

Thanks Wazzled & Arrch, that's clear now. Cheers, ToAd

What do you mean? OLDvalues1[3] doesnt exist, it's out of the range of the array.

You can modify OLDvalues1[3] even if it's out of the range of the array, however this will modify a different variable, which is allocated at the place in memory just after OLDValues1[2]. Better don't care which variable is affected, but fix the problem code)

michael_x: You can modify OLDvalues1[3] even if it's out of the range of the array, however this will modify a different variable, which is allocated at the place in memory just after OLDValues1[2].

The joys of C# (wouldn't let you do that). :)