invalid conversion from "int*" to "int" - array question

Guys,

I have a function that has 2 bytes and an array as input parameters and should return an array.
But I keep getting above mentioned error.

Below the program. What it does is this:
it reads some rotary encoders via a shiftIn function. encoders connected to a 74HC156 multuplexer.
than the myPotCalcFunction : it calculates if a rotary encoder has moved, calculates if it went CW or CCW, calculate the real value of the encoder (old value + 1 for CW and -1 for CCW).
the pot values are than displayed on a 16x2LCD.
I hope the coments will help :wink:
The error is in the myPotCalcFunction
When I just put the function into the void (loop) it kind of works :wink: (it detects the rotation of my pots OK, but does +3 instead of +1???)

PS this is the beginning of an UI interfacte template; reading rotary encoders and push buttons, than displaying values on the LCD. Next step is to do something with all these rotary encoders and push buttons :wink:

Any help appreciated.

//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[4];
int OLDencoders1[4];

//Define varaibles to hold data
//for values of rotary encoders
int valuesPot1[4];
int OLDvaluesPot1[4];

// 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
  //and calculate pot values  
  valuesPot1 = myPotCalcFunction(switchVar1, OLDswitchVar1, OLDvaluesPot1);  
 
  //display the values of the rotary encoders on the LCD
  lcd.clear();
  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



//-------------------------------------------------------------------------start pot calculation function

int* myPotCalcFunction(byte switchVar1, byte OLDswitchVar1, int OLDvaluesPot1)
 {
  if ((switchVar1 == 255)) //do nothing when swichVar = 11111111
    {
    }
    else
    {
    for (int k = 0; k < 4; k++)    //when "k=0"
    {
      int j = (2*k)+1;        //"j=1"
      int i = j-1;              // "i=0"
      int a = bitRead(switchVar1, i);  // a = bit 0
      int b = bitRead(switchVar1, j);   // b= bit 1
      int OLDa = bitRead(OLDswitchVar1, i); // read old a bit
      int OLDb = bitRead(OLDswitchVar1, j);  //read old b bit
      int c = OLDvaluesPot1[k];   //read old pot value
       if ((a == 1) && (b == 1))  //if a and b =0 do nothing
      {
      }         // next up some math to decide if pot is CW or CCW
      if ((a == 0) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a); //write new bit into 
       bitWrite(OLDswitchVar1, j, b); //old bit
       valuesPot1[k] = c - 1;  //calculate new pot value
       }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 0)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       valuesPot1[k] = c - 1;
      }
      if ((a == 0) && (b == 1) && (OLDa == 1) && (OLDb == 0)) 
      {
       encoders1[k]=-1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       valuesPot1[k] = c - 1;
      }
      if ((a == 0) && (b == 0) && (OLDa == 1) && (OLDb == 0)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       valuesPot1[k] = c + 1;
      }
      if ((a == 0) && (b == 1) && (OLDa == 0) && (OLDb == 0)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       valuesPot1[k] = c + 1;
      }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       encoders1[k]=1;
       bitWrite(OLDswitchVar1, i, a);
       bitWrite(OLDswitchVar1, j, b);
       valuesPot1[k] = c + 1;
      }
     else 
      {
        //if value changed and is not 11, than do as before??
        encoders1[k]=OLDencoders1[k]; //what about the pot values calculation?
      } 
      OLDvaluesPot1[k] = valuesPot1[k]; //make new old
      OLDencoders1[k] = encoders1[k];  //make new old
     }
  return valuesPot1; //for further use
  return OLDvaluesPot1; //for reuse in the loop
 }   
//----------------------------------------------end pot calculation function

Also, two consecutive returns rarely does what the programmer expects.

A standard way to return an array is to have it as one of the arguments. This avoids problems with allocating the return variable - if you create an array variable on the stack and then return it, there is no guarantee that the memory it was in will be untouched after the return. The alternative is to perform some sort of malloc operation, but this wouldn't be necessary unless the calling function doesn't know how long the result will be. So for example, you should never do this:

int * getarray( int val1, int val2 )
{
  int result[3];
  result[0] = val1 + val2;
  result[1] = val1 - val2;
  result[2] = val1 * val2;
  return result;
}

Since it is no longer valid to access the result variable after the return. Something like the following would be better:

void getarray( int val1, int val2, int result[3] )
{
  result[0] = val1 + val2;
  result[1] = val1 - val2;
  result[2] = val1 * val2;
}

void some_other_func()
{
  int array[3];

  getarray( 3, 5, array ); // Populate my local array variable
}
//Define varaibles to hold data
//for values of rotary encoders
int valuesPot1[4];
int OLDvaluesPot1[4];

...

int* myPotCalcFunction(byte switchVar1, byte OLDswitchVar1, int OLDvaluesPot1)
 {
...
  return valuesPot1; //for further use
  return OLDvaluesPot1; //for reuse in the loop
 }

Why are you returning them in the first place? They are global variables.

Guys,

Thanks for the answers so far, but I'm still not getting any result.
So here's the thing I want to do:
I want to calculate if an encoder has rotated and in which direction it rotated. When I know if the pot has rotated I calculate the real value of the pot.

The encoders are read via a shiftIn function. I end up with a byte. Now in total I will have 4 bytes as there are lot's of pot's involved. bit0&1, bit2&3, bit4&5 and bit6&7 represent an encoder.
So I want to create a function I can call for each byte and that results in an array, 4 long, contiaining the pot values.
For the direction detection I need the byte containing the encoder position and the previous byte containing the previous encoder position. I read the 2 bit's for old value and new value, compare them and decide of the encoder was turned CW or CCW. If CW I write a +1 value in the encoders array, els CCW is a -1. The real value of the pot is than calculated by taking the previous value of the pot and adding it with the encoders array. This I do for every set of bit's and thus the output of the function will be an array.

Next step, how I think, posibly completely wrong;
So I first read the rotary encoders giving me following bytes: switchVar1, switchVar2, switchVar3 and switchVar4
than I do
valuesPot1 = myPotCalcFunction(byte switchvar1, byte OLDswitchVar1, int OLDvaluesPot1);
valuesPot2 = myPotCalcFunction(byte switchvar2, byte OLDswitchVar2, int OLDvaluesPot2);
valuesPot3 = myPotCalcFunction(byte switchvar3, byte OLDswitchVar3, int OLDvaluesPot3);
valuesPot4 = myPotCalcFunction(byte switchvar4, byte OLDswitchVar4, int OLDvaluesPot4);
with valuesPot1 an array[4] and OLD valuesPot1 an array [4]

My function than looks like this:
int myPotCalcFunction (byte switchvar, byte OLD switchVar, intOLDvaluesPot)
{ for each couple of bit's bit0&1, bit2&3, bit4&5, bit6&7
calculate rotation, calculate potvalue;
return potvalue, oldencoders and old switchvar}

Now, I need to create the OLDswitchVar in this function as I only change the bit pairs when a movement is detected.

I hope this makes some sense. I'm really wondering if this is the way to go. Maybe there is a different approach for these kind of things?

Anyway;

is this also true if you want to have the function run for 4 times with different variables?
I tried removing the returns, but no change.

Cheers,
ToAd

thebigear:
My function than looks like this:
int myPotCalcFunction (byte switchvar, byte OLD switchVar, intOLDvaluesPot)
{ for each couple of bit's bit0&1, bit2&3, bit4&5, bit6&7
calculate rotation, calculate potvalue;
return potvalue, oldencoders and old switchvar}

You can't return multiple variables within a function. If you need a single function to change the value of multiple values, then you need to pass a reference to them, not simply the value.

Example:

// Variables
int valueToChange1=6, valueToChange2=5;
int valueToGet;

int doSomethingWithTwoVariables(int* valueToChange1, int* valueToChange2); // Function Prototype

void setup() {
  // Stuff
}

void loop() {
  // ... stuff
  valueToGet = doSomethingWithTwoVariables(&valueToChange1, &valueToChange2);
  // ... do more stuff
}

// Implementation
int doSomethingWithTwoVariables(int* valueToChange1, int* valueToChange2) {
  int valueToReturn;
  // Do something to change the arguments and return value
  return valueToReturn;
}

Arrch:
Example:

// Implementation

int doSomethingWithTwoVariables(int* valueToChange1, int* valueToChange2) {
 int valueToReturn;
 // Do something to change the arguments and return value
 return valueToReturn;
}

Can you give me an example of how you do the " Do something to change the arguments and return value" ?
Thanks,
ToAd

void doSomethingWithTwoVariables (const int a, const int b, int & addition, int & subtraction)
 {
 addition = a + b;
 subtraction = a - b;
 }

...

void loop ()
  {
  int answer1, answer2;

  doSomethingWithTwoVariables (21, 42, answer1, answer2);
  }

Ok, tried some more but no go yet.

How would the code look if answer1 and answer2 to are arrays and a and b would be bytes?

This is what I got now:

//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[4];
int OLDencoders1[4];

//Define varaibles to hold data
//for values of rotary encoders
int valuesPot1[4];
int OLDvaluesPot1[4];

int myPotCalcFunction(byte switchVar, byte OLDswitchVar, int valuesPot[], int  OLDvaluesPot[]);

// 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
  //and calculate pot values  
  myPotCalcFunction(switchVar1, OLDswitchVar1, valuesPot1, OLDvaluesPot1);  
 
  //display the values of the rotary encoders on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(valuesPot1[0]);
  lcd.setCursor(4, 0);
  lcd.print(valuesPot1[1]);
  lcd.setCursor(8, 0);
  lcd.print(valuesPot1[2]);
  lcd.setCursor(12, 0);
  lcd.print(valuesPot1[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


//-----------------------------------------------start pot calculation function
void myPotCalcFunction(byte switchVar, byte OLDswitchVar, int & valuesPot, int & OLDvaluesPot)
 {
  if ((switchVar == 255)) //do nothing when swichVar = 11111111
    {
    }
    else
    {
    for (int k = 0; k < 4; k++)    //when "k=0"
    {
      int j = (2*k)+1;        //"j=1"
      int i = j-1;              // "i=0"
      int a = bitRead(switchVar, i);  // a = bit 0
      int b = bitRead(switchVar, j);   // b= bit 1
      int OLDa = bitRead(OLDswitchVar, i); // read old a bit
      int OLDb = bitRead(OLDswitchVar, j);  //read old b bit
      int c = OLDvaluesPot[k];   //read old pot value
       if ((a == 1) && (b == 1))  //if a and b =0 do nothing
      {
      }         // next up some math to decide if pot is CW or CCW
      if ((a == 0) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       valuesPot[k] = c - 1;  //calculate new pot value
       }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 0)) 
      {
       valuesPot[k] = c - 1;
      }
      if ((a == 0) && (b == 1) && (OLDa == 1) && (OLDb == 0)) 
      {
       valuesPot[k] = c - 1;
      }
      if ((a == 0) && (b == 0) && (OLDa == 1) && (OLDb == 0)) 
      {
       valuesPot[k] = c + 1;
      }
      if ((a == 0) && (b == 1) && (OLDa == 0) && (OLDb == 0)) 
      {
       valuesPot[k] = c + 1;
      }
      if ((a == 1) && (b == 0) && (OLDa == 0) && (OLDb == 1)) 
      {
       valuesPot[k] = c + 1;
      }
     else 
      {
      } 
      OLDvaluesPot[k] = valuesPot[k]; //make new old
      }
      OLDswitchVar = switchVar; //make old new
 }   }



//----------------------------------------------end pot calculation function

I get following error now: invalid types 'int[int]' for array subscript.
The error indicates this line:
     int c = OLDvaluesPot[k];   //read old pot value

Cheers,
ToAd

edit: code edited inside the function.

Oldvaluespot is a reference to an int, not a int pointer, so you can't subscript it.

How would the code look if answer1 and answer2 to are arrays and a and b would be bytes?

Like this:

const int numberOfAnswers = 4;

void doSomethingWithTwoVariables (const int a, const int b, 
                                  int (& addition) [numberOfAnswers], 
                                  int (& subtraction) [numberOfAnswers])
 {
 addition [1] = a + b;
 subtraction [1] = a - b;
 }

void setup () {}

void loop ()
  {
  int answer1 [numberOfAnswers], answer2 [numberOfAnswers];

  doSomethingWithTwoVariables (21, 42, answer1, answer2);
  }