Go Down

Topic: reading the encoder pulses without the Encoder library (Read 461 times) previous topic - next topic

koronus

hi
I have an analog require encoder I mean that the channels of the encoder must be connected to the analog pins of the Arduino board, I know that there is a library <Encoder.h> but I want to read the pulses of the encoder by writing code and I won't use this library because I want to extend this code to the other software

cattledog

Quote
I have an analog require encoder I mean that the channels of the encoder must be connected to the analog pins of the Arduino board
Can you attach the document, or provide a link to the data sheet for this encoder?

koronus


Willpatel_Kendmirez

#3
Jan 17, 2019, 07:56 am Last Edit: Jan 17, 2019, 07:58 am by Willpatel_Kendmirez
Well that's just a normal encoder, use the normal logic.

  • If output1 has changed and output 2 is the same, count one click in one direction.
  • If output1 has changed and output 2 is different, count one click in the other direction.





cattledog

I don't see anything in the datasheet which would cause you to say
Quote
I have an analog require encoder I mean that the channels of the encoder must be connected to the analog pins of the Arduino board,
In fact Note 1 says
Quote
1.The output signal is open collector structure
This would indicate a digital input with a pull-up resistor to Vcc.

Please explain why you think this encoder must be connected to the analog pins of the Arduino.

Willpatel_Kendmirez

hi
I have an analog require encoder I mean that the channels of the encoder must be connected to the analog pins of the Arduino board, I know that there is a library <Encoder.h> but I want to read the pulses of the encoder by writing code and I won't use this library because I want to extend this code to the other software
It just struck me though, there's no actual question in there. So what is it you need help with, if anything?


koronus

I test the encoder with encoder library in the Arduino software, when I use digital pins for encoder the pulses had 0 values and when I connect the encoder pins to the analog pins of the Arduino the pulses had correct values, and my question is how to write a code to read the pulses of the analog pins without using Encoder library and I just have analogRead(A0) and analogRead(A1) how I could write a code to convert this reading of the analog pins to the pulses of the encoder?

Willpatel_Kendmirez

I test the encoder with encoder library in the Arduino software, when I use digital pins for encoder the pulses had 0 values and when I connect the encoder pins to the analog pins of the Arduino the pulses had correct values
I'd be keen to see the sketch/es and the connections for those two scenarios, along with the output.

I wonder if you had pullups on the digital pins, either external or pinMode()-ed?







koronus

I have written this code:
Code: [Select]
#include <Encoder.h>
Encoder myEncoder(A0,A1);
double Position;
int t;
void setup() {
  Serial.begin(250000);
  Serial.println("DC POSITION.");
 
}
long oldPosition = -999;
void loop() {
  long newPosition = myEncoder.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition ;
  }
Serial.print("t");
Serial.println(t);
Serial.print("Position = ");
Serial.println(newPosition);

  analogWrite(5,60);
 
  digitalWrite(8,LOW);
  t=millis();
  if (t<2000){
     digitalWrite(7,HIGH);
  }     
  else if(t>=2000 && t<4000){ digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  }
  else{  digitalWrite(8,LOW);

  }
}

and the pulses had correct values and as I said I have used the ENCODER LIBRARY but I don't want to use this library, I want to write the other code to read the pulses of the encoder

koronus

let me ask my question clearly
when we use this code:
Code: [Select]
# include <Encoder.h>
Encoder one(pina,pinb);
void loop {
a=one.read()};

what is this library and encoder reading doing?
(what is the source code of the Encoder Library?)

Willpatel_Kendmirez

let me ask my question clearly
You mean:

Quote
let me ask my [new, totally different]question clearly
(what is the source code of the Encoder Library?)
Check the .h file in the encoder folder in your sketchbook/libraries.


Willpatel_Kendmirez

I want to write the other code to read the pulses of the encoder
.... and I explained how to do that in #3.


Willpatel_Kendmirez

#12
Jan 17, 2019, 09:41 am Last Edit: Jan 17, 2019, 09:44 am by Willpatel_Kendmirez
And btw you still didn't show the connections and code for when it didn't work on the digital pins.

edit: and since the analog pins are digital pins anyway, when you used the encoder library with pins A0 and A1, you were actually using digital pins; the library won't have been doing anything different.



koronus

.... and I explained how to do that in #3.


ok thank you I will write the code as:
Code: [Select]

count=0;
a0=analogRead(A0);
a1=analogRead(A1);
if (a0 !=a0 && a1=a1){
count=count+1;}
else if (a0 !=a0 && a1 !=a1){
count=count-1}
}[\code]

is it correct?

Willpatel_Kendmirez

Quote
is it correct?
How can something not be equal to itself?

Code: [Select]
if (a0 !=a0...

You need the StateChangeDetect example I'd say.


Go Up