Hey guys, I'm new to Arduino and Processing and coding in general and could really use a hand. I've got a rotary encoder hooked up to a hand drill and eventually my goal is to make every every turn of the handrill spin through 12 frames or so of a 24 frame looped animation on a projector. I'm early in the project but I've come against 3 major issues which I'm not yet quite equipped to deal with. I'm sure with time I'll figure it out but any help at all I'll greatly appreciate.
Issue #1: My encoder functions great with the code below, which I found on another site. The encoder has 24 steps per rotation which is dandy and all, but the problem is that every step it prints in increments of 4. Meaning one step takes me from 0 to 3, the next from 3 to 7, and so on. My diabolical plan was that I'd name my still frames something like MyAnimation_(VARIABLE).png, and I'd just call up MyAnimation_0.png and MyAnimation_1.png and so on, but that won't work if the code is going to skip every 3 frames. How can I tame it? I imagine it's something simple like dividing something by 4 somewhere? But what exactly?
Issue #2: The encoder cranks up to 255 and then zips right back to zero. If I go backwards from 0, then it goes to 255. The behavior itself is great for my loop, but the number 255 is no good. I need to make the cap something divisible by 24, like 239 for example (That way I can save out frames for 10 consecutive loops and personalize each loop so it doesn't seem quite so...loopy.) I confess I have no idea what to do here.
Issue #3: This is more of a question for another forum and I'm more worried about taming the encoder right now, but if you guys have any ideas. What are some good ways to call on and playback the frames of the animation, preferably fullscreen? Again this is something I'm worried about more later on and at that point I'll find the appropriate forum for it, but if you just happened to have some information that points me in the right direction ahead of time. It would be most appreciated.
Thanks so much in advance. Here's the code I'm using at the moment:
/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15
#define ENC_PORT PINC
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
Serial.begin (115200);
Serial.println("Start");
}
void loop()
{
static uint8_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
/**/
tmpdata = read_encoder();
if( tmpdata ) {
Serial.print("Counter value: ");
Serial.println(counter, DEC);
counter += tmpdata;
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}