Ich hab das Problem mittlerweile gefunden. Es lag daran, dass das 7-Segment ständig überschrieben wurde. Eine richtige Klammer später, wurde der Output nur noch dann geschrieben, wenn es eine Veränderung in der Indexzahl gab. Dann war es aber ok.
Der nun funktionierende Code lautet:
#include <PinChangeInt.h> // necessary otherwise we get undefined reference errors.
#include <AdaEncoder.h>
#define a_PINA 2
#define a_PINB 3
const int latch = 8;
const int data = 11;
const int clock = 12;
int kanaele_einer[] = {6,91,79,102,109,125,7,127,103,63,6,91,79,102,109,125,7,127,103,63,6,91,79,102,109,125,7,127,103,63,6,91,79,102,109,125,7,127,103,63}; // right segment, one-spot?
int kanaele_zehner[] = {63,63,63,63,63,63,63,63,63,6,6,6,6,6,6,6,6,6,6,91,91,91,91,91,91,91,91,91,91,79,79,79,79,79,79,79,79,79,79,102}; // left segment, ten-spot?
int x = 0;
int8_t clicks=0;
char id=0;
void setup()
{
AdaEncoder::addEncoder('a', a_PINA, a_PINB);
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data, OUTPUT); // make the data pin an output
pinMode(latch, OUTPUT); // make the latch pin an output
Serial.begin(115200);
digitalWrite(latch, 0);
shiftOut(data, clock, MSBFIRST, kanaele_einer[x]);
Serial.println(kanaele_einer[x]);
shiftOut(data, clock, MSBFIRST, kanaele_zehner[x]);
Serial.println(kanaele_zehner[x]);
digitalWrite(latch, 1);
}
void loop()
{
encoder *thisEncoder;
thisEncoder=AdaEncoder::genie(&clicks, &id);
if (thisEncoder != NULL) {
thisEncoder=AdaEncoder::getFirstEncoder();
if (clicks > 0 ) {
x++;};
if (x==40) {
x=0;};
if (clicks < 0) {
x--;};
if (x==-1) {
x=39;};
// Anzeigenausgabe
digitalWrite(latch, 0);
shiftOut(data, clock, MSBFIRST, kanaele_einer[x]);
Serial.println(kanaele_einer[x]);
shiftOut(data, clock, MSBFIRST, kanaele_zehner[x]);
Serial.println(kanaele_zehner[x]);
digitalWrite(latch, 1);
}
}
Leider muss ich sagen, bounct das Ding noch gewaltig. Ich würde eigentlich gerne den Rotary Code von Buxter verwenden, bekomme ihn aber nicht richtig konfiguriert.
/* Rotary encoder handler for arduino.
*
* Copyright 2011 Ben Buxton. Licenced under the GNU GPL Version 3.
* Contact: bb@cactii.net
*
* Quick implementation of rotary encoder routine.
*
* More info: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
*
*/
// Half-step mode?
#define HALF_STEP
// Arduino pins the encoder is attached to. Attach the center to ground.
#define ROTARY_PIN1 2
#define ROTARY_PIN2 3
// define to enable weak pullups.
#define ENABLE_PULLUPS
#ifdef HALF_STEP
// Use the half-step state table (emits a code at 00 and 11)
const char ttable[6][4] = {
{0x3 , 0x2, 0x1, 0x0}, {0x83, 0x0, 0x1, 0x0},
{0x43, 0x2, 0x0, 0x0}, {0x3 , 0x5, 0x4, 0x0},
{0x3 , 0x3, 0x4, 0x40}, {0x3 , 0x5, 0x3, 0x80}
};
#else
// Use the full-step state table (emits a code at 00 only)
const char ttable[7][4] = {
{0x0, 0x2, 0x4, 0x0}, {0x3, 0x0, 0x1, 0x40},
{0x3, 0x2, 0x0, 0x0}, {0x3, 0x2, 0x1, 0x0},
{0x6, 0x0, 0x4, 0x0}, {0x6, 0x5, 0x0, 0x80},
{0x6, 0x5, 0x4, 0x0},
};
#endif
volatile char state = 0;
/* Call this once in setup(). */
void rotary_init() {
pinMode(ROTARY_PIN1, INPUT);
pinMode(ROTARY_PIN2, INPUT);
#ifdef ENABLE_PULLUPS
digitalWrite(ROTARY_PIN1, HIGH);
digitalWrite(ROTARY_PIN2, HIGH);
#endif
}
/* Read input pins and process for events. Call this either from a
* loop or an interrupt (eg pin change or timer).
*
* Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction.
*/
char rotary_process() {
char pinstate = (digitalRead(ROTARY_PIN2) << 1) | digitalRead(ROTARY_PIN1);
state = ttable[state & 0xf][pinstate];
return (state & 0xc0);
}
void setup() {
Serial.begin(9600);
rotary_init();
}
void loop() {
char result = rotary_process();
if (result) {
Serial.println(result == 0x40 ? "LEFT" : "RIGHT");
}
}
Ich muss dann ja das Ergebnis von result also aus diesem Block Serial.println(result == 0x40 ? "LEFT" : "RIGHT");
in eine Variable überführen. Mit if result == 0x40 geht das ja noch, dann gab ich eine Drehrichtung. Aber die andere bekomme ich irgendwie nicht hin.