Go Down

Topic: [SOLVED] Rotary Encoder only increments (Read 3196 times) previous topic - next topic

anishkgt

Jul 12, 2017, 10:02 pm Last Edit: Jul 13, 2017, 06:58 pm by anishkgt
Hi All,

Learning the rotary encoder and i've managed to get this far
Code: [Select]
int PinA = 6;
int PinB = 7;
int Counter = 10;
int PinALastState = LOW;
int pinAState = LOW;

void setup() {
  pinMode (PinA, INPUT);
  pinMode (PinB, INPUT);
  Serial.begin (9600);
}

void loop() {
  pinAState = digitalRead(PinA);
  if ((PinALastState == LOW) && (pinAState == HIGH))
  {
    if (digitalRead(PinB) == LOW) {
      Counter--;
    } else {
      Counter++;
    }
    Serial.println(Counter);
  }
  PinALastState = pinAState;
}


for some reason the decrements do not seem to working it just increments no matter how i turn the knob.

vinceherman

Just guessing, but does the digital read of PinB ever go LOW?
I would write a test sketch to read both pins and print the values.
Then you should see something like this:

00
01
11
10

with possible repetitions. 

anishkgt

No Vince, Just increments like below
2
3
4
5
6
7
8
9
10
11
12
13
14
15

anishkgt

#3
Jul 12, 2017, 10:59 pm Last Edit: Jul 12, 2017, 11:00 pm by anishkgt
here is my wiring just one 10K resistor between pin A and GND

anishkgt

half through it comes like this
169
170
169
170
171
172
173
172
171
170
169
170
171
172
173
172
173
174
175
176
177
176

anishkgt

and after a reset
2
1
0
-1
-2
-3
-4
-5
-6
-5
-6
-5
-4
-5
-6
-5
-4
-5
-4
-3
-2
-1
0
1
2
3
4
3
2
1
2
1
2

dougp

here is my wiring just one 10K resistor between pin A and GND
Per the photo it doesn't look like the ground side of the resistor is connected to ground, maybe I'm not seeing well. Should you not also have a resistor to ground for channel B? What happens if you swap the blu/orn wires on the Arduino header?
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

Southpark

#7
Jul 12, 2017, 11:47 pm Last Edit: Jul 12, 2017, 11:59 pm by Southpark
for some reason the decrements do not seem to working it just increments no matter how i turn the knob.
Check to see if you need to set the pinmode to "INPUT_PULLUP" or not.

Also.....  one end of your resistor doesn't even seem to be connected to anything. The leg is just connected to a rail, but nothing else is connected to it. If that resistor is needed..... then the problem here is..... you need to review the basics of bread-boards. The rows of little hole sockets in different sections of the bread-board are not always electrically-connected in the same direction.

anishkgt

Check to see if you need to set the pinmode to "INPUT_PULLUP" or not.

Also.....  one end of your resistor doesn't even seem to be connected to anything. The leg is just connected to a rail, but nothing else is connected to it. If that resistor is needed..... then the problem here is..... you need to re-learn/learn some things about breadboards.
changed the code to just INPUT and no visible change on the serial monitor.

The wiring is correct and the resistor is connected to GND and the other to pin A.

Southpark

#9
Jul 13, 2017, 12:03 am Last Edit: Jul 13, 2017, 12:10 am by Southpark
The wiring is correct and the resistor is connected to GND and the other to pin A.
The wiring is not correct in this photo you provided. If you go back to the bread-board manual, and look at the internal wiring of the bread-board along that rail.... you will find that your resistor leg is connected to nothing.

Take a look at the red coloured arrow.

Also, if you intend that resistor leg to be connected to ground, then probably need to make sure that it really needs to be connected to ground (ie.......the purpose of that resistor).


anishkgt

Per the photo it doesn't look like the ground side of the resistor is connected to ground, maybe I'm not seeing well. Should you not also have a resistor to ground for channel B? What happens if you swap the blu/orn wires on the Arduino header?
Added to resistor to pin B as well, no change. Swaped the wires in the arduino PINs A and B, no change there either.

anishkgt

#11
Jul 13, 2017, 12:09 am Last Edit: Jul 13, 2017, 12:12 am by anishkgt
The wiring is not correct in this photo you provided. If you go back to the bread-board manual, and look at the internal wiring of the bread-board along that rail.... you will find that your resistor leg is connected to nothing.

Take a look at the red coloured arrow.
checked and for some reason i missed that. but i've correct it now, not thing noticeable change tho.


dougp

re: photo in post #11 - where is the resistor for channel B?
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

Delta_G

Can you try what @vinceherman described in reply #1 and create a sketch that outputs the pin states instead of counting.  
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

anishkgt

i had removed since it did not make a difference.

Tried the test what vinceherman mentioned and all seems to go high only.
Code: [Select]
int PinA = 6;
int PinB = 7;
int val;
int Counter = 1;
int PinALastState = LOW;
int pinAState = LOW;

void setup() {
  pinMode (PinA, INPUT);
  pinMode (PinB, INPUT);
  Serial.begin (9600);
}

void loop() {
  pinAState = digitalRead(PinA);
  if ((PinALastState == LOW) && (pinAState == HIGH))
  {
    if (digitalRead(PinA) == LOW) {
      Serial.println(0);
    } else {
      Serial.println(1);
    }
    //Serial.println(Counter);
  }
  PinALastState = pinAState;
}

Go Up