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
//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
}