There is no USPS pickup on Monday Feb 15th, 2016 because of President's Day. USPS orders placed after 11am EST Fri February 12th will go out Tuesday February 16. UPS orders will ship as normal.

Running a stepper without interrupting
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Running a stepper without interrupting

by IanJohnson on Fri Feb 22, 2013 1:08 am

I have a stepper motor which runs continuously to pull plastic filament to a spooler. I'm running it with an Uno and an Easy driver, and have a rotary encoder that dials the motor speed up and down by incrementing the step delay with each click. Each time the encoder changes the speed, it calculates an rpm, converts it to a feed rate for the filament, and writes it to the serial monitor.

The stepper runs in a loop with 1 revolution per loop with a delay between each step that is determined by the rotary encoder. The encoder interrupts to make speed changes. I want to add some additional features like an LCD interface, some sensor reading, and control of an additional DC motor which turns the spool.

How do I add additional functions in the loop without interrupting the motion of the stepper? Clicking the encoder already pauses the motor between speed changes due to the serial.print. It didn't pause before I added that in. Also when I started out with an example script, there was a serial.print in the loop with a delay to keep the output from flowing too fast. This meant that the serial.print happened once per revolution, and the motor paused after each revolution. So how do I keep stepper control separate from the other functions?

Code: Select all | TOGGLE FULL SIZE
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);
  digitalWrite(dirpin, LOW);     // Set the direction.

void loop(){
  int i;

  for (i = 0; i<6880; i++)       // Iterate for 6880 microsteps.
    digitalWrite(steppin, LOW);  // This LOW to HIGH change is what creates the
    digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.


void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) stepperspeed = stepperspeed + 5;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) stepperspeed = stepperspeed - 5;

  float revtime = (stepperspeed*.00688);
  float feedrate = (60/revtime)*1.37;
  Serial.print(" in/min ");
  Serial.println(" Delay");
  lastEncoded = encoded; //store this value for next time
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: Running a stepper without interrupting

by tldr on Fri Feb 22, 2013 1:37 am

timer interrupts. i'd have provided a link, but the arduino site seems to be down tonight.
"If I had known it was harmless, I would have killed it myself." - Phillip K. Dick, A Scanner Darkly
User avatar
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Please be positive and constructive with your questions and comments.