I need help with a rotary encoder problem. Is it possible to get two separate counts from a single rotary encoder? In the code below I have three physical encoders setup and a forth "virtual" encoder set to count if a button is pushed. The problem is that the count for the physical and virtual encoder stays sequential. I need to make them count only if activated by the button.
I'm working on a camera RCP which may require as many as 12 rotary encoders to control various parts of the camera vision. The simplest solutions would probably be to run a couple of microcontrollers (teensy 3.1) and have them talk to each other. Before I head down that route I wanted to see if I can make 1 rotary encoder perform two functions.
/*
TESTING MULTI USE OF ENCODER FOR BOTH WB AND BB CONTROL. BUTTON WORKS
BUT ONLY WITH MOMENTARY ACTION. CONTROL WORKS BUT THERE IS NO SEPARATION
BETWEEN CONTROLS. (SETTINGS CONTINUE TO COUNT WHEN THEY SHOULDN'T)
4/5/15
*/
#include <Encoder.h>
//#define HWSERIAL Serial3 //used later to send commands to camera
const int buttonAWB = 7;
int bstateAWB = 0;
Encoder knobLeft(0, 1);
Encoder knobCentre(2, 3);
Encoder knobRight(4, 5);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
delay(2000);
Serial.println("TwoKnobs Encoder Test:");
knobLeft.write(513);
knobCentre.write(500);
knobRight.write(450);
pinMode(buttonAWB, INPUT); //testing push button for BB control of encoders
digitalWrite(7, HIGH);
}
long positionLeft = -999;
long positionCentre = -999;
long positionRight = -999;
long positionBBRed = -999; //test to try and separate control counts
void loop() {
bstateAWB = digitalRead(buttonAWB);
if (bstateAWB == HIGH) {
encoderOneChk();
encoderTwoChk();
encoderThreeChk();
}
else {
encoderFourChk();
}
}
void encoderOneChk() {
int newLeft = knobLeft.read();
// delay(500); //used to minimize the times a serial command is sent
if (newLeft != positionLeft) {
Serial.print("Left = ");
Serial.println(newLeft);
positionLeft = newLeft;
}
}
void encoderTwoChk() {
int newCentre = knobCentre.read();
if (newCentre != positionCentre) {
Serial.print("Centre = ");
Serial.println(newCentre);
positionCentre = newCentre;
}
}
void encoderThreeChk() {
int newRight = knobRight.read();
if (newRight != positionRight) {
Serial.print("Right = ");
Serial.println(newRight);
positionRight = newRight;
}
}
void encoderFourChk() { //test to try and separate control counts
int bbRed = knobLeft.read();
// delay(500); //used to minimize the times a serial command is sent
if (bbRed != positionBBRed) {
Serial.print("bbRed = ");
Serial.println(bbRed);
positionBBRed = bbRed;
}
}