multi rotary encoders via shiftIn


hello guys,

got program that's not working ;-)
what i'm trying read couple of rotary encoders, calculate if encoder turned cw (+1) or ccw (-1).
when reading encoders put values in byte, gives me (when turning encoder 1 cw):
01111111
11111111
00111111
11111111
10111111
11111111
11111111
11111111
01111111
11111111
i decided ignore 11111111, means:
sequence cw rotation:
00
10
11
01
sequence ccw rotation:
00
01
11
10

so not working:
the values on place. when turning cw -1 or 1, same when turn in opposite direction.
the 4th value of int encoders1[3] 280?? well, that's what's on display...

here code:
code: [select]

//define pins are
int latchpin = 8;
int datapin = 11;
int clockpin = 12;

//define variables hold data
//for each shift register.
byte switchvar1;
byte oldswitchvar1;

int encoders1[3];

// include library code:
#include <liquidcrystal.h>

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

////------------------------------------------------start setup
void setup() {
  // set lcd's number of columns , rows:
  lcd.begin(16, 2);
  // print message 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);
   
    (int k = 0; k <= 3; k++)
    {
      int j = (2*k)+1;
      int = j-1;
      int = 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);

  (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, want use in bigger sketch send sysex strings jd-990. start interface (knobs, pots , lcd) working.
any pointers appreciated!
cheers,
toad



Arduino Forum > Using Arduino > Programming Questions > multi rotary encoders via shiftIn


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial