Author Topic: Re: Arduino Encoder to Stepper Motor question, help?  (Read 123 times)

0 Members and 1 Guest are viewing this topic.

Offline bkukuk62

  • Contributor
  • Posts: 7
  • Country: us
Re: Arduino Encoder to Stepper Motor question, help?
« on: September 17, 2019, 05:25:34 pm »
All,
I have been reviewing the following code from gman4925.   I would like to see if this could be modified to include a button that would break out of the interactive mode using the encoder for rotation to turning the stepper clockwise until another button is pushed using a potentiometer for speed control.
Has anyone done this that could help me.

thanks,
-Bkukuk62

//Arduino code for controlling stepper motor from encoder
//video at
//reconstructed code from 5+ years ago
//untested in current form

 #define encoder_a 2    //encoder a = arduino pin 2
 #define encoder_b 3    //encoder b = arduino pin 3
 #define motor_step 4    //motor step = arduino pin 4
 #define motor_direction 5    //motor direction = arduino pin 5

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);
    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;
}

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

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


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);
        digitalWrite(motor_step, HIGH);
        digitalWrite(motor_step, LOW);
        _delay_us(200);
        motor_position++;
        encoder = 0;
    }
    else if (encoder < 0)
    {
        digitalWrite(motor_direction, LOW);
        digitalWrite(motor_step, HIGH);
        digitalWrite(motor_step, LOW);
        _delay_us(200);
        motor_position--;
        encoder = 0;
    }
}
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 1575
  • Country: us
Re: Arduino Encoder to Stepper Motor question, help?
« Reply #1 on: September 17, 2019, 06:20:40 pm »
Which part are you having trouble with?

Button/Switch or Potentiometer? Plenty of tutorials on those.

Speed? Change the delay value.

Logic?
  if (your_way)
  {
    // your code
  }
  else
  {
    // existing code
  }
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf