Mapping serial input to control speed/direction

I have a rotary encoder now talking to oF (moderately) accurately. As an int, it’s sending values 0 - 65535 on each click. (A full rotation is about 12 clicks, so starting at 0 it would output a single ‘1’, ‘2’, etc on each click)

Now I’m attempting to map that value to a video stream, so it will only play when being spun. (FF for one direction, RW for the other)

I don’t need anyone to write out all the code for this, I just want to know if anyone has some pointers for what direction I should try and tackle this. I’ve done a small Arduino code before that found the average values for a particular amount of time, but I need something a little more elegant and stable than that for this.

Ideally I’d like the output to be able to go directly into the ‘speed’ value in the movieExample included in oF:

  
  
	if(!frameByframe){  
        int width = ofGetWidth();  
        float pct = (float)x / (float)width;  
        float speed = (2 * pct - 1) * 5.0f;  
        fingerMovie.setSpeed(speed);  
	}  
}  
  

Thanks!

From what I understand maybe ofMap could help?

http://www.openframeworks.cc/documentation?adv=yes&detail=ofMath#ofMap

ofMap would help take it from the massive range to something easier to work with, but I need a way to register change over time. So if the values are changing faster, the speed value is increased, and if the values are static the speed drops to zero.

normally, rotaries on midi controllers only send a +/-1 on “click”, depending on direction (+ is CW). In some controllers this value gets increased if turned faster (e.g. up to ~8 on my novation remote zero if I spin really fast). I would model the behaviour on the real-world thing, and do any playhead tracking just in your code.

If you only manage it to spit out +/-1, you could track the time since the last impulse with the correct sign, and if the time is short, enlarge the increment, and add this value to the playhead.

Having done this myself with a cheap rotary encoder I just kept a counter in the OF code that I incremented/decremented depending on the value coming from the serial connection:

  
  
float spdAdj;  
if(serialVal == '1') {   
   spdAdj = 0.05;  
} else {  
   spdAdj = -0.05;  
}  
speed += spdAdj;  
  

on the Arduino side just send a 0 when the encoder is negative and a 1 when it’s pos:

  
  
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {    
   Serial.write('1');  
} else {                                     
   Serial.write('0');  
}  
  

joshuajnoble,

I’m looking at the serial monitor in Arduino from that code and things are responding appropriately, but it doesn’t seem like that distinguishes which way the encoder is moving. It gives ‘0’ for CW or CCW turns. It definitely seems like a very accurate way to send speed data to oF though.

Ah, yeah, depending on the rotary encoder you might need to set that up differently. There’s a few ways of working with a rotary encoder here http://www.arduino.cc/playground/Main/RotaryEncoders you might want to try some of them out to see which works best for your encoder and circuit.

Just to bring some closure here (and to anyone who stumbles across this looking for a similar solution) I ended up scratching the idea of tracking the speed of the rotary encoder. In my scenario it was actually a hundred times easier to just use the nextFrame() and previousFrame(). The code in Arduino just sends ‘1’ or ‘2’, and oF then pushes the video forward or backward depending on how you want to calibrate it. If anyone is interested in seeing my Arduino code feel free to contact me.

Thanks for all the help everyone! This has been a truly helpful and knowledgable community. Looking forward to knowing enough to help people myself one day.