int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
volatile long lastencoderValue = 0;
void setup() {
Serial.begin(19200);
btSerial.begin(9600);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop() {
lcd.print(encoderValue/29382.9);
Serial.print(encoderValue/29382.9);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
}
What has the colour of the encoder got to do with anything?
Your question is not very clear.
We normally require a schematic an proper question to be of help to you.
The code is not the correct way to read a rotary encoder anyway. It is normal to use a state machine driven by interrupts.
encoder/This is the Encoder I am using
Thanks, this is a lot more clear.
So how do you know that the Arduino freezes as opposed to the fact that the reading of the encoder suddenly stops working? Can you try blinking an LED in the loop function as well and see.
That encoder has such a high resolution it is possible that it overwhelms the speed of the Arduino to cope with it.
At a first stab I would try and speed up the ISRs, with things like using bytes instead of ints, and having the variables inside the ISR as volatile global variables instead of declaring them every time the ISR is run.
Also I can't see where the btSerial.begin is defined, is it supposed to be a software serial generated port?
Again I can't see why these should be ints and not bytes. Is the processor you are using actually an Uno or is it something else?
Also I can't see the 4K7 pull up resistors mentioned in the text.
Got your point I will use bytes instead of ints actually this is not the complete code it is 1/10th part of a code and I am 100% sure that the board is freezing because I am printing values in bluetooth terminal and LCD screen after capturing the value from different sensors such as encoder, MLX90614 & IMUs after clicking a push button for 2 seconds and it is getting stuck so its pretty obvious and yes I am using UNO
I do not share your confidence in that. It is always better to start with code for what you actually have.
While the code does enable the internal pullup resistors in a very quaint old fashioned way, these are very weak 30 to 55K, and an external pull up resistor of 4K7 would be much harder and give you better response.
I would suggest that you post all your code that matches your software even if it means writing and wiring up a special reduced code that shows the fault.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
