Hello guys,
Got a program that's not working ![]()
What I'm trying to do is read a couple of rotary encoders, than calculate if the encoder was turned CW (+1) or CCW (-1).
When reading the encoders I put the values in a Byte, this gives me (when turning encoder 1 CW):
01111111
11111111
00111111
11111111
10111111
11111111
11111111
11111111
01111111
11111111
I decided to ignore the 11111111, so that means:
Sequence CW rotation:
00
10
11
01
Sequence CCW rotation:
00
01
11
10
So what is not working:
the values are all over the place. when turning CW I get -1 or 1, the same when I turn in the opposite direction.
the 4th value of int encoders1[3] is 280?? Well, that's what's on the display...
Here is the code:
//define where your pins are
int latchPin = 8;
int dataPin = 11;
int clockPin = 12;
//Define variables to hold the data
//for each shift register.
byte switchVar1;
byte OLDswitchVar1;
int encoders1[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, world!");
 //define pin modes
 pinMode(latchPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(dataPin, INPUT);
}
//------------------------------------------------end setup
////------------------------------------------------start main loop
void loop() {
 digitalWrite(latchPin, LOW);
 delayMicroseconds(5);
 digitalWrite(latchPin, HIGH);
 switchVar1 = shiftIn(dataPin, clockPin);
 Â
  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 ((OLDa == 1) && (a == 0))
   {
    if (b == 1)
    {
     encoders1[k]=1;
    }
    else
    {
     encoders1[k]=-1;
    }
   bitWrite(OLDswitchVar1, i, a);
   bitWrite(OLDswitchVar1, j, b);
   }
   if ((OLDa == 0) && (a == 0))
   {
    if (b == 0)
    {
     encoders1[k]=1;
    }
    else
    {
     encoders1[k]=-1;
    }
   bitWrite(OLDswitchVar1, i, a);
   bitWrite(OLDswitchVar1, j, b);
   }
   if ((a == 1) && (b == 0))
   {
    if (OLDb == 0)
    {
     encoders1[k]=1;
    }
    else
    {
     encoders1[k]=-1;
    }
   bitWrite(OLDswitchVar1, i, a);
   bitWrite(OLDswitchVar1, j, b);
   }
   }
  lcd.setCursor(0, 1);
  lcd.print(encoders1[0]);
  lcd.setCursor(4, 1);
  lcd.print(encoders1[1]);
  lcd.setCursor(8, 1);
  lcd.print(encoders1[2]);
  lcd.setCursor(12, 1);
  lcd.print(encoders1[3]);
  delay (50);
}
//------------------------------------------------end main loop
////// ----------------------------------------start shiftIn function
byte shiftIn(int myDataPin, int myClockPin) {
 int i;
 int temp = 0;
 int pinState;
 byte myDataIn = 0;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, INPUT);
 for (i=7; i>=0; i--)
 {
  digitalWrite(myClockPin, 0);
  delayMicroseconds(2);
  temp = digitalRead(myDataPin);
  if (temp) {
   pinState = 1;
   myDataIn = myDataIn | (1 << i);
  }
  digitalWrite(myClockPin, 1);
 }
 return myDataIn;
}
//------------------------------------------------end shiftIn function
Eventually, I want to use this in a bigger sketch that will send sysex strings to a JD-990. This is just the start to get the interface (knobs, pots and LCD) working.
Any pointers are appreciated!
Cheers,
ToAd