Hello everybody,
I hope I am in the good sub, sorry if not.
I'm using an incremental encoder to generate a command. When I'm turning it in the clockwise direction, the Arduino generates a LOW state on the digital pin 8. When I'm turning it in the counterclockwise direction, the Arduino generates a LOW state on the digital pin 9.
These two pins are wired to the GPIO pins of my device (BrightSign HD224 player, datasheet here : https://support.brightsign.biz/hc/en-us/articles/218065937-GPIO-Which-pins-correspond-to-which-buttons-)
Then, according to the direction, the device will scroll in a image list (next or previous image).
The player needs a change of state on the GPIO pins in order to generate an event. This is why I do a digitalWrite(LOW), the rest state being HIGH.
Everything works well, except when I'm turning the encoder too fast (just for you to understand my needs, I want to create a kind of flipbook / folioscope).
I think the problem is that there is a continous LOW state in this case, so the player is not able to detect the event.
Here is my code, taken on a website and slightly adapted to my needs :
/* Stepper Motor using a Rotary Encoder
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
// defines pins numbers
// - enc_a is ENC Signal A line (Arduino digital pin 2)
// - enc_b is ENC Signal B line (Arduino digital pin 3)
#define ENC_A 2
#define ENC_B 3
#define CW_cmd 8
#define CCW_cmd 9
// Main loop refresh period.
#define REFRESH_MS 10
// Main serial data connection to computer.
#define BAUD_RATE 9600
// Encoder signal line states
volatile boolean state_a = 0;
volatile boolean state_b = 0;
// Rotation direction states
volatile boolean cwState = 0;
volatile boolean ccwState = 0;
// Encoder position
volatile int enc_pos = 0;
int enc_pos_prev = 0;
void setup() {
pinMode(ENC_A, INPUT);
pinMode(ENC_B, INPUT);
pinMode(CW_cmd,OUTPUT);
pinMode(CCW_cmd,OUTPUT);
state_a = (boolean) digitalRead(ENC_A);
state_b = (boolean) digitalRead(ENC_B);
attachInterrupt(0, interrupt_enc_a, CHANGE);
attachInterrupt(1, interrupt_enc_b, CHANGE);
Serial.begin(BAUD_RATE);
}
void loop() {
ccwState = 1;
cwState = 1;
//digitalWrite(CW_cmd, HIGH) ;
// digitalWrite(CCW_cmd, HIGH) ;
if (enc_pos > enc_pos_prev){
cwState = 0;
digitalWrite(CW_cmd, LOW) ;
//digitalWrite(CW_cmd, HIGH) ;
}
if (enc_pos < enc_pos_prev){
ccwState = 0;
digitalWrite(CCW_cmd, LOW) ;
//digitalWrite(CCW_cmd, HIGH) ;
}
enc_pos_prev = enc_pos;
// Emit data
Serial.print(state_a);
Serial.print("\t");
Serial.print(state_b);
Serial.print("\t");
Serial.print(cwState);
Serial.print("\t");
Serial.print(ccwState);
Serial.print("\t");
Serial.print(enc_pos);
Serial.print("\t");
Serial.print(enc_pos_prev);
Serial.print("\t");
Serial.print("\n");
delay(REFRESH_MS);
}
// Detect pulses from depth encoder.
void interrupt_enc_a()
{
if (!state_a) {
state_b ? enc_pos++: enc_pos--;
}
state_a = !state_a;
}
void interrupt_enc_b()
{
state_b = !state_b;
}
I tried to decrease the refresh time, or to add some digitalWrite(HIGH) in the loop but nothing works ...
Do you know if there is a way to create a kind of pulse signal ? Or any other solution to solve my problem ?
Many thanks ![]()
