Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Read the encoder - making servo from encoder and motor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Read the encoder - making servo from encoder and motor

by nekodrugi99 on Fri Jul 04, 2008 4:21 am

I am buying the encoder with 10000 ppr and I want with arduino to forward each step to PC. If I understood well, servo can be controlled for 1024 steps so I thought I will just somehow control the speed of the motor (DC or Stepper) and with encoder get the info when each step occured. It should rotate slow ~60steps per second, so I hope there will be no missing stepps.

Any ideas how to make it?
nekodrugi99
 
Posts: 25
Joined: Fri Jul 04, 2008 4:12 am

by mcleanb1 on Sun Jul 06, 2008 2:05 am

Hi,
This should do what you need to do. It will read an encoder with pin A going to 0 on the arduino and pin B going to 2. The code to read and print a position from the encoder was found here:http://www.arduino.cc/playground/Main/RotaryEncoders



Code: Select all | TOGGLE FULL SIZE
#include <AFMotor.h>

AF_Stepper motor(48, 2);

int val;
int encoder0PinA = 0;
int encoder0PinB = 2;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;


void setup() {
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);

  Serial.begin (9600);
  motor.setSpeed(600);

}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
      motor.step(1, BACKWARD, INTERLEAVE);  //edit this lines to tune encoder resolution and motor perfomance
      // Serial.print ("<B>");  //uncomment to print B to indicate direction of last step
    }
    else {
      encoder0Pos++;
      motor.step(1, FORWARD, INTERLEAVE);  //edit this lines to tune encoder resolution and motor perfomance
      //Serial.print ("<F>"); //uncomment to print F to indicate direction of last step
    }
    Serial.print (encoder0Pos);
    Serial.print ("/");
  }
  encoder0PinALast = n;
}
mcleanb1
 
Posts: 1
Joined: Sun Jul 06, 2008 1:51 am
Location: Chicago

by nekodrugi99 on Sun Jul 06, 2008 6:47 am

Thanks for the reply
Do you know how fast are encoder readouts made. Does this mean that the PC gets the info 9600 times per second? Is Arduino fast enough to implement also motor control and several other function within the main loop without problem?
nekodrugi99
 
Posts: 25
Joined: Fri Jul 04, 2008 4:12 am

by ahoeben on Wed Jul 09, 2008 3:07 am

The [url="http://openservo.com"]openservo[/url] uses the same AVR microcontroller as a controller for their servo boards, so it should be fast enough.

ahoeben
 
Posts: 21
Joined: Fri Apr 25, 2008 3:18 pm

Please be positive and constructive with your questions and comments.