Hello i am new in Arduino. I do project with rotary encoder and motor. I want that when i three time circling encoder to other side the motor finish first circle after that do the next circle.
This is my code.
void unStepMotor(int pin_1, int pin_2,int pin_3,int pin_4,int speedMotor){
digitalWrite(pin_1,HIGH);
digitalWrite(pin_2,LOW);
digitalWrite(pin_3,LOW);
digitalWrite(pin_4,LOW);
delay(speedMotor + 1);
//
digitalWrite(pin_1,HIGH);
digitalWrite(pin_2,HIGH);
digitalWrite(pin_3,LOW);
digitalWrite(pin_4,LOW);
delay(speedMotor + 1);
//
digitalWrite(pin_1,LOW);
digitalWrite(pin_2,HIGH);
digitalWrite(pin_3,LOW);
digitalWrite(pin_4,LOW);
delay(speedMotor + 1);
//
digitalWrite(pin_1,LOW);
digitalWrite(pin_2,HIGH);
digitalWrite(pin_3,HIGH);
digitalWrite(pin_4,LOW);
delay(speedMotor + 1);
//
digitalWrite(pin_1,LOW);
digitalWrite(pin_2,LOW);
digitalWrite(pin_3,HIGH);
digitalWrite(pin_4,LOW);
delay(speedMotor + 1);
//
digitalWrite(pin_1,LOW);
digitalWrite(pin_2,LOW);
digitalWrite(pin_3,HIGH);
digitalWrite(pin_4,HIGH);
delay(speedMotor + 1);
//
digitalWrite(pin_1,LOW);
digitalWrite(pin_2,LOW);
digitalWrite(pin_3,LOW);
digitalWrite(pin_4,HIGH);
delay(speedMotor + 1);
//
digitalWrite(pin_1,HIGH);
digitalWrite(pin_2,LOW);
digitalWrite(pin_3,LOW);
digitalWrite(pin_4,HIGH);
delay(speedMotor + 1);
//
}
int z;
int i;
int x=0;
int y=0;
int j;
int curso;
#define outputA 2
#define outputB 3
int counter = 0;
int aState;
int aLastState;
int oldcounter=0;
void setup(){
pinMode(8,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(11,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(11,LOW);
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
aLastState = digitalRead(outputA);
}
//
void loop(){
aState = digitalRead(outputA); // Reads the "current" state of the outputA
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
}
aLastState = aState;
if(oldcounter != counter){
if (z < 2)
{
z++;
}
else {
if (oldcounter < counter)
{
curso++;
x++;
} else {
curso--;
y++;
}
z = 0;
z++;
}
}
oldcounter=counter;
if(x>0){
Serial.print(x);
if(i<25){
i++;
unStepMotor(11,9,10,8,2);}
else{
i=0;
x--;}
}
else if(y>0){
Serial.print(y);
if(j<25){
j++;
unStepMotor(8,10,9,11,2);}
else{
j=0;
y--;}
}
}