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
The error is in the myPotCalcFunction
When I just put the function into the void (loop) it kind of works (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
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