Pages: [1]   Go Down
Author Topic: Encoder driver steeper motor for DUE  (Read 623 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys,
trying to use the code from the Arduino mini but do not work.
Namely, the duo can not use the features of <delay.h>. Does anyone have any solutions.
I am a beginner and I can not solve this problem.
This is code

Code:
// encoder drive stepper motor
//


#define encoder_a 2 //keep this on and interrupt pin
#define encoder_b 3 //keep this on and interrupt pin
#define motor_step 4 //can be any pin
#define motor_direction 5 //can be any pin

#include <delay.h>
volatile long motor_position, encoder;

void setup () {
  //set up the various outputs
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
 
  // then the encoder inputs
  pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);
  // enable pullup as we are using an open collector encoder
  digitalWrite(encoder_a, HIGH);
  digitalWrite(encoder_b, HIGH);
 
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, encoderPinChangeA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  encoder = 0; //reseet the encoder position to 0
}

void loop() {
  //do stuff dependent on encoder position here
  //such as move a stepper motor to match encoder position
  //if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);// move stepper in reverse
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    delayMicroseconds(600); //_delay_us(200); //modify to alter speed
    motor_position++;
    encoder = 0; //encoder--;
  }
  else if (encoder < 0) {
    digitalWrite (motor_direction, LOW); //move stepper forward
    digitalWrite (motor_step, HIGH);
    digitalWrite (motor_step, LOW);
    delayMicroseconds(600); //_delay_us(200); //modify to alter speed
    motor_position--;
    encoder = 0; //encoder++;
  }
}

void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
    encoder--;
  }
  else{
      encoder++;
  }
}

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
    encoder--;
  }
  else {
    encoder++;
  }
 }
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 610
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

_delay_us was already commented out and replaced with delayMicroseconds.  It will probably compile if you simply remove that #include line.

In general, when porting C/C++ code, when some #include is missing, usually the very first thing you do is just comment it out and try compiling.  Then all the stuff that .h file defines, that isn't defined because you removed the .h, will become errors.  If you're lucky, they'll be simple thing things you can find a way to replace.  In this case, looks like someone already replaced the one thing that probably comes from that .h.

But one other thing you'll need to change is the numbers in those attachInterrupt lines, because the numbers work differently on Due.  Arduino has a "reference" section of the site, where you can find documentation on how attachInterrupt works.  You should find that page and read it to understand what to do. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I try some remove row #include  <delay.h> and there were no errors but the program does not work.
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in attachInterrupt().
See: http://arduino.cc/en/Reference/AttachInterrupt
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,
yes i to change is the numbers in those attachInterrupt lines.I had also to change in/out pin.
However, I have the same problem as before. At a higher speed encoder stepper motor slow work.
 If encoder slowly move work ok. There is no error in the Arduino mini but in the program.
Can you help me how can I fix the error that signals from encoder fast read and sent to the stepper motor.
This is new code:
Code:
#define encoder_a 22
#define encoder_b 23
#define motor_step 53
#define motor_direction 52

volatile long motor_position, encoder;

void setup () {
  //set up the various outputs
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
 
    pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);
    digitalWrite(encoder_a, HIGH);
  digitalWrite(encoder_b, HIGH);
 
    attachInterrupt(22, encoderPinChangeA, CHANGE);
    attachInterrupt(23, encoderPinChangeB, CHANGE);
  encoder = 0; //reseet the encoder position to 0
}

void loop() {
  //do stuff dependent on encoder position here
  //such as move a stepper motor to match encoder position
  //if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);// move stepper in reverse
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    delayMicroseconds(600); //_delay_us(200); //modify to alter speed
    motor_position++;
    encoder = 0; //encoder--;
  }
  else if (encoder < 0) {
    digitalWrite (motor_direction, LOW); //move stepper forward
    digitalWrite (motor_step, HIGH);
    digitalWrite (motor_step, LOW);
    delayMicroseconds(600); //_delay_us(200); //modify to alter speed
    motor_position--;
    encoder = 0; //encoder++;
  }
}

void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
    encoder--;
  }
  else{
      encoder++;
  }
}

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
    encoder--;
  }
  else {
    encoder++;
  }
 }




Logged

Pages: [1]   Go Up
Jump to: