The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

Running a stepper without interrupting

by IanJohnson on Fri Feb 22, 2013 12: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.
    delayMicroseconds(stepperspeed);     
  }                             

}


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(feedrate);
  Serial.print(" in/min ");
  Serial.print(stepperspeed);
  Serial.println(" Delay");
 
 
  lastEncoded = encoded; //store this value for next time
}
IanJohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 1:16 pm

Re: Running a stepper without interrupting

by tldr on Fri Feb 22, 2013 12: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
tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 12:34 am