Go Down

Topic: encoder (Read 16 times) previous topic - next topic

Cheater

Just so you know, some encoders have over 500 pulses per revolution.
Turning on a LED after every 10 will make it look always on. ;)

Is there a URL for the encoder your looking at?

mingki

Hi, I am trying to figure out the purpose of below programing.
does anyone know what this commend for? Especially the commends in bold letters.
void loop() {
n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n; }

mem

#7
Feb 03, 2008, 12:08 pm Last Edit: Feb 03, 2008, 12:56 pm by mem Reason: 1
Quote
Hi, I am trying to figure out the purpose of below programing.
does anyone know what this commend for? Especially the commends in bold letters.
void loop() {
n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n; }

// encoder0Pos          holds the position for encoder0
// encoder0PinA         is the pin that encoder0 is connected to
// encoder0PinALast   is the last processed state of encoder0

void loop() {
n = digitalRead(encoder0PinA);   // n is current state of encoder0PinA pin
  if ((encoder0PinALast == LOW) && (n == HIGH)) {   // check if encoder0PinA pin has changed state
    if (digitalRead(encoder0PinB) == LOW) {  
         encoder0Pos--;   // if it has changed and its now low decrement  encoder0Pos;
    } else {
     encoder0Pos++;   // if it has changed and its now high, increment encoder0Pos
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }  
  encoder0PinALast = n; }   // set the variable holding the previous state to the value n read above


mingki

#8
Feb 04, 2008, 01:25 pm Last Edit: Feb 04, 2008, 01:48 pm by mingki Reason: 1
thank you very much for your answer.
i still having some trouble understanding some of the program. sorry.
Please help me to understand some points.
Quote
Quote
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }  
  encoder0PinALast = n; }   // set the variable holding the previous state to the value n read above

Do i need to use Serial.print commend in order to take puls signals(using arduino) from encoder in real time?
could you tell me what ("/") does for?
i hope i could get some more explination on this quote.
Quote
Quote

encoder0PinALast = n; }   // set the variable holding the previous state to the value n read above



mem

I assume the author of that code was using the serial port to display the status of the encoder. You don't need to send this data to the serial monitor but it is useful to see what is happening.

Anyway, the variable encoder0PinALast is used to remember if the previous state of the encoder sensor pin was high or low. The code uses this to determine the direction the encoder is rotating.

The variable encoder0Pos contains a count of the encoder position and you would use this value in your application. By the way, what is your application?  It would be easier to guide you knowing a little more about what you are trying to achieve.

If you don't have a specific application in mind and just want to learn about encoders, then have a search on google for some tutorials on the use of phototransistors. Also have a search on any tutorials on the innards of mechanical mice, they are a good source of cheap (free?) encoders to play with if you have an old one lying around.

Go Up