Hello!
Last week I posted about a project I'm making and thanks to some guys I continued nicely.
Now...
I have my stepper motor and my encoder.
I need to calculate some things in order to proceed with my project, by now I need to read the encoder position when my stepper moves some steps...
But... I noticed a few codes ago that I cant manage to move my stepper without problems if my code includes Serial.print's...
Here's my code... any time my stepper moves -- my encoder moves
................................... any time my encoder moves -- my encoder position in printed in my screen
But my stepper slows down when it happens affecting the steps... Any ideas? I really need to know my encoder position!!
#include <RotaryEncoder.h>
static int pos = 0;
RotaryEncoder encoder(A2, A3);
#define DIR_PIN 2
#define STEP_PIN 3
#define EN_PIN 3
int ar = 11;
int ab = 12;
int arr;
int aba;
float v;
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH);
pinMode(ar, INPUT);
pinMode(ab, INPUT);
Serial.begin(9600);
}
void loop()
{
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
Serial.print(newPos);
Serial.println();
pos = newPos;
}
v=4;
arr = digitalRead(ar);
aba = digitalRead(ab);
if (arr == HIGH)
{
rotate(-1, v);
}
if (aba == HIGH)
{
rotate(1,v);
}
}
void rotate(int steps, float speed){
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN,dir);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}